import pathlib
import typing
import datetime
from re import compile as compile_expression
from re import sub as replace_matched
from pandas import DataFrame, concat
from pathlib import Path
from numpy import Inf

from kivy import require as version_require
from kivy.app import App
from kivy.config import Config
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.metrics import mm
from kivy.properties import StringProperty, ObjectProperty
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition
from kivy.uix.textinput import TextInput
from kivy.uix.treeview import TreeViewNode, TreeViewLabel

import matplotlib.pyplot as plot_system

from PyJMy.json_file import json_read
from PyJMy.debug import debug_message

from accounting import Ledger, open_ledger

#needed for kv file load
from nametreeviewer import NameTreeViewer
from textureviewer import TextureViewer
from dataframetable import DataFrameTable

def kivy_initialize() :
    version_require('2.0.0')
    Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
    Builder.load_file("stocked_up.kv")

class LedgerNameInput(TextInput):

    pattern = compile_expression('[^A-Za-z0-9_]')

    def insert_text(self, substring, from_undo=False) :
        s = replace_matched(LedgerNameInput.pattern, "", substring)
        return super().insert_text(s, from_undo=from_undo)

class LedgerLoader(Screen) :

    root_path = StringProperty(None)

    def on_load_ledger(self, path, _) :
        debug_message("[LedgerLoader] on_load_ledger fired")

        if str(self.manager.data_root_directory.absolute()) != path :
            debug_message(f"[LedgerLoader] Loading Ledger path: {path}")

            self.manager.load_ledger(pathlib.Path(path))

class LedgerCreator(Screen) :

    def on_create_ledger(self, ledger_name) :
        debug_message("[LedgerCreator] on_create_ledger fired")
        debug_message(f"[LedgerCreator] Create Ledger named: {ledger_name}")

        ledger_path = self.manager.data_root_directory.joinpath(ledger_name)
        if not ledger_path.exists() :
            ledger_path.mkdir()

        self.manager.load_ledger(ledger_path)

class LedgerAccountTreeViewNode(AnchorLayout, TreeViewNode) :

    account_name_entry = ObjectProperty(None)

    def __init__(self, account_name, account_open_callback, **kwargs) :
        super(LedgerAccountTreeViewNode, self).__init__(**kwargs)

        self.account_name = account_name
        self.account_name_entry.text = account_name
        self.open_callback = account_open_callback

    def open(self) :
        self.open_callback(self.account_name)

class LedgerViewer(Screen) :

    tree_view_widget = ObjectProperty(None)

    def set_ledger(self, ledger : Ledger) -> None :
        debug_message("[LedgerViewer] set_ledger called")

        self.ledger = ledger
        internal_node_cb = lambda name, is_active : TreeViewLabel(text=name, no_selection=True, is_open=is_active)
        external_node_cb = lambda name : LedgerAccountTreeViewNode(name, self.__view_account_transactions)

        self.tree_view_widget.init_tree_viewer(internal_node_cb, external_node_cb)
        self.tree_view_widget.add_list("Base Accounts", self.ledger.get_base_account_names())
        self.tree_view_widget.add_tree("Derived Accounts", self.ledger.category_tree)

    def __view_account_transactions(self, account_name : str) -> None :
        account_data = self.ledger.get_account_table(account_name)

        new_screen = AccountViewer(account_name, account_data, [0.1, 0.70, 0.08, 0.12])
        self.manager.push_overlay(new_screen)

    def view_unused_transactions(self) :
        account_data = self.ledger.get_unaccounted_transaction_table()

        new_screen = AccountViewer("Unaccounted", account_data, [0.05, 0.1, 0.65, 0.08, 0.12])
        self.manager.push_overlay(new_screen)

    def show_data_visualizer(self) :

        new_screen = DataPlotter()
        new_screen.set_ledger(self.ledger)
        self.manager.push_overlay(new_screen)

class AnalyzeLedgerTreeIncludeNode(AnchorLayout, TreeViewNode) :
    name_entry = ObjectProperty(None)
    toggle_inclusion = ObjectProperty(None)

    def __init__(self, name, callback, **kwargs) :
        super(AnalyzeLedgerTreeIncludeNode, self).__init__(**kwargs)

        self.name = name
        self.name_entry.text = name
        self.toggle_inclusion.active = True
        self.toggle_callback = callback

    def toggle(self) :
        self.toggle_callback(self)

TransactionGroupDict = typing.Dict[str, DataFrame]

def get_full_subtree_timeseries(ledger : Ledger, leaf_accounts : typing.List[str]) -> DataFrame :

    total_start_value : float = 0.0
    df_list = []
    for account_name in leaf_accounts :
        account = ledger.get_account_data(account_name)
        total_start_value -= account.start_value
        df_list.append(DataFrame({
            "timestamp" : account.transactions.timestamp.values,
            "delta" : -account.transactions.delta.values
        }))

    subtree_timeseries = concat(df_list, ignore_index=False).sort_values(by=["timestamp"], kind="stable", ignore_index=True)
    subtree_timeseries = subtree_timeseries.groupby(by=["timestamp"], as_index=False).sum()
    subtree_timeseries.delta = total_start_value + subtree_timeseries.delta.cumsum()

    first_row = DataFrame({"timestamp": [0.0], "balance": [total_start_value]})
    balances = DataFrame({"timestamp": subtree_timeseries.timestamp, "balance": total_start_value + subtree_timeseries.delta.cumsum()})
    return concat([first_row, balances], ignore_index=False)

def collect_subtree_timeseries(ledger : Ledger, leaf_accounts : typing.List[str], start_time_point : float, end_time_point : float) -> DataFrame :

    subtree_timeseries = get_full_subtree_timeseries(ledger, leaf_accounts)

    existing_start_time_point = subtree_timeseries.query(f"timestamp == {start_time_point}")
    if len(existing_start_time_point.index) == 0 :
        earlier_timepoints = subtree_timeseries.query(f"timestamp < {start_time_point}")
        select_first_row = DataFrame({"timestamp": [start_time_point], "balance": earlier_timepoints.tail(1).balance.values}) #assume one row
        subtree_timeseries = concat([select_first_row, subtree_timeseries], ignore_index=True)

    existing_end_time_point = subtree_timeseries.query(f"timestamp == {end_time_point}")
    if len(existing_end_time_point.index) == 0 :
        earlier_timepoints = subtree_timeseries.query(f"timestamp < {end_time_point}")
        select_last_row = DataFrame({"timestamp": [end_time_point], "balance": earlier_timepoints.tail(1).balance.values}) #assume one row
        subtree_timeseries = concat([subtree_timeseries, select_last_row], ignore_index=True)

    return subtree_timeseries.query(f"timestamp >= {start_time_point}").query(f"timestamp <= {end_time_point}")

def get_selected_account_sets(ledger : Ledger, tree_view : NameTreeViewer, start_time_point : float, end_time_point : float) -> TransactionGroupDict :
    transaction_groups : TransactionGroupDict = {}
    for node in tree_view.get_visible_frontier_nodes() :
        if node.toggle_inclusion.active :
            leaf_accounts = [n.name_entry.text for n in tree_view.get_all_subtree_nodes(node) if n.is_leaf]
            transaction_groups[node.name_entry.text] = collect_subtree_timeseries(ledger, leaf_accounts, start_time_point, end_time_point)

    return transaction_groups

class DataPlotter(Screen) :

    from_date_textbox = ObjectProperty(None)
    to_date_textbox = ObjectProperty(None)

    absolute_scale_radio = ObjectProperty(None)
    normalized_scale_radio = ObjectProperty(None)

    series_scale_radio = ObjectProperty(None)
    total_scale_radio = ObjectProperty(None)

    tree_view_widget = ObjectProperty(None)
    figure_container = ObjectProperty(None)

    def __init__(self, **kwargs : typing.ParamSpecKwargs) -> None :
        super(Screen, self).__init__(**kwargs)

    def set_ledger(self, ledger : Ledger) -> None :
        debug_message("[LedgerViewer] set_ledger called")

        self.ledger = ledger

        internal_node_cb = lambda name, is_active : AnalyzeLedgerTreeIncludeNode(name, self.__toggle_subtree, is_open=is_active)
        external_node_cb = lambda name : AnalyzeLedgerTreeIncludeNode(name, self.__toggle_subtree)

        self.tree_view_widget.init_tree_viewer(internal_node_cb, external_node_cb)
        self.tree_view_widget.add_tree("External Accounts", self.ledger.category_tree)

    def absolute_series_expenses(self, transaction_groups : TransactionGroupDict, from_timestamp : float, to_timestamp : float) -> None :

        min_v = Inf
        max_v = -Inf
        for name, series_data in transaction_groups.items() :
            t = series_data.timestamp
            v = series_data.balance.values
            min_v = min(min(v), min_v)
            max_v = max(max(v), max_v)
            self.plotted_axis.step(t, v, where="post", label=name)

        self.plotted_axis.set_xlabel("time")
        self.plotted_axis.set_xlim(from_timestamp, to_timestamp)
        self.plotted_axis.set_ylabel("value")
        self.plotted_axis.set_ylim(min_v, max_v)
        self.plotted_axis.set_title("Absolute Series Expenses")
        self.plotted_axis.legend()

    def normalized_series_expenses(self, transaction_groups : TransactionGroupDict, from_timestamp : float, to_timestamp : float) -> None :
        pass

    def absolute_total_expenses(self, transaction_groups : TransactionGroupDict, from_timestamp : float, to_timestamp : float) -> None :

        totals = {}
        for name, series_data in transaction_groups.items() :
            totals[name] = sum(series_data.balance.values)

        min_v = min(totals.values())
        max_v = max(totals.values())

        self.plotted_axis.bar(totals.keys(), totals.values(), color='blue', alpha=0.7)
        self.plotted_axis.set_xlabel("Account")
        self.plotted_axis.set_ylabel("Amount")
        self.plotted_axis.set_ylim(min_v, max_v)
        self.plotted_axis.set_title("Absolute Total Expenses")

    def normalized_total_expenses(self, transaction_groups : TransactionGroupDict, from_timestamp : float, to_timestamp : float) -> None :
        pass

    def make_plot(self) :

        assert self.absolute_scale_radio.active != self.normalized_scale_radio.active
        is_absolute = self.absolute_scale_radio.active

        assert self.series_scale_radio.active != self.total_scale_radio.active
        is_series = self.series_scale_radio.active

        from_time_point = datetime.datetime.strptime(self.from_date_textbox.text, "%Y-%b-%d").timestamp()
        to_time_point = datetime.datetime.strptime(self.to_date_textbox.text, "%Y-%b-%d").timestamp()

        if from_time_point <= to_time_point :

            transaction_groups = get_selected_account_sets(self.ledger, self.tree_view_widget, from_time_point, to_time_point)

            self.plotted_figure, self.plotted_axis = plot_system.subplots()
            if is_series :
                if is_absolute :
                    self.absolute_series_expenses(transaction_groups, from_time_point, to_time_point)
                else :
                    self.normalized_series_expenses(transaction_groups, from_time_point, to_time_point)
            else :
                if is_absolute :
                    self.absolute_total_expenses(transaction_groups, from_time_point, to_time_point)
                else :
                    self.normalized_total_expenses(transaction_groups, from_time_point, to_time_point)
            buffer_data, buffer_size = self.plotted_figure.canvas.print_to_buffer()
            self.figure_container.set_texture(buffer_data, buffer_size)

    def __toggle_subtree(self, root_node) :
        is_active = root_node.toggle_inclusion.active
        for node in self.tree_view_widget.get_all_subtree_nodes(root_node) :
            node.toggle_inclusion.active = is_active

class AccountViewer(Screen) :

    account_name_label = ObjectProperty(None)
    account_data_table = ObjectProperty(None)

    def __init__(self, account_name : str, account_data : DataFrame, column_relative_sizes : typing.List[float], **kwargs : typing.ParamSpecKwargs) -> None :
        super(Screen, self).__init__(**kwargs)

        self.account_name_label.text = account_name
        self.account_data_table.set_data_frame(account_data, column_relative_sizes)

    def filter_by_description(self, match_string : str) -> None :
        if match_string == "" :
            self.account_data_table.filter_by(lambda df : df)
        else :
            self.account_data_table.filter_by(lambda df : df[df['Description'].str.contains(match_string, regex=False)])

class StockedUpAppManager(ScreenManager) :

    def __init__(self, **kwargs : typing.ParamSpecKwargs) :
        super(ScreenManager, self).__init__(**kwargs)

        self.__overlay_stack : typing.List[Screen] = []

    def swap_screen(self, screen_name : str) -> typing.Any :
        debug_message(f"[StockedUpAppManager] swap_screen {screen_name}")
        if len(self.__overlay_stack) > 1 :
            debug_message(f"Failed, currently has overlays! {[s.name for s in self.__overlay_stack]}")
            return None
        elif self.has_screen(screen_name) :
            if len(self.__overlay_stack) > 0 :
                debug_message(f"Previous = {self.__overlay_stack[-1].name}")
                assert self.current == self.__overlay_stack[-1].name
                self.__overlay_stack.pop()
            else :
                debug_message(f"First screen pushed, so no previous, current = {self.current}")
            screen = self.get_screen(screen_name)
            return self.push_overlay(screen)
        else :
            debug_message(f"Failed, screen {screen_name} does not exist!")
            return None

    def push_overlay(self, screen : Screen) -> typing.Any :
        debug_message(f"[StockedUpAppManager] push_overlay {self.current} -> {screen.name}")
        self.__overlay_stack.append(screen)
        return super().switch_to(screen, direction="left")

    def pop_overlay(self) -> typing.Any :
        assert len(self.__overlay_stack) > 1, "No overlays on stack!"
        assert self.current == self.__overlay_stack[-1].name
        debug_message(f"[StockedUpAppManager] pop_overlay {self.current} -> {self.__overlay_stack[-2].name}")
        next_screen = super().switch_to(self.__overlay_stack[-2], direction="left")
        self.remove_widget(self.__overlay_stack.pop())
        return next_screen

    def import_ledgers(self) :

        for ledger_import in self.ledger_configuration.ledgers :
            ledger_data_path = self.data_root_directory.joinpath(ledger_import.name)
            if not ledger_data_path.exists() :
                debug_message(f"Creating ledger folder {ledger_data_path}")
                ledger_data_path.mkdir()
            Ledger(ledger_data_path, ledger_import)

        self.load_default_ledger()

    def load_ledger(self, ledger_path : pathlib.Path) -> None :
        ledger = open_ledger(ledger_path)
        if ledger is not None :
            ledger_viewer = LedgerViewer()
            ledger_viewer.set_ledger(ledger)
            self.push_overlay(ledger_viewer)

    def load_default_ledger(self) :

        default_ledger_path = self.data_root_directory.joinpath(self.ledger_configuration.default_ledger)
        self.load_ledger(default_ledger_path)

class StockedUpApp(App) :

    def __init__(self, data_directory : pathlib.Path, **kwargs : typing.ParamSpecKwargs) :
        super(StockedUpApp, self).__init__(**kwargs)

        Window.size = (1600, 900)
        Window.left = (1920 - 1600) / 2
        Window.top = (1080 - 900) / 2

        if not data_directory.exists() :
            data_directory.mkdir()
        assert data_directory.is_dir(), "Data directory not directory?"

        self.data_root_directory = data_directory

    def build(self) :
        debug_message("[StockedUpApp] build fired")

        self.scroll_bar_colour = [0.2, 0.7, 0.9, .5]
        self.scroll_bar_inactive_colour = [0.2, 0.7, 0.9, .5]
        self.scroll_bar_width = mm(4)
        self.fixed_button_height = mm(8)

        self.account_viewer_fixed_size = (3 * self.fixed_button_height + mm(6))
        self.account_viewer_fixed_row_height = 12

        screen_manager = StockedUpAppManager(transition=WipeTransition())
        screen_manager.data_root_directory = self.data_root_directory
        screen_manager.ledger_configuration = json_read(self.data_root_directory.joinpath("LedgerConfiguration.json"))

        screen_manager.swap_screen("LedgerSetup")
        return screen_manager


""" FileCatalogFactory class to create file catalog client objects according to the
    configuration description
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from DIRAC import gLogger, gConfig, S_OK, S_ERROR
from DIRAC.ConfigurationSystem.Client.Helpers.Resources import getCatalogPath
from DIRAC.Resources.Catalog.FileCatalogProxyClient import FileCatalogProxyClient
from DIRAC.Core.Utilities.ObjectLoader import ObjectLoader

__RCSID__ = "$Id$"

class FileCatalogFactory(object):
  """ Factory of file catalog objects. Only exposes createCatalog() method
  """

  def __init__(self):
    self.log = gLogger.getSubLogger('FileCatalogFactory')
    self.catalogPath = ''

  def createCatalog(self, catalogName, useProxy=False):
    """ Create a file catalog object from its name and CS description
    """
    catalogPath = getCatalogPath(catalogName)
    catalogType = gConfig.getValue(catalogPath + '/CatalogType', catalogName)
    catalogURL = gConfig.getValue(catalogPath + '/CatalogURL', "DataManagement/" + catalogType)
    optionsDict = {}
    result = gConfig.getOptionsDict(catalogPath)
    if result['OK']:
      optionsDict = result['Value']

    if useProxy:
      result = self.__getCatalogClass(catalogType)
      if not result['OK']:
        return result
      catalogClass = result['Value']
      methods = catalogClass.getInterfaceMethods()
      catalog = FileCatalogProxyClient(catalogName)
      catalog.setInterfaceMethods(methods)
      return S_OK(catalog)

    return self.__createCatalog(catalogName, catalogType, catalogURL, optionsDict)

  def __getCatalogClass(self, catalogType):
    result = ObjectLoader().loadObject('Resources.Catalog.%sClient' % catalogType)
    if not result['OK']:
      gLogger.error('Failed to load catalog object', '%s' % result['Message'])
    return result

  def __createCatalog(self, catalogName, catalogType, catalogURL, optionsDict):

    self.log.debug('Creating %s client of type %s' % (catalogName, catalogType))

    result = self.__getCatalogClass(catalogType)
    if not result['OK']:
      return result
    catalogClass = result['Value']

    try:
      optionsDict['url'] = catalogURL
      catalog = catalogClass(**optionsDict)
      self.log.debug('Loaded module %sClient' % catalogType)
      return S_OK(catalog)
    except Exception as x:
      errStr = "Failed to instantiate %s()" % (catalogType)
      gLogger.exception(errStr, lException=x)
      return S_ERROR(errStr)

    # Catalog module was not loaded
    return S_ERROR('No suitable client found for %s' % catalogName)


from django.shortcuts import render, redirect
from django.http import HttpResponse
from datetime import datetime

class Chat:
    def __init__(self):
        self.messages = []
    def add(self,message):
        self.messages.append(message)
    def get_last_100_messages(self):
        self.messages.sort(key=lambda x: x.timestamp, reverse=True)
        return self.messages[:99]
    def get_messages_of_page(self, n):
        self.messages.sort(key=lambda x: x.timestamp, reverse=True)
        return self.messages[5*n:5*(n+1)]

class Message:
    def __init__(self, body, author):
        self.body = body
        self.timestamp = datetime.now()
        self.author = author
chat = Chat()

# for i in range(120):
#     message1 = Message("blablabalbal", "pancho{}".format(i))
#     chat.add(message1)

# Create your views here.
def index(request):
    if request.method == "GET":
        if not request.session.get("alias"):
            # No Alias
            return render(request, "input_alias.html")
        else:
            return redirect('send_message')
    elif request.method == "POST":
        print(request.POST.get("alias",""))
        alias = request.POST.get("alias","")
        request.session["alias"] = alias
        return redirect('send_message')

def send_message(request):
    alias = request.session.get("alias")
    if not alias:
        return redirect("") # Back to home
    else:
        if request.method == "GET":
            data = {"alias": alias}
            return render(request, "send_message.html",data)

        elif request.method == "POST":
            body = request.POST.get("body","")
            author = alias
            message = Message(body, author)
            chat.add(message)
            return redirect("messages",0)

def messages(request, page):
    alias = request.session.get("alias")
    if not alias:
        return redirect("") # Back to home
    else:
        if request.method == "GET":
            if page < min(19,(len(chat.get_last_100_messages())-1)//5):
                next_page_num = page + 1
            else:
                next_page_num = min(19,(len(chat.get_last_100_messages())-1)//5)
            if page <= 0:
                prev_page_num = 0
            else:
                prev_page_num = page - 1

            data = {"alias": alias,
                    "messages": chat.get_messages_of_page(page),
                    "next_page_num": next_page_num,
                    "prev_page_num": prev_page_num,
                    }
                    ### Meter current page
            return render(request, "messages.html",data)


import streamlit as st
import pickle

# Load the pickle file containing the trained model
with open('rain_prediction_model.pkl', 'rb') as file:
    model = pickle.load(file)

# Define the streamlit app
if __name__ == '__main__':
    st.title('Rain Prediction App')

    # Create an input form for user inputs
    st.header('Enter Input Features')
    st.sidebar.header('Country Labels')
    st.sidebar.text('''London : 0
Paris : 1
New York : 2
Tokyo : 3
Beijing : 4
Istanbul : 5
Moscow : 6
Berlin : 7
Bangkok : 8
Seoul : 9
Mumbai : 10
Rome : 11
Madrid : 12
Mexico City : 13
Shanghai : 14
Rio de Janeiro : 15
Los Angeles : 16
Buenos Aires : 17
Sydney : 18
Johannesburg : 19''')
    options = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
    Location = st.selectbox('Select a Location', options)
    Min_Temperature = st.slider('Min Temperature today (C)', -20.00, 40.00, 0.00)
    Max_Temperature = st.slider('Max Temperature today (C)', -20.00, 40.00, 0.00)
    Wind_Direction = st.slider('Wind Direction in Degrees (°) --> 0° - blowing from the north, 90° - blowing from the east, 180° - blowing from the south, 270° - blowing from the west', 0, 360, 180)
    Wind_Speed = st.slider('Wind Speed (m/s)', 0.00, 13.00, 0.00)
    Humidity = st.slider('Humidity (%)', 7, 100, 50)
    Pressure = st.slider('Pressure (hPa)', 1000, 1036, 1000)
    Cloud = st.slider('Cloud (%)', 0, 100, 50)
    Temperature = st.slider('Present Temperature (C)', -20.00, 40.00, 0.00)
    opt = [0,1]
    Rain = st.selectbox('Is it raining today (0 - No, 1 - Yes)', opt)

    # Use the loaded model to make predictions
    features = [[Location, Min_Temperature, Max_Temperature, Wind_Direction, Wind_Speed, Humidity, Pressure, Cloud, Temperature, Rain]]
    prediction = model.predict(features)

    # Display the prediction results to the user
    st.write('Prediction : ', prediction[0])
    if (prediction[0]==1):
        st.header("Yes, it'll rain tomorrow")
    else:
        st.header("No, it'll not rain tomorrow")


# -*- coding: utf-8 -*-
"""
Created on Mon Mar  9 10:05:26 2020

@author: Seta
"""

from collections import defaultdict
import os
import sys
import random
sys.path.append(os.getcwd()+'\\v0_5')

import pandapower as pp
from pandapower import timeseries as ts
from pandapower import control
import pandas as pd
import numpy as np
import time

from v0_5.Prosumer import Prosumer
from v0_5.centralcpu import CPU
from Storage import BatterySimple, BatterySimple
from PVgen import PVgen
from utils.function_repo import parse_hours, timegrid

# ============================================================================
# Create NETWORK

def simple_net():
    net = pp.create_empty_network()
    # Create buses
    pp.create_bus(net, name='Bus ext grid', vn_kv=10., type='b')
    pp.create_bus(net, name='Bus LV0', vn_kv=0.4, type='n')
    for i in range(1, 6):
        pp.create_bus(net, name='Bus LV1.%s' % i, vn_kv=0.4, type='m')
    for i in range(1, 5):
        pp.create_bus(net, name='Bus LV2.%s' % i, vn_kv=0.4, type='m')
    pp.create_bus(net, name='Bus LV2.2.1', vn_kv=0.4, type='m')
    pp.create_bus(net, name='Bus LV2.2.2', vn_kv=0.4, type='m')
    # Create lines
    for i in range(1,6):
        pp.create_line(net, from_bus=i, to_bus=i+1, length_km=0.08, std_type='NAYY 4x120 SE')
    pp.create_line(net, from_bus=1, to_bus=7, length_km=0.12, std_type='NAYY 4x120 SE')
    pp.create_line(net, from_bus=7, to_bus=8, length_km=0.12, std_type='NAYY 4x120 SE')
    for i in range(8, 10):
        pp.create_line(net, from_bus=i, to_bus=i+1, length_km=0.12, std_type='15-AL1/3-ST1A 0.4')
    pp.create_line(net, from_bus=8, to_bus=11, length_km=0.12, std_type='15-AL1/3-ST1A 0.4')
    pp.create_line(net, from_bus=11, to_bus=12, length_km=0.12, std_type='15-AL1/3-ST1A 0.4')
    # Create external grid
    pp.create_ext_grid(net, bus=0, m_pu=1.03, va_degree=0, name='External grid',
                       s_sc_max_mva=10, rx_max=0.1, rx_min=0.1)
    # Create transformer
    pp.create_transformer_from_parameters(net, hv_bus=0, lv_bus=1, sn_mva=.4,
                                          vn_hv_kv=10, vn_lv_kv=0.4, vkr_percent=1.325,
                                          vk_percent=4, pfe_kw=0.95, i0_percent=0.2375,
                                          tap_side="hv", tap_neutral=0, tap_min=-2,
                                          tap_max=2, tap_step_percent=2.5, tp_pos=0,
                                          shift_degree=150, name='MV-LV-Trafo')
    for ind in range(1,13):
        pp.create_load(net, bus=ind,
                        p_mw=0.0035,
                        name='Prosumer %s' % ind)
    return net

def import_data():
    irr = pd.read_csv(
                      filepath_or_buffer = 'data/1minIntSolrad-07-2006.csv',
                      sep                = ';',
                      skiprows           = 25,
                      parse_dates        = [[0,1]],
                      index_col          = 0,
                      )
    # Import load_profile test data
    load_data = pd.read_csv(
                            filepath_or_buffer = 'data/1MinIntSumProfiles-Apparent-2workingpeople.csv',
                            sep                = ';',
                            usecols            = [1,2],
                            parse_dates        = [1],
                            index_col          = 0,
                            )
    # Convert 0..24:00 hours to 0..23:59
    parse_hours(irr)
    load_data.index = pd.to_datetime(load_data.index, dayfirst=True) + pd.Timedelta(minutes=1)
    load_demand     = load_data.iloc[:, 0]
    irrad_data      = irr.iloc[:, 3]
    irrad_data.index= pd.to_datetime(irrad_data.index) + pd.DateOffset(years=10)
    if any(',' in string for string in load_demand):
        load_demand = load_demand.str.replace(',', '.')
        load_demand = 30*pd.to_numeric(load_demand)

    return irrad_data, load_demand

def neighborhood(net):

    _, load_demand = import_data()
    neighborhood = {}
    for b in net.bus.index[1:]:

        # Randomly generate a variation of a prosumer pv peak within +/- 30 %
        ft = random.randint(1,30)/100
        ld = load_demand*(1-ft)
        # Install a PV power around the magnitude of the peak demand of Prosumer X
        pk = np.max(ld)
        # stantiate a Prosumer X
        pvgen   = PVgen(installed_pv = pk*0.7)
        battery = BatterySimple(battery_capacity = 3.5,
                                initial_SOC = 60,
                                min_max_SOC = (20,80))
        p = Prosumer(pvgen = pvgen, battery=battery)
        # Store Prosumer X in Neighborhood dictionary
        neighborhood['%s' % net.bus.name[b]] = p

    return neighborhood

# Initialize results storage
def create_output_writer(net, time_steps, output_dir):
    ow = ts.OutputWriter(net, time_steps, output_path=output_dir,
                         output_file_type=".xls", log_variables=list())
    # these variables are saved to the harddisk after / during the time series loop
    ow.log_variable('res_ext_grid', 'p_mw')
    # ow.log_variable('res_load', 'p_mw')
    ow.log_variable('res_bus', 'vm_pu')
    ow.log_variable('res_line', 'loading_percent')
    # ow.log_variable('res_line', 'i_ka')
    return ow

# ============================================================================
# RUN example
# Load data
irr, load = import_data()
# Extract timestep size
timestep = timegrid(load)
# create network
net = simple_net()
# create neighborhood
nh = neighborhood(net)
# create central CPU that monitors and commands prosumers
cpu = CPU()

now=time.time()
t           = []
th_overload = pd.DataFrame()
vm_pu       = pd.DataFrame()
slack_p     = pd.DataFrame()
# Run stepwise simulation extracting load and irradiation
for i, (ir, ld) in enumerate(zip(irr[:1230], load[:1230]*10)):
    # Instantiate a controller unit for each Prosumer's load
    for j, (key, val) in enumerate(nh.items()):
        d = pd.DataFrame(index=[0])
        # Randomly generate a variation of a prosumer load within +/- 30 %
        ld = ld*(1-random.randint(1,30)/1000)
        val.run_pflow(ir, ld, timestep, timestamp=irr[:1440].index[i])
        net.load.at[j, "p_mw"] = -val.recorder.meta['p_grid_flow'][-1]/1000
        # d[key] = -val.recorder.meta['p_grid_flow'][-1]/1000
        # ds = ts.DFData(d)
        # control.ConstControl(net, element='load',
        #                       element_index=[j],
        #                       variable='p_mw',  data_source=ds,
        #                       profile_name=[key])
        # print("net_load",net.load.loc[j,"p_mw"], "pros_SOC", val.recorder.meta['battery_SOC'][-1])
    # Run power flow calculation at every timestep iteration
    # ow = create_output_writer(net,[], "E:/Temp")
    # ts.run_timeseries(net, verbose=False)
    pp.runpp(net)
    cpu.control_prosumers(net, nh, bypass_control=False)
    # Store line overload, voltage at buses and slack power balance
    # res['Time'].append(irr.index[i])
    # res['load'].append(net.load.p_mw.tolist())
    # res['th_overload'].append(net.res_line.loading_percent.tolist())
    # res['vm_pu_bus'].append(net.res_bus.vm_pu.tolist())
    # res['slack_p'].append(net.res_ext_grid.p_mw.tolist())
    # print('Time since beginning of simulation: ', time.time() - now)
    t.append(irr.index[i])
    th_overload = th_overload.append(net.res_line.loading_percent.transpose(), ignore_index=True)
    vm_pu = vm_pu.append(net.res_bus.vm_pu.transpose(), ignore_index=True)
    slack_p = slack_p.append(net.res_ext_grid.p_mw, ignore_index=True)
    # print('Time since beginning of simulation: ', time.time() - now)
th_overload.index = t
vm_pu.index = t
slack_p.index = t
# results = pd.DataFrame(res)


#!/usr/bin/env python3

import json
import pandas as pd
import numpy as np
import re
import os
import argparse

import subprocess

RESOURCES_FILE = "data/HOAResources.csv"
CROPLAND_FILE = "data/HOACropland.csv"
CYCLES_RUN_SCRIPT = "bin/cycles/run"
TMP_DIR = "tmp"
OUTPUT_FILE = "outputs/cycles_results.csv"
CROPS_FILE = "data/crops-horn-of-africa.crop"
SOIL_WEATHER_DIR = "data/soil_weather"

def run_cycles(params):
    crops = [
        'Maize',
        'Sorghum',
        'Cassava',
        'Millet',
        'SpringLentil',
        'Potato',
    ]

    cropland_df = pd.read_csv(CROPLAND_FILE)
    cropland_df.set_index(['country', 'admin1', 'admin2', 'admin3'], inplace=True)

    df = pd.read_csv(RESOURCES_FILE)
    df.set_index(['country', 'admin1', 'admin2', 'admin3'], inplace=True)
    country_soil_points = df.loc[params["country"]]

    # Run Cycles for all soil points in the country
    # - TODO: Should run in parallel
    first = True
    for index, point in country_soil_points.iterrows():

        # Get the crop fractional area for this region
        cropland_row = cropland_df.loc[params["country"], index[0], index[1], index[2]]

        crop_pd = cropland_row[params["crop_name"].lower()+"_pd"]
        crop_grain_yield = cropland_row[params["crop_name"].lower()+"_grain_yield"]
        crop_fractional_area = cropland_row[params["crop_name"].lower()+"_fractional_area"]

        # Calculate planting date
        planting_day = int(crop_pd) + int(params["start_planting_day"])
        planting_day = planting_day - 365 if planting_day > 365 else planting_day
        planting_day = planting_day + 365 if planting_day <= 0 else planting_day

        # Calculate reference and current food production
        reference_production = 0.0
        production = 0.0
        for c in crops:
            reference_production += cropland_row[c.lower()+"_grain_yield"] * cropland_row[c.lower()+"_fractional_area"]

            if c != params["crop_name"]:
                production += cropland_row[c.lower()+"_grain_yield"] * cropland_row[c.lower()+"_fractional_area"]

        # Get the input/output files
        inputfile = point["filename"]
        season_file = f"{TMP_DIR}/{inputfile}.season"
        summary_file = f"{TMP_DIR}/{inputfile}.summary"

        # ** Run Cycles **
        cmd = [CYCLES_RUN_SCRIPT,
               '-i1', f"{SOIL_WEATHER_DIR}/{inputfile}",
               '-i2', CROPS_FILE,
               '-o1', season_file,
               '-o2', summary_file,
               '-p1', params["start_year"],
               '-p2', params["end_year"],
               '-p3', params["crop_name"],
               '-p4', str(planting_day),
               '-p5', params["fertilizer_rate"],
               '-p6', params["weed_fraction"],
               '-p7', "False"
              ]
        print(cmd)
        try:
              output = subprocess.run(cmd)
        except subprocess.CalledProcessError as exc:
              print("Status : FAIL", exc.returncode, exc.output)
              continue

        # Load the output file
        exdf = get_dataframe_for_execution_result(season_file, index, params,
                                                ["grain_yield", "cum._n_stress", "actual_tr", "potential_tr"])

        # Filter/Modify/Add Columns
        exdf["crop_production"] = exdf["grain_yield"]*crop_fractional_area
        exdf["water_stress"] = 1 - exdf["actual_tr"]/exdf["potential_tr"]
        exdf["relative_yield"] = exdf["grain_yield"] / crop_grain_yield
        exdf["planting_day"] = planting_day
        exdf["impact"] = (production + exdf["crop_production"]) / reference_production - 1.0

        exdf = exdf.rename(columns={'cum._n_stress': 'nitrogen_stress'})
        exdf = exdf.drop(['actual_tr', 'potential_tr'], axis = 1)

        # Write output
        if first:
            exdf.to_csv(OUTPUT_FILE, index=False)
            first = False
        else:
            exdf.to_csv(OUTPUT_FILE, mode='a', header=False, index=False)

def load_execution_result(outputloc):
    df = pd.read_csv(outputloc, sep='\t', header=0, skiprows=[1], skipinitialspace=True)
    df = df.rename(columns=lambda x: x.strip().lower().replace(' ', '_'))
    df['date'] = pd.to_datetime(df['date'], format="%Y-%m-%d")
    return df

def get_dataframe_for_execution_result(season_file, index, params, result_fields):
    df = load_execution_result(season_file)

    # Fetch feature values
    ndf = df.filter(result_fields, axis=1)

    # Insert timestamp
    ndf.insert(0, "date", df['date']) #.values.astype(np.int64) // 10 ** 9)

    # Insert geospatial details
    ndf.insert(1, "country", params["country"])
    ndf.insert(2, "admin1", index[0] if index[0] else None)
    ndf.insert(3, "admin2", index[1] if index[1] else None)
    ndf.insert(4, "admin3", index[2] if index[2] else None)

    return ndf

def _main():
    parser = argparse.ArgumentParser(
        description="Cycles execution for a country"
    )
    parser.add_argument("--country", dest="country", default="Kenya", help="Country name")
    parser.add_argument("--crop-name", dest="crop_name", default="Maize", help="Crop name")
    parser.add_argument("--start-year", dest="start_year", default="2000", help="Simulation start year")
    parser.add_argument("--end-year", dest="end_year", default="2020", help="Simulation end year")
    parser.add_argument("--start-planting-day", dest="start_planting_day", default="103", help="Start planting date")
    parser.add_argument("--fertilizer-rate", dest="fertilizer_rate", default="50.00", help="Fertilizer rate")
    parser.add_argument("--weed-fraction", dest="weed_fraction", default="0.2", help="Weed fraction")
    args = parser.parse_args()
    run_cycles(vars(args))

if __name__ == "__main__":
    _main()


# coding: utf-8

from typing import Dict

import pytest
from httpx import AsyncClient
from src.models.get_level_list_response import GetLevelListResponse  # noqa: F401
from src.models.get_level_response import GetLevelResponse  # noqa: F401
from src.models.level import Level  # noqa: F401

@pytest.mark.asyncio
async def test_add_level(client: AsyncClient) -> None:
    """Test case for add_level

    Add a level
    """
    level = {
        "title": "title",
        "titleEn": "titleEn",
        "author": "author",
        "authorEn": "authorEn",
        "artists": "artists",
        "artistsEn": "artistsEn",
        "description": "No description",
        "descriptionEn": "No description",
        "genre": "general",
        "engine": "a",
        "background": None,
        "skin": None,
        "particle": None,
        "effect": None,
        "cover": "before_hash",
        "bgm": "hash",
        "data": "sus_hash",
        "preview": "hash",
        "length": "0",
        "bpm": 555,
        "notes": 2000,
        "rating": 8,
    }

    headers = {
        "Authorization": "Bearer admin",
    }
    response = await client.request(
        "POST",
        "/levels",
        headers=headers,
        json=level,
    )

    assert response.status_code == 200

@pytest.mark.asyncio
async def test_delete_level(client: AsyncClient) -> None:
    """Test case for delete_level

    Delete a level
    """

    headers = {
        "Authorization": "Bearer admin",
    }
    response = await client.request(
        "DELETE",
        "/levels/{levelName}".format(levelName="botorushippunotabi"),
        headers=headers,
    )

    assert response.status_code == 200

@pytest.mark.asyncio
async def test_edit_level(client: AsyncClient) -> None:
    """Test case for edit_level

    Edit a level
    """
    level = {
        "title": "b",
        "titleEn": "b",
    }

    headers = {
        "Authorization": "Bearer admin",
    }
    response = await client.request(
        "PATCH",
        "/levels/{levelName}".format(levelName="botorushippunotabi"),
        headers=headers,
        json=level,
    )

    assert response.status_code == 200

@pytest.mark.asyncio
async def test_get_level(client: AsyncClient) -> None:
    """Test case for get_level

    Get a level
    """

    headers: Dict[str, str] = {}
    response = await client.request(
        "GET",
        "/levels/{levelName}".format(levelName="botorushippunotabi"),
        headers=headers,
    )

    assert response.status_code == 200

@pytest.mark.asyncio
async def test_get_level_list(client: AsyncClient) -> None:
    """Test case for get_level_list

    Get level list
    """
    params: Dict[str, str] = {
        "localization": "en",
        "page": "0",
        "keywords": "Chino",
        "sort": "0",
        "order": "0",
        "status": "0",
        "author": "any",
        "random": "0",
    }
    headers: Dict[str, str] = {}
    response = await client.request(
        "GET",
        "/levels/list",
        headers=headers,
        params=params,
    )

    assert response.status_code == 200


import pandas as pd

import matplotlib.pyplot as plt

def load(source):
    csv = pd.read_csv(source)
    return csv

iris_csv = load("iris.csv")

print(iris_csv)

# primero

iris_csv.plot()

# segundo

iris_csv["SepalLengthCm"].plot()

# tercero

iris_csv.plot.scatter(x="SepalLengthCm", y="PetalLengthCm", alpha=0.5)

# cuarto

iris_csv.plot.box()

# Quinto

iris_csv.plot.area(figsize=(12, 4), subplots=True)

fig, axs = plt.subplots(figsize=(12, 4))
iris_csv.plot.area(ax=axs)
axs.set_ylabel("Concentración de tamaños")
fig.savefig("concentracion.png")
plt.show()


import unittest
from hydipy.Discrete import DiscreteNode
import pyAgrum as gum
import numpy as np

class Test_DiscreteNOde(unittest.TestCase):
    def test_no_parents(self):
        self.discnode = DiscreteNode(
            "a", [[0.7], [0.2], [0.1]], parents=[], states=["a1", "a2", "a3"]
        )
        self.assertEqual("a", self.discnode.id)
        self.assertIsNone(
            np.testing.assert_array_equal(
                np.array([[0.7], [0.2], [0.1]]), self.discnode.values
            )
        )
        self.assertListEqual([], self.discnode.parents)
        self.assertListEqual(["a1", "a2", "a3"], self.discnode.states)
        self.assertEqual(3, self.discnode.cardinality)
        self.assertIsNone(
            np.testing.assert_array_equal(
                np.array([0.7, 0.2, 0.1]), self.discnode.agrum_cpd()
            )
        )
        bn = gum.BayesNet("test_bn")
        var = self.discnode.agrum_var()
        bn.add(var)
        bn.cpt("a").fillWith(self.discnode.agrum_cpd())
        cpt = bn.cpt("a")
        self.assertTupleEqual(cpt.names, tuple("a"))
        self.assertListEqual(cpt.tolist(), [0.7, 0.2, 0.1])

    def test_one_parent(self):
        self.a = DiscreteNode(
            "a", [[0.7], [0.2], [0.1]], parents=[], states=["a1", "a2", "a3"]
        )
        self.b = DiscreteNode(
            "b", [[0.4, 0.2, 0.9], [0.6, 0.8, 0.1]], parents=["a"], states=["b1", "b2"]
        )
        self.assertEqual("b", self.b.id)
        self.assertIsNone(
            np.testing.assert_array_equal(
                np.array([[0.4, 0.2, 0.9], [0.6, 0.8, 0.1]]), self.b.values
            )
        )
        self.assertListEqual(["a"], self.b.parents)
        self.assertListEqual(["b1", "b2"], self.b.states)
        self.assertEqual(2, self.b.cardinality)
        self.assertListEqual([], self.a.agrum_edges())
        self.assertListEqual([("a", "b")], self.b.agrum_edges())
        self.assertIsNone(
            np.testing.assert_array_equal(np.array([0.7, 0.2, 0.1]), self.a.agrum_cpd())
        )
        self.assertIsNone(
            np.testing.assert_array_equal(
                np.array([0.4, 0.6, 0.2, 0.8, 0.9, 0.1]), self.b.agrum_cpd()
            )
        )
        bn = gum.BayesNet("test_bn")
        var_a = self.a.agrum_var()
        var_b = self.b.agrum_var()
        bn.add(var_a)
        bn.add(var_b)
        bn.addArcs(self.b.agrum_edges())
        bn.cpt("a").fillWith(self.a.agrum_cpd())
        bn.cpt("b").fillWith(self.b.agrum_cpd())
        self.assertListEqual(bn.cpt("a").tolist(), [0.7, 0.2, 0.1])
        self.assertListEqual(bn.cpt("b").tolist(), [[0.4, 0.6], [0.2, 0.8], [0.9, 0.1]])

    def test_two_parents(self):
        self.a = DiscreteNode(
            "a", [[0.7], [0.2], [0.1]], parents=[], states=["a1", "a2", "a3"]
        )
        self.b = DiscreteNode("b", [[0.2], [0.8]], parents=[], states=["b1", "b2"])
        self.c = DiscreteNode(
            "c",
            [[0.4, 0.8, 0.2, 0.9, 0.25, 0.99], [0.6, 0.2, 0.8, 0.1, 0.75, 0.01]],
            parents=["a", "b"],
            states=["c1", "c2"],
        )
        self.assertListEqual([("b", "c"), ("a", "c")], self.c.agrum_edges())
        self.assertIsNone(
            np.testing.assert_array_equal(np.array([0.7, 0.2, 0.1]), self.a.agrum_cpd())
        )
        self.assertIsNone(
            np.testing.assert_array_equal(np.array([0.2, 0.8]), self.b.agrum_cpd())
        )
        self.assertIsNone(
            np.testing.assert_array_equal(
                np.array(
                    [0.4, 0.6, 0.8, 0.2, 0.2, 0.8, 0.9, 0.1, 0.25, 0.75, 0.99, 0.01]
                ),
                self.c.agrum_cpd(),
            )
        )
        bn = gum.BayesNet("test_bn")
        var_a = self.a.agrum_var()
        var_b = self.b.agrum_var()
        var_c = self.c.agrum_var()
        bn.add(var_a)
        bn.add(var_b)
        bn.add(var_c)
        bn.addArcs(self.a.agrum_edges())
        bn.addArcs(self.b.agrum_edges())
        bn.addArcs(self.c.agrum_edges())
        bn.cpt("a").fillWith(self.a.agrum_cpd())
        bn.cpt("b").fillWith(self.b.agrum_cpd())
        bn.cpt("c").fillWith(self.c.agrum_cpd())
        cpt_c = bn.cpt("c")
        self.assertTupleEqual(cpt_c.names, ("c", "b", "a"))
        self.assertListEqual(
            cpt_c.tolist(),
            [
                [[0.4, 0.6], [0.8, 0.2]],
                [[0.2, 0.8], [0.9, 0.1]],
                [[0.25, 0.75], [0.99, 0.01]],
            ],
        )
        self.assertEqual(
            str(cpt_c),
            "\n             ||  c                |\nb     |a     ||c1       |c2       |\n------|------||---------|---------|\nb1    |a1    || 0.4000  | 0.6000  |\nb2    |a1    || 0.8000  | 0.2000  |\nb1    |a2    || 0.2000  | 0.8000  |\nb2    |a2    || 0.9000  | 0.1000  |\nb1    |a3    || 0.2500  | 0.7500  |\nb2    |a3    || 0.9900  | 0.0100  |\n",
        )


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import os
import json
import io
import typing
import copy

from typing import Any, List, Text

from rasa_core import utils
from rasa_core.policies.policy import Policy
from rasa_core.constants import FALLBACK_SCORE
from rasa_core.events import (
    UserUttered, ActionExecuted,
    Event, SlotSet, Restarted, ActionReverted, UserUtteranceReverted,
    BotUttered)

logger = logging.getLogger(__name__)

if typing.TYPE_CHECKING:
    from rasa_core.domain import Domain
    from rasa_core.trackers import DialogueStateTracker

class ConfirmationPolicy(Policy):
    """Policy which executes a confirmation action if NLU confidence is low
       Should be put before other prediction policies.
        :param float nlu_threshold:
          minimum threshold for NLU confidence.
          If intent prediction confidence is lower than this,
          predict confirmation action with confidence 1.0.

        :param Text confirmation_action_name:
          name of the action to execute as a confirmation.
    """

    @staticmethod
    def _standard_featurizer():
        return None

    def __init__(self,
                 nlu_threshold=0.1,  # type: float
                 confirmation_action_name="action_default_confirmation", # type: Text
                 confirmation_rephrase_name="action_default_rephrase",  # type: Text
                 affirm_intent_name="affirm",  # type: Text
                 deny_intent_name="deny"  # type: Text
                 ):
        # type: (...) -> None

        super(ConfirmationPolicy, self).__init__()

        self.nlu_threshold = nlu_threshold
        self.confirmation_rephrase_name = confirmation_rephrase_name
        self.confirmation_action_name = confirmation_action_name
        self.affirm_intent_name = affirm_intent_name
        self.deny_intent_name = deny_intent_name

        self.cache_message = UserUttered(None)
    def train(self,
              training_trackers,  # type: List[DialogueStateTracker]
              domain,  # type: Domain
              **kwargs  # type: Any
              ):
        # type: (...) -> None
        """Does nothing. This policy is deterministic."""

        pass

    def should_confirm(self,
                        nlu_confidence,  # type float
                        last_action_name  # type: Text
                        ):
        # type: (...) -> bool
        """It should predict confirmation action only if
        a. predicted NLU confidence is lower than ``nlu_threshold`` &&
        b. last action is NOT confirmation action
        """
        return (nlu_confidence < self.nlu_threshold and
                last_action_name != self.confirmation_action_name)

    def confirmation_scores(self, domain, confirmation_score=FALLBACK_SCORE):
        """Prediction scores used if a confirmation is necessary."""

        result = [0.0] * domain.num_actions
        idx = domain.index_for_action(self.confirmation_action_name)
        result[idx] = confirmation_score
        return result

    def predict_action_probabilities(self, tracker, domain):
        # type: (DialogueStateTracker, Domain) -> List[float]
        """Predicts a confirmation action if NLU confidence is low
        """

        nlu_data = tracker.latest_message.parse_data

        # if NLU interpreter does not provide confidence score,
        # it is set to 1.0 here in order
        # to not override standard behaviour
        nlu_confidence = nlu_data["intent"].get("confidence", 1.0)
        '''
        print("\033[92m Events\033[0m")
        for idx, event in enumerate(tracker.events):
            print(str(idx) + '. ' + str(event))
        print("\033[92m Applied Events\033[0m")
        for idx, event in enumerate(tracker.applied_events()):
            print(str(idx) + '. ' + str(event))
        print("\033[33mLatest action:{}\033[0m".format(tracker.latest_action_name))
        print("\033[33mLatest intent:{}\033[0m".format(nlu_data["intent"]["name"]))
        '''
        # know what bot just did before 'action_listen'
        action_history = [evt.action_name for evt in tracker.events if isinstance(evt, ActionExecuted)]
        proceeding_action = action_history[-2] if len(action_history) > 2 else 'action_listen'

        if proceeding_action == self.confirmation_action_name:
            # Last action is confirmation question
            if nlu_data["intent"]["name"] == self.affirm_intent_name:
                # TODO: The user affirms the intent in confirmation, thus,
                # predict next action based on previous intent
                # Revert current `confirm (action) <- affirm (intent)`
                # And retrieve cached message for other policy to predict

                from rasa_core.events import UserUtteranceReverted
                tracker.update(UserUtteranceReverted())
                tracker.update(ActionExecuted('action_listen'))
                tracker.update(self.cache_message)
                tracker.latest_message = self.cache_message
                result = [0.0] * domain.num_actions

            elif nlu_data["intent"]["name"] == self.deny_intent_name:
                # TODO: The user denies the intent in confirmation, thus,
                # predict next action as ask for rephrasing
                # Revert current `confirm (action) <- deny (intent)`.
                from rasa_core.events import UserUtteranceReverted
                result = [0.0] * domain.num_actions
                idx = domain.index_for_action(self.confirmation_rephrase_name)
                result[idx] = FALLBACK_SCORE

            else:
                # TODO: The user gives a rephrase, thus,
                # predict next action based on current intent
                # Just return all 0 and let prediction policy do.
                result = [0.0] * domain.num_actions

        elif proceeding_action == self.confirmation_rephrase_name:
            # TODO: The user shall give another intent after bot ask for rephrase.
            # So predict based on new intent.
            result = [0.0] * domain.num_actions

        elif self.should_confirm(nlu_confidence if nlu_confidence else 1, tracker.latest_action_name):
            logger.debug("NLU confidence {} is lower "
                         "than NLU threshold {}. "
                         "Predicting confirmation action: {}"
                         "".format(nlu_confidence, self.nlu_threshold,
                                   self.confirmation_action_name))
            # we set this to 1.1 to make sure confirmation overrides
            # the memoization policy
            # Meanwhile we need to cache the current message for later prediction,
            # then revert input intent and confirmation action.
            self.cache_message = copy.deepcopy(tracker.latest_message)
            self.cache_message.parse_data['intent']['confidence'] = 1.0
            result = self.confirmation_scores(domain)
        elif tracker.latest_action_name == self.confirmation_action_name:
            result = [0.0] * domain.num_actions
            idx = domain.index_for_action('action_listen')
            result[idx] = FALLBACK_SCORE
        else:
            # NLU confidence threshold is met, as well as not in a confirmation process,
            # so predict all 0, let other policies predict.
            result = [0.0] * domain.num_actions
        '''
        print("\033[92m Events\033[0m")
        for idx, event in enumerate(tracker.events):
            print(str(idx) + '. ' + str(event))
        print("\033[92m Applied Events\033[0m")
        for idx, event in enumerate(tracker.applied_events()):
            print(str(idx) + '. ' + str(event))
        '''
        return result

    def persist(self, path):
        # type: (Text) -> None
        """Persists the policy to storage."""
        config_file = os.path.join(path, 'confirmation_policy.json')
        meta = {
            "nlu_threshold": self.nlu_threshold,
            "confirmation_action_name": self.confirmation_action_name
        }
        utils.create_dir_for_file(config_file)
        utils.dump_obj_as_json_to_file(config_file, meta)

    @classmethod
    def load(cls, path):
        # type: (Text) -> ConfirmationPolicy
        meta = {}
        if os.path.exists(path):
            meta_path = os.path.join(path, "confirmation_policy.json")
            if os.path.isfile(meta_path):
                with io.open(meta_path) as f:
                    meta = json.loads(f.read())

        return cls(**meta)


import nltk

import spacy
import random

import numpy as np
import multiprocessing as mp

from collections import defaultdict
from itertools import islice
from operator import itemgetter
from pysem.utils.multiprocessing import plainmap, PoolContainer
from pysem.utils.vsa import convolve, deconvolve, normalize, HRR

tokenizer = nltk.load('tokenizers/punkt/english.pickle')
stopwords = nltk.corpus.stopwords.words('english')
nlp = spacy.load('en')

class RandomIndexing(object):
    """A base class for embedding models that make use of random indexing
    algorithms. These algorithms assign random unit vectors to all of the
    words in a vocabulary to be modelled. Then, operations on these unit
    vectors are performed while scanning through text to build up useful
    representations of the words in question.

    Specific random indexing models are subclasses of this class that specify
    a unique encoding algorithm to be used to train the model. Subclasses also
    specify model-specific methods for retrieving and manipulating learned
    embeddings in useful ways. The methods in the base class that implement
    training etc. make use of these specifications and thus only operate
    as intended when called via an instance of an appropriately defined
    subclass.

    Parameters:
    ----------
    corpus : datahandler object
        A text corpus to model. The provided object must implement a stream
        that yields corpus articles.
    vocab : list
        A list of strings that define the vocabulary of words to be modelled.

    Attributes:
    ----------
    corpus : datahandler object
        The text corpus to model using random indexing.
    vocab : list
        A list of strings that define the vocabulary of words to be modelled.
    vectors : numpy.ndarray
        An array whose rows correspond to the learned embeddings for each
        vocabulary item. Initialized as an array of zeros when the model is
        trained. The index of a word in the vocab is the same as the index of
        the row in the array containing the word's embedding.
    """
    def __init__(self, corpus, vocab):
        self.corpus = corpus
        self.vocab = vocab

    @property
    def vocab(self):
        return self._vocab

    @vocab.setter
    def vocab(self, wordset):
        randitem = random.choice(wordset)
        if isinstance(wordset, list) and isinstance(randitem, str):
            self._vocab = wordset
            self.word_to_idx = {word: idx for idx, word in enumerate(wordset)}
            self.idx_to_word = {idx: word for idx, word in enumerate(wordset)}
        else:
            raise TypeError('The vocabulary must be a list of strings')

    @staticmethod
    def encode():
        raise NotImplementedError('Random indexing models must implement a \
                                   staticmethod for encoding text corpora')

    @staticmethod
    def preprocess(article):
        '''Convert an article into a list of lowercase sentences'''
        sen_list = tokenizer.tokenize(article)
        sen_list = [s.replace('\n', ' ') for s in sen_list]
        sen_list = [s.translate(readonly.strip_num) for s in sen_list]
        sen_list = [s.translate(readonly.strip_pun) for s in sen_list]
        sen_list = [s.lower() for s in sen_list if len(s) > 5]
        sen_list = [nltk.word_tokenize(s) for s in sen_list]
        sen_list = [[w for w in s if w in readonly.vocab] for s in sen_list]
        return sen_list

    def run_pool(self, function, batch):
        '''Apply an embedding function to batch of articles in parallel'''
        with mp.Pool(processes=mp.cpu_count()) as pool:
            results = pool.map_async(function, batch)

            for encoding in results.get():
                for word, vector in encoding.items():
                    self.vectors[self.word_to_idx[word], :] += vector

    def train(self, dim, batchsize=100):
        '''Train a model with embeddings of the specified dimension'''
        self.dim = dim
        self.batchsize = batchsize
        self.vectors = np.zeros((len(self.vocab), dim))
        self._config_readonly()

        for batch in self._batches():
            sents = plainmap(self.preprocess, batch)
            self.run_pool(self.encode, sents)

        self.normalize()

    def normalize(self):
        '''Normalize the embeddings learned by the model to unit length'''
        norms = np.linalg.norm(self.vectors, axis=1)
        norms[norms == 0] = 1  # avoids divide by zero for words w/o embedding
        self.vectors = np.divide(self.vectors, norms[:, np.newaxis])

    def top_matches(self, probe, n, base_vectors=False):
        '''Return the top n embeddings matching a probe embedding'''
        if base_vectors:
            sims = np.dot(readonly.vectors, probe)
        else:
            sims = np.dot(self.vectors, probe)
        ranked = sorted(enumerate(sims), key=itemgetter(1), reverse=True)
        return [(self.idx_to_word[x[0]], x[1]) for x in ranked[:n]]

    def _config_readonly(self):
        '''Declares a PoolContainer as a global variable for shared
        memory used by all processes in a multiprocessing pool.'''
        global readonly
        readonly = PoolContainer(self.dim, self.vocab)

    def _batches(self):
        '''Use of islice requires that self.corpus.articles is a generator.'''
        while True:
            batch = list(islice(self.corpus.articles, self.batchsize))
            yield batch

            if len(batch) < self.batchsize:
                break

        self.corpus.reset_streams()

class ContextEmbedding(RandomIndexing):
    """A random indexing model that produces word embeddings on the basis of
    word co-occurences within sentences.
    """
    @staticmethod
    def encode(sen_list):
        '''Encode context info for each unique word in a list of sentences'''
        encodings = defaultdict(readonly.zeros)
        for sen in sen_list:
            sen_sum = sum([readonly[w].v for w in sen if w not in stopwords])
            for word in sen:
                word_sum = sen_sum - readonly[word].v
                encodings[word] += word_sum
        return encodings

    def get_nearest(self, word, n=5):
        '''Print the n nearest neighbors to a target word in context space'''
        probe = self.vectors[self.word_to_idx[word], :]
        top_n = self.top_matches(probe, n)
        for item in top_n:
            print(item[0], item[1])

class OrderEmbedding(RandomIndexing):
    """A random indexing model that produces word embeddings on the basis of
    positional word co-occurences within sentences.
    """
    @staticmethod
    def encode(sen_list):
        '''Encode order info for each unique word in a list of sentences'''
        encodings = defaultdict(readonly.zeros)
        win = readonly.winsize
        for sen in sen_list:
            for i in range(len(sen)):
                o_sum = HRR(readonly.zeros())
                for j in range(win):
                    if i+j+1 < len(sen):
                        w = readonly[sen[i+j+1]]
                        p = readonly.pos_idx[j]
                        o_sum += w * p
                    if i-j-1 >= 0:
                        w = readonly[sen[i-j-1]]
                        p = readonly.neg_idx[j]
                        o_sum += w * p
                encodings[sen[i]] += o_sum.v
        return encodings

    def get_completions(self, word, position, n=5):
        '''Print the n most likely words to occur in the specified position
        relative to the provided target word in order space'''
        embedding = self.vectors[self.word_to_idx[word], :]
        if position > 0:
            probe = deconvolve(readonly.pos_idx[position-1].v, embedding)
        else:
            probe = deconvolve(readonly.neg_idx[abs(position+1)].v, embedding)

        top_n = self.top_matches(probe, n, base_vectors=True)
        for item in top_n:
            print(item[0], item[1])

    def get_resonants(self, phrase, n=5):
        '''Print the n most likely words to occur in the specified position
        relative to the provided phrase in order space'''
        probe = self.get_vector_encoding(phrase)
        top_n = self.top_matches(probe, n)
        for item in top_n:
            print(item[0], item[1])

    def get_vector_encoding(self, phrase):
        '''Converts a phrase query into a vector encoding the linguistic
        context surrounding a target position'''
        words = phrase.split()
        index = words.index('__')
        probe = np.zeros(self.dim)
        for word in words:
            if word == '__':
                continue
            w = readonly[word].v
            if words.index(word) < index:
                p = readonly.neg_idx[index-words.index(word)-1].v
                probe += convolve(w, p)
            if words.index(word) > index:
                p = readonly.pos_idx[words.index(word)-index-1].v
                probe += convolve(w, p)

        return normalize(probe)

    def _config_readonly(self):
        '''Configures PoolContainer to include vectors that tag different
        positional contexts around a target word'''
        global readonly
        readonly = PoolContainer(self.dim, self.vocab)
        readonly.build_position_tags()

class SyntaxEmbedding(RandomIndexing):
    """A random indexing model that produces word embeddings on the basis of
    syntactic dependency relations within sentences.
    """
    @staticmethod
    def encode(article):
        doc = nlp(article)
        encodings = defaultdict(readonly.zeros)
        targpos = 'VERB'

        for sen in doc.sents:
            for token in sen:
                word = token.lower_
                if word in readonly.vocab and token.pos_ == targpos:
                    for c in token.children:
                        if c.dep_ in readonly.verb_deps:
                            role = readonly.verb_deps[c.dep_]
                            if c.lower_ in readonly.vocab:
                                filler = readonly[c.lower_].v
                                binding = convolve(role, filler)
                                encodings[word] += binding
        return encodings

    def _config_readonly(self):
        '''Configures PoolContainer to include vectors that tag different
        syntactic neighbors of a target word'''
        global readonly
        readonly = PoolContainer(self.dim, self.vocab)
        readonly.build_dependency_tags()

    def train(self, dim, batchsize=100):
        '''Overrides usual RandomIndexing train method to run multiprocessing
        pools on batches of articles instead of batches of sentences'''
        self.dim = dim
        self.batchsize = batchsize
        self.vectors = np.zeros((len(self.vocab), dim))
        self._config_readonly()

        for batch in self._batches():
            self.run_pool(self.encode, batch)

        self.normalize()

    def get_verb_neighbors(self, word, dep, n=5):
        '''Print the n most likely words to occupy the specified syntactic
        dependency relative to the provided target word in syntax space'''
        embedding = self.vectors[self.word_to_idx[word], :]
        probe = deconvolve(readonly.verb_deps[dep], embedding)
        top_n = self.top_matches(probe, n, base_vectors=True)
        for item in top_n:
            print(item[0], item[1])


# -*- coding: utf-8 -*-
"""
Created on Thu Apr 11 12:55:43 2019

@author: nmishra
"""

import os
import datetime
import numpy as np
import h5py

DELIVERABLE_DIR = r'C:\Users\nmishra\Desktop\SAO_deliverable'
DELIVERABLE_FILE_NAME = [each for each in os.listdir(DELIVERABLE_DIR)
                         if each.endswith('.csv')]

#print(*DELIVERABLE_FILE_NAME, sep='\n')

META_DATA = {'Bad Pixel Mask': 'Unitless [1 is bad pixel, O is good pixel]',
             'Cross Talk Factor': 'Scaling Factor (Unitless)',
             'FPA Ref Temp' : 'Temp (Deg C)',
             'FPA gain scaling':'Temp (Deg C) and Scaling Factor (Unitless)',
             'FPA DC Scaling' : 'Scaling Factor (Unitless)',
             'FPA Gain Scaling' : 'Scaling Factor (Unitless)',
             'FPE Ref Temp' : 'Temp (Deg C)',
             'Electronics Gain': 'e-/DN',
             'Non-linearity correcttion': 'DN',
             'Wavelength': 'nm',
             'Radiance Cal. Coeffs':'ph.sec/sec.cm2.nm.sr.DN',
             'PRNU': 'Unitless (Scaling Factor)',
             'Trailing Overclocks': 'DN'
            }

TODAYS_DATE = datetime.datetime.now().strftime('%m_%d_%Y')
HF_NAME = os.path.join(DELIVERABLE_DIR, 'TEMPO_LaRC_Image_Processing_' + TODAYS_DATE + '_V2.h5')
with h5py.File(HF_NAME, 'w') as hf:
    for data_files in DELIVERABLE_FILE_NAME:
        file_header = data_files.strip('.csv')
        deliverable = os.path.join(DELIVERABLE_DIR, data_files)
        data_read = np.genfromtxt(deliverable, delimiter=',')
       # print(data_read[1:3])
        hf.create_dataset(file_header, data=data_read)
        hf.attrs.update(META_DATA)
print('DONE')


#建议用tornado与gevent封装flask，以添加并发能力
from flask_api import FlaskAPI, status, exceptions
from flask import request, url_for
import os

from flask_cas import login_required

import server
import server.flask.ApiServerTornado
from app.app import getAppMgr

try:
    loggerInfo = getAppMgr().get('log')
except:
    import logging
    loggerInfo = logging.getLogger()

app = FlaskAPI(__name__)
getAppMgr().insert('flaskApp',app)

task_queue = None
requestHandMgr = getAppMgr().get('RequestHandlerMgr')

class flaskApi(object):

    @staticmethod
    def init():
        app.config['SESSION_TYPE'] = 'filesystem'
        app.config['SECRET_KEY'] = os.urandom(24)

        getAppMgr().get('sqlAlchemyDB').sqlDB_init()
        getAppMgr().get('mycas').cas_init()
        getAppMgr().get('ApiServerAdmin').init_Admin()

    @staticmethod
    def run(queue, flaskQueue, debug=False):
        flaskApi.init()
        global task_queue
        task_queue = queue

        iPort = flaskQueue.get()
        loggerInfo.info('启动服务器监听，监听端口：%s' % iPort)
        server.flask.ApiServerTornado.setTornado(app, iPort)

def push_queue(data):
    global task_queue
    task_queue.put(data)
getAppMgr().insert('push_queue', push_queue)

desc = getAppMgr().get('desc')
notes = getAppMgr().get('notes')

def note_repr(key):
    return {
        '链接': request.host_url.rstrip('/') + url_for('notes_detail', key=key),
        '说明': notes[key]
    }

#根目录下应该罗列所有接口的列表以及每个接口的说明
@app.route("/", methods=['GET'])
def notes_list():
    #request.method == 'GET'
    return [note_repr(idx) for idx in sorted(notes.keys())]

@app.route("/ai/api/<string:key>", methods=['POST', 'GET'])
@login_required
def notes_detail(key):

    loggerInfo.info("一个请求访问过来,key:" + key + " method:" + str(request.method) + " requst data:" + str(request.data) + "GET:" + str(request.args))
    retData = None
    retData = requestHandMgr.getResponseData(key, request)
    if not retData is None:
        return retData

    # request.method == 'GET'
    if key not in notes.keys():
        return {"Message": "错误的接口", "Code": 0, "Succeed": False,'V':1.0}
    return note_repr(key)


"""module: stonehenge game, with a class of game and a class of gamestate
"""
from typing import Any, List
from game import Game
from game_state import GameState

class StonehengeGame(Game):
    """a class representing a game called Stonehenge, a subclass of Game.

    Cannot write example since class needs input.

    state - the current state of the Stonehenge Game
    side_length - the side length og the Stonehenge grid

    """
    current_state: "StonehengeState"
    side_length: int

    def __init__(self, p1_starts: bool) -> None:
        """
        Initialize StonehengeGame, using p1_starts to find who the first
        player is.
        overrides Game.__init__.
        """
        self.side_length = int(input("Enter the side length of the board:"))
        self.current_state = StonehengeState(p1_starts, self.side_length)

    def get_instructions(self) -> str:
        """
        Return the instructions for Stonehenge Game.
        overrides Game.get_instruction.
        """
        instructions = "Welcome to the Stonehenge Game, you are going to claim"\
                       " the cell in the Stonehenge grid by turns, who claims "\
                       "at least a half ley-lines of the grid will be the "\
                       "winner!"
        return instructions

    def is_over(self, state: "StonehengeState") -> bool:
        """
        Return whether or not StonehengeGame is over at state.
        Overrides Game.is_over.
        """
        counting1 = state.current_line[0] / (3 * (self.side_length + 1))
        counting2 = state.current_line[1] / (3 * (self.side_length + 1))
        return counting2 >= 0.5 or counting1 >= 0.5

    def is_winner(self, player: str) -> bool:
        """
        Return whether player has won StonehengeGame.
        Overrides Game.is_winner.

        Precondition: player is 'p1' or 'p2'.
        """
        if self.is_over(self.current_state):
            if self.current_state.get_current_player_name() != player:
                return True
        return False

    def str_to_move(self, string: str) -> str:
        """
        Return the move that string represents. If string is not a move,
        return some invalid move.
        Overrides Game.str_to_move.
        """
        return str(string)

class StonehengeState(GameState):
    """a class representing a game state of the Stonehenge Game, a subclass
    of GameState.

    grid - the list of how stonehenge grid looks like with each specific
           elements.
    current_line - a list showed the number of two players claimed ley lines.
    """
    current_line: List[int]
    grid: List[str]

    def __init__(self, is_p1_turn: bool, grid_length: int) -> None:
        """
        Initialize StonehengeState state and set the current player based on
        is_p1_turn. Form the initial grid by given grid_length
        extends GameState.__init__
        >>> state = StonehengeState(True, 1)
        >>> state.grid
        [['@', '@'], ['@', 'A', 'B'], ['@', 'C', '@'], ['@']]
        >>> state.p1_turn
        True
        """
        GameState.__init__(self, is_p1_turn)
        self.current_line = [0, 0]
        self.grid = [['@', '@']]
        order = ord('A')
        first_num = 2
        for i in range(grid_length + 1):
            line = ['@']
            num = 0
            if i == grid_length:
                first_num -= 2
            while num < first_num:
                line.append(chr(order + num))
                num += 1
            order += num
            first_num += 1
            if i != grid_length - 1:
                line.append('@')
            self.grid.append(line)
        self.grid += [['@'] * grid_length]

    def __str__(self) -> str:
        """
        Return a string representation of the current state of the game.
        Overrides GameState.__str__
        """

        n = len(self.grid)
        g1 = 3
        g2 = ' - '
        g3 = ' / \\'
        g4 = ' \\ /'
        graph = (2 * n - 2) * ' ' + self.grid[0][0] + g1 * ' ' + \
                self.grid[0][1] + '\n'
        graph += (2 * n - 3) * ' ' + '/' + g1 * ' ' + '/' + '\n'
        for i in range(1, n - 3):
            graph += 2 * (n - 3 - i) * ' '
            for j in range(len(self.grid[i]) - 2):
                graph += self.grid[i][j] + g2
            graph += self.grid[i][len(self.grid[i]) - 2] + g1 * ' ' + \
                     self.grid[i][len(self.grid[i]) - 1] + '\n'
            graph += (2 * (n - 3 - i) + 2) * ' ' + g3 * (i + 1) + ' /' + '\n'
        for j in range(len(self.grid[n - 3]) - 1):
            graph += self.grid[n - 3][j] + g2
        graph += self.grid[n - 3][len(self.grid[n - 3]) - 1] + '\n'
        graph += 4 * ' ' + g4 * (n - 3) + ' \\' '\n' + 2 * ' '
        for j in range(len(self.grid[n - 2]) - 2):
            graph += self.grid[n - 2][j] + g2
        graph += self.grid[n - 2][len(self.grid[n - 2]) - 2] + g1 * ' ' + \
                 self.grid[n - 2][len(self.grid[n - 2]) - 1] + '\n'
        graph += 4 * ' ' + '   \\' * (n - 3) + '\n'
        graph += 5 * ' '
        for j in range(n - 3):
            graph += g1 * ' ' + self.grid[n - 1][j]
        graph += '\n'
        return graph

    def get_possible_moves(self) -> list:
        """
        Return all possible moves that can be applied to this state.
        Overrides GameState.get_possible_moves

        >>> state = StonehengeState(True, 1)
        >>> state.grid
        [['@', '@'], ['@', 'A', 'B'], ['@', 'C', '@'], ['@']]
        >>> state.get_possible_moves()
        ['A', 'B', 'C']
        """
        new_list = []
        counting1 = self.current_line[0] / (3 * (len(self.grid) - 2))
        counting2 = self.current_line[1] / (3 * (len(self.grid) - 2))
        for i in range(1, len(self.grid) - 1):
            for j in range(1, len(self.grid[i])-1):
                if self.grid[i][j].isalpha():
                    new_list.append(self.grid[i][j])
            if i == len(self.grid) - 3:
                if self.grid[i][len(self.grid[i]) - 1].isalpha():
                    new_list.append(self.grid[i][len(self.grid[i]) - 1])
        if counting1 >= 0.5 or counting2 >= 0.5:
            new_list = []
        return new_list

    def make_move(self, move: str) -> 'StonehengeState':
        """
        Return the StonehengeState that results from applying move to
        this StonehengeState.
        Overrides GameState.make_move

        >>> state = StonehengeState(True, 1)
        >>> new_state = state.make_move('A')
        >>> new_state.grid
        [['1', '@'], ['1', '1', 'B'], ['@', 'C', '@'], ['1']]
        >>> new_state.p1_turn
        False
        """
        new_state = StonehengeState(self.p1_turn, len(self.grid) - 2)
        new_state.current_line, new_state.grid = self.current_line[:], [
            [cell for cell in item] for item in self.grid]
        if not self.is_valid_move(move):
            return self
        my_pos, index, new_state.p1_turn, ley_line_dic = '1', 0, False, {}
        if not self.p1_turn:
            my_pos, index, new_state.p1_turn = '2', 1, True
        for i in range(len(new_state.grid)):
            for j in range(len(new_state.grid[i])):
                new_state.grid[
                    i][j] = my_pos if new_state.grid[
                        i][j] == move else new_state.grid[i][j]
        c1, c2, ley_line_dic['+0+0'], ley_line_dic['-2-1'] \
            = 0, 0, [], []
        ley_line_dic['+0+0'].append(new_state.grid[0][0])
        ley_line_dic['-2-1'].append(new_state.grid[-2][-1])
        for i in range(1, len(new_state.grid) - 2):
            ley_line_dic['+0+0'].append(new_state.grid[i][1])
            ley_line_dic['-2-1'].append(new_state.grid[i][i + 1])
            c1 = c1 + 1 if new_state.grid[i][1] == my_pos else c1
            c2 = c2 + 1 if new_state.grid[i][i + 1] == my_pos else c2
        if c1 >= (len(ley_line_dic['+0+0']) - 1) / 2 and new_state.grid[int(
                '+0+0'[0:2])][int('+0+0'[2:])] == '@':
            new_state.grid[int('+0+0'[0:2])][
                int('+0+0'[2:])], new_state.current_line[
                    index] = my_pos, new_state.current_line[index] + 1
        if c2 >= (len(ley_line_dic['-2-1']) - 1) / 2 and new_state.grid[int(
                '-2-1'[0:2])][int('-2-1'[2:])] == '@':
            new_state.grid[int('-2-1'[0:2])][
                int('-2-1'[2:])], new_state.current_line[
                    index] = my_pos, new_state.current_line[index] + 1
        name1, name2, c1, c2, ley_line_dic['+0+1'], ley_line_dic['-1-1'] \
            = '+0+1', '-1-1', 0, 0, [], []
        ley_line_dic['+0+1'].append(
            new_state.grid[0][1])
        ley_line_dic['-1-1'].append(
            new_state.grid[-1][-1])
        for j in range(2, len(new_state.grid) - 1):
            for i in range(j - 1, len(new_state.grid) - 2):
                ley_line_dic[name1].append(new_state.grid[i][j])
                ley_line_dic[name2].append(new_state.grid[i][i + 2 - j])
                c1 = c1 + 1 if new_state.grid[i][j] == my_pos else c1
                c2 = c2 + 1 if new_state.grid[i][i + 2 - j] == my_pos else c2
            ley_line_dic[name1].append(
                new_state.grid[len(new_state.grid) - 2][j - 1])
            ley_line_dic[name2].append(
                new_state.grid[len(new_state.grid) - 2][-j])
            c1 = c1 + 1 if new_state.grid[
                len(new_state.grid) - 2][j - 1] == my_pos else c1
            c2 = c2 + 1 if new_state.grid[
                len(new_state.grid) - 2][-j] == my_pos else c2
            if c1 >= (len(ley_line_dic[name1]) - 1) / 2 and new_state.grid[int(
                    name1[0:2])][int(name1[2:])] == '@':
                new_state.grid[
                    int(name1[0:2])][int(name1[2:])], new_state.current_line[
                        index] = my_pos, new_state.current_line[index] + 1
            if c2 >= (len(ley_line_dic[name2]) - 1) / 2 and new_state.grid[int(
                    name2[0:2])][int(name2[2:])] == '@':
                new_state.grid[
                    int(name2[0:2])][int(name2[2:])], new_state.current_line[
                        index] = my_pos, new_state.current_line[index] + 1
            if j + 1 <= len(new_state.grid) - 2:
                name1, name2 = '+' + str(j - 1) + '+' + str(j + 1), \
                               '-1' + str(-j)
                ley_line_dic[name1], ley_line_dic[name2], c1, c2 = \
                    [], [], 0, 0
                ley_line_dic[name1].append(new_state.grid[j - 1][j + 1])
                ley_line_dic[name2].append(new_state.grid[-1][-j])
        for i in range(1, len(new_state.grid) - 1):
            name3, c3 = '+' + str(i) + '+' + '0', 0
            ley_line_dic[name3] = []
            for j in range(len(new_state.grid[i]) - 1):
                ley_line_dic[name3].append(new_state.grid[i][j])
                c3 = c3 + 1 if new_state.grid[i][j] == my_pos else c3
            if i == len(new_state.grid) - 3:
                ley_line_dic[name3].append(
                    new_state.grid[i][len(new_state.grid[i]) - 1])
                c3 = c3 + 1 if new_state.grid[i][
                    len(new_state.grid[i]) - 1] == my_pos else c3
            if c3 >= (len(ley_line_dic[name3]) - 1) / 2 and new_state.grid[int(
                    name3[0:2])][int(name3[2:])] == '@':
                new_state.grid[int(name3[0:2])][int(
                    name3[2:])], new_state.current_line[
                        index] = my_pos, new_state.current_line[index] + 1
        return new_state

    def __repr__(self) -> Any:
        """
        Return a representation of this StonehengeState (which can be used for
        equality testing).
        Overrides GameState.__repr__
        """
        player = 'p2'
        if self.p1_turn:
            player = 'p1'
        return "Player {} is playing and result as below: {}".format(player,
                                                                     self.grid)

    def rough_outcome(self) -> float:
        """
        Return an estimate in interval [LOSE, WIN] of best outcome the current
        player can guarantee from state self.
        >>> state = StonehengeState(True, 1)
        >>> state = state.make_move('A')
        >>> state.rough_outcome()
        -1
        """
        move_list = self.get_possible_moves()
        if move_list == []:
            return self.LOSE
        for item in move_list:
            new_state1 = self.make_move(item)
            if new_state1.get_possible_moves() == []:
                return self.WIN
            for item2 in new_state1.get_possible_moves():
                new_state2 = new_state1.make_move(item2)
                if new_state2.get_possible_moves() == []:
                    return self.LOSE
        return self.DRAW

if __name__ == "__main__":
    from doctest import testmod
    testmod()
    from python_ta import check_all
    check_all(config="a2_pyta.txt")


#!/usr/bin/env python
"""
Gcode cleaner to work around prusa slic3r annoyances for multi-filament
single-tool printing on non-Prusa printers.

This gist can be found here:
* https://gist.github.com/ex-nerd/22d0a9796f4f5df7080f9ac5a07a381f

Bugs this attempts to work around:
* https://github.com/prusa3d/Slic3r/issues/557
* https://github.com/prusa3d/Slic3r/issues/559
* https://github.com/prusa3d/Slic3r/issues/560
"""

import os
import re
import argparse

def addled(infile, outfile, verbose=False):
    lines = [line for line in infile]
    num_lines = len(list(filter(lambda line: not line.startswith(';'), lines))) # 100%

    #lines per percent
    lines_per_percent = num_lines / 100

    print('Total number of lines: {:d}'.format(len(lines)))
    print('Number of lines (w/o comments): {:d}'.format(num_lines))
    print('Number of lines per percent: {:f}'.format(lines_per_percent))

    line_count = 0
    total_line_count = 0
    percent = 0
    for line in lines:
        total_line_count = total_line_count + 1
        if not line.startswith(';'):
            line_count = line_count + 1
            if line_count > lines_per_percent:
                percent = percent + 1
                outfile.write('; Percent = {:d}\n'.format(percent))
                if verbose:
                    print('Percent: {:d} @ line {:d}'.format(percent, total_line_count))
                line_count = 0
        outfile.write(line)
    if verbose:
        print('--\n')

def parse_args():
    parser = argparse.ArgumentParser(
        description='Gcode manipulation. Adds LED commands for progress.'
    )
    parser.set_defaults(
        verbose=False,
        overwrite=False,
    )
    parser.add_argument(
        '--verbose', '-v',
        action='store_true',
        help="Enable debug output",
    )

    parser.add_argument(
        '--overwrite',
        action='store_true',
        help="Overwrite the input file (default is FALSE)",
    )
    parser.add_argument(
        'filenames',
        type=argparse.FileType('r'),
        nargs='+',
        help="One or more paths to .gcode files to extend with progress information",
    )
    return parser.parse_args()

if __name__ == '__main__':
    args = parse_args()
    if args.verbose:
        print('\r\n\r\nStarting conversion: \r\n\r\n')
        print('- overwrite source file: {}'.format(args.overwrite))
        print('- source files:')
        for infile in args.filenames:
            print('   * {}'.format(infile.name))
        print('\r\n')
    for infile in args.filenames:
        infilename = infile.name
        tmpfilename = '{}.tmp{}'.format(*os.path.splitext(infilename))
        with open(tmpfilename, 'w') as tmpfile:
            addled(infile, tmpfile, args.verbose)
        infile.close()
        if args.overwrite:
            os.rename(infilename, "{}.bak".format(infilename))
            outfilename = infilename
        else:
            outfilename = '{}.ledextended{}'.format(*os.path.splitext(infilename))
        os.rename(tmpfilename, outfilename)
        print("{} => {}".format(infilename, outfilename))


"""create categories table

Revision ID: fcb5737ade99
Revises: a6f255b9c242
Create Date: 2017-10-22 02:31:40.203051+00:00

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'fcb5737ade99'
down_revision = '7f4b767fea17'
branch_labels = None
depends_on = None

def upgrade():
    op.create_table(
        'categories',
        sa.Column('id', sa.Integer, primary_key=True),
        sa.Column('name', sa.String(64), nullable=True)
    )

def downgrade():
    op.drop_table('categories')


from flask import Flask, request, render_template
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

import nltk
from nltk.corpus import stopwords
nltk.download('stopwords')
set(stopwords.words('english'))

app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template('index.html')

@app.route('/', methods=['POST'])
def my_form_post():
    stop_words = stopwords.words('english')

    #convert to lowercase
    text1 = request.form['text1'].lower()
    text_final = ''.join(c for c in text1 if not c.isdigit())

    #remove stopwords
    processed_doc1 = ' '.join([word for word in text_final.split() if word not in stop_words])

    sa = SentimentIntensityAnalyzer()
    dd = sa.polarity_scores(text=processed_doc1)
    compound = dd['compound']

    if (compound > 0.05):
        final = "Positive"
    elif (compound < -0.05):
        final = "Negative"
    else:
        final = "Neutral"

    return render_template('index.html', final=final, pos=dd['pos'], neu=dd['neu'], neg=dd['neg'], compound=compound, text_final=text_final)

if __name__ == "__main__":
    app.run(debug=True, host="127.0.0.1", port=5002, threaded=True)

LearningContainers/Sentiment Analysis using VADAR/app.py


import unittest, os, convertor
from convertor.encode_convertor import encode
from pathlib import Path
from convertor.encode_convertor import encode as encod

KEY_FILE = Path(os.path.dirname(convertor.__file__)) / 'key'

class UnitTesting(unittest.TestCase):

    def test_0(self):
        texts="Privet HexTeam"
        code = encod(KEY_FILE)
        text=code.convert_text(texts)
        self.assertEqual(text, '|o|2!\\/\n37 #3><734|\\/|')

    def test_1(self):
        texts="Privet HexTeam"
        self.assertNotEqual(texts, '|o|2!\\/\n37 #3><734|\\/|')

    def test_2(self):
        texts="4|24<#|\|0"
        code = encod(KEY_FILE)
        text=code.convert_text(texts)
        self.assertEqual(text, 'arachno')

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

Interview Task/Interview task 0/unittests.py


import serial
import time

#####################################################################################
# Protocol Notes                                                                    #
# --------------                                                                    #
#                                                                                #
# - All communication happens one byte at a time                                    #
# - The `protocol` on the Python side is the canonical list of what each byte means #
# - Every message sent from Python to Arduino is ACKed                              #
#####################################################################################

def waitForArduinoReady(conn):
    message = conn.read()
    if message != '' and message[0] == getProtocolCode('arduinoReady'):
        pass
    else:
        print("Debug: Didn't get ready signal from Arduino. Already ready?")

protocol = {
    'arduinoReady': chr(1),
    'ACK': chr(2),

    # Lighting styles
    'pulse': chr(10),
    'rgb': chr(11),
    'white': chr(12),

    # Actions
    'pause': chr(30),
    'clear': chr(31)
}

def getProtocolCode(code):
    return protocol[code]

def sendToArduino(conn, operation):
    conn.write(getProtocolCode(operation))
    response = conn.read()
    if not response == getProtocolCode("ACK"):
        raise ValueError("Arduino didn't ACK the request")

def setup():
    conn = serial.Serial('COM4', 9600, timeout=1, write_timeout=1)
    waitForArduinoReady(conn)
    return conn


import subprocess
from datetime import datetime
from matplotlib import pyplot as plt

def run_script_for_version(script_name, program_name, iterations, image_name, num_threads=None):
    # Exécuter le script Python pour une version spécifique du programme C
    command = ["python3", script_name, program_name, str(iterations), image_name]
    if num_threads is not None:
        command.append(str(num_threads))
    result = subprocess.run(command, capture_output=True, text=True)
    avg_time_line = [line for line in result.stdout.split('\n') if "Temps moyen d'exécution :" in line]
    if avg_time_line:
        avg_time = float(avg_time_line[0].split(":")[1].strip().split(" ")[0])
        return avg_time, result
    return None, result

if __name__ == "__main__":
    script_name = "script.py"
    iterations = 100
    image_name = "Ressources/image1.pgm"

    versions = {
        #"./Codes_C/Code_Sequentiel/V1/Init": None,
        "./Codes_C/Code_Sequentiel/V2/Vecteur": None,
        "./Codes_C/Code_Sequentiel/V3_Float/Float": None,
        "./Codes_C/Code_Sequentiel/V4_Short/Short": None,
        "./Codes_C/Code_Sequentiel/V5_Char/Char": None,
        "./Codes_C/AVX/V4/AVX": None,
        #"./Codes_C/CUDA/V1/V1_CUDA": None,
        "./Codes_C/CUDA/V2/V2_CUDA": 10, # Taille des blocs
        "./Codes_C/OpenMP/V2_pixel_min_max/OMP": 9 # Nombre de thread(s)
        # ... (ajouter tous les autres chemins de fichier exécutables ici)
    }

    average_times = []
    for version, threads in versions.items():
        print(f"\nRunning for version: {version}\n")
        avg_time, result = run_script_for_version(script_name, version, iterations, image_name, threads)
        if avg_time is not None:
            average_times.append(avg_time)
        else:
            print(f"Erreur : le temps moyen n'a pas pu être calculé pour {version}")
            print("Sortie du script (stdout):\n", result.stdout)
            print("Sortie d'erreur du script (stderr):\n", result.stderr)
            average_times.append(0)

    labels = [version.split("/")[-1] for version in versions.keys()]

    plt.bar(range(len(versions)), average_times, tick_label=labels)
    plt.xlabel('Version')
    plt.ylabel('Temps moyen (us)')
    plt.title('Temps moyen d\'exécution par version')

    now = datetime.now()
    formatted_date = now.strftime("%Y-%m-%d-%H-%M-%S")
    fileName = "Graphes/GraphiquesBlocs/"+formatted_date+".png"
    plt.savefig(fileName)
    plt.show()


"""
@author: gsivaraman@anl.gov
"""
import GPyOpt
import json
import os
import numpy as np
from ase.io import read, write
import subprocess
from pprint import pprint
from time import time
from sklearn.metrics import mean_absolute_error, r2_score

def run_quip(cutoff=4.5, delta2b=.001, delta=1.0, n_sparse=100, n_sparse2b=50, lmax=4, nmax=8):
    """
    Descriptor specific input
    """
    if os.path.isfile('quip_test.xyz'):
        os.remove('quip_test.xyz')
    if os.path.isfile('gap.xml'):
        os.remove('gap.xml')

    start = time()
    quippath = "/home/gsivaraman/libatoms/QUIP/build/linux_x86_64_ifort_icc_openmp/"
    commands = quippath+"gap_fit  at_file=train.xyz  gap={distance_2b  cutoff="+str(cutoff)+"  n_sparse="+str(n_sparse2b)+"  sparse_method=uniform  delta="+str(delta2b)+"  covariance_type=ard_se theta_uniform=1.0  cutoff_transition_width=1.0  add_species : soap  cutoff="+str(cutoff)+"  n_sparse="+str(
        n_sparse)+"  covariance_type=dot_product sparse_method=cur_points  delta="+str(delta)+"  zeta=4 l_max="+str(lmax)+"  n_max="+str(nmax)+"  atom_sigma=0.5  cutoff_transition_width=1.0  add_species}   gp_file=gap.xml   default_sigma={0.001 0.1 0.01 0.0} sparse_jitter=1.0e-8 energy_parameter_name=energy "  # force_parameter_name=force  virial_parameter_name =virial
    # print(screenout)
    output = subprocess.check_output(commands,stderr=subprocess.STDOUT, shell=True)
    end = time()
    print("\nTraining Completed in {} sec".format(end - start))

    testoutstat = subprocess.check_output(quippath +
                                                   "quip  E=T   atoms_filename=test.xyz   param_filename=gap.xml  | grep AT | sed 's/AT//' >> quip_test.xyz",stderr=subprocess.STDOUT, shell=True)

    inp = read('test.xyz', ':')
    inenergy = [ei.get_potential_energy() for ei in inp]
    output = read('quip_test.xyz', ':')
    outenergy = [eo.get_potential_energy() for eo in output]
    if len(inenergy) == len(outenergy):
        mae = mean_absolute_error(np.asarray(outenergy), np.asarray(inenergy))
        rscore = r2_score(np.asarray(outenergy), np.asarray(inenergy))
        return [mae, rscore]

    else:
        print("Memory blow up for cutoff={}, n_sparse={}".format(cutoff, n_sparse))
        return [float(20000), float(20000)]

bounds = [{'name': 'cutoff',             'type': 'continuous', 'domain': (4, 8)},
          {'name': 'delta2b',            'type': 'continuous',
              'domain': (1, 20.0)},
          {'name': 'delta',            'type': 'continuous',
              'domain': (0.1, 0.99)},
          {'name': 'n_sparse',        'type': 'discrete',
              'domain': range(100, 1501, 100)},
          {'name': 'n_sparse2b',        'type': 'discrete',
              'domain': range(10, 105, 5)},
          {'name': 'lmax',            'type': 'discrete',  'domain': (4, 5, 6)},
          {'name': 'nmax',            'type': 'discrete',  'domain': (8, 9, 10, 11, 12)}, ]

# function to optimize MP model
def f(x):

    evaluation = run_quip(cutoff=float(x[:, 0]), delta2b=float(x[:, 1]), delta=float(x[:, 2]), n_sparse=float(
        x[:, 3]), n_sparse2b=float(x[:, 4]), lmax=float(x[:, 5]), nmax=float(x[:, 6]) )
    print("\nParam: {}  |  MAE, R2: {}".format(x, evaluation))

    return evaluation[0]

# optimizer
opt_quip = GPyOpt.methods.BayesianOptimization(f=f, domain=bounds, initial_design_numdata=35,
                                               model_type="GP_MCMC",
                                               acquisition_type='EI_MCMC',  # EI
                                               evaluator_type="predictive",  # Expected Improvement
                                               exact_feval=False)

# optimize MP model
print("\nBegin Optimization run \t")

opt_quip.run_optimization(max_iter=75)
x_best = opt_quip.x_opt

print("\n\n Best parameters :\n")
print(x_best)

print("\nOptimized Parameters:\n")

hyperdict = {}
for num in range(len(bounds)):
    hyperdict[bounds[num]["name"]] = opt_quip.x_opt[num]

hyperdict["MAE"] = opt_quip.fx_opt

print("\n Writing output : hyperparam_quip.json\n")

with open('hyperparam_quip.json', 'w') as outfile:
    json.dump(hyperdict, outfile)

print("\n Printing best hyper parameters : \n")

pprint(hyperdict)


#  !!! json bize veri taşımada yardım eder !!!

import json

personString = '{"name":"Ali", "languages":["python","C#"]}'

personDict = { "name": "Ali","languages": ["Python","C#"]}

# JSON string to Dict
"""
jsonDonusturme = json.loads(personString)
#jsonDonusturme = jsonDonusturme["name"]

print(jsonDonusturme)
"""
# Bunun yerine bu kullanılabilir
with open("person.json") as f:
    data = json.load(f)
    print(data)
# ***************************************************

# Dict to JSON string
"""
print(type(personDict))

jsonCevirme = json.dumps(personDict) # json un bilgiye ulaşabilmesi için bilgiyi str yapar
print(type(jsonCevirme))
"""
personDict = json.loads(personString)
result = json.dumps(personDict, indent= 4) # okunaklığı arttırmak için belirtilen sayıda boşluk bıraktırır

print(personDict)
print(result)


import networkx as nx
import matplotlib.pyplot as plot
import numpy

G = nx.Graph()
G.add_edge('a', 'b', score=3)
G.add_edge('b', 'c', score=4)
G.add_edge('a', 'c', score=1)
G.add_edge('a', 'd', score=7)
G.add_edge('b', 'e', score=8)
G.add_edge('c', 'd', score=9)

# If we wanted a's neighbors, we just access that node directly:

print("Dict G['a']=", G['a'])

# To sort those results, we use tools from the standard Python toolbox.
# .items() to convert the dict to a tuple and the sorted builtin to sort the results:
esort = sorted(list(G['a'].items()), key=lambda edge: edge[1]['score'])
print("esort=", esort)

xpos=nx.spring_layout(G)
nx.draw(G, pos=xpos, node_color='r',edge_color='b')
nx.draw_networkx_edges (G,xpos,width=2)
nx.draw_networkx_labels(G,xpos,font_size=12,font_family='sans-serif')

plot.show()


from requests.api import request
from lib.Hubot import Hubot

if __name__ == "__main__":

    config = {
        'message_type' : {
            'text': True,
            'link': True,
            'files': {
                'image': True,
                'video': True,
                'audio': True,
                'application': True
            }
        },
        'skip_pinned' : True,
        'date': [None, None],
        'limit': 100
    }

    hubocik = Hubot('YOUR_SESSION_ID', config)
    hubocik.channel_id = "ROOM-ID"
    import time
    start = time.time()
    hubocik.run()
    print('\nKoniec',end="\n")
    end =time.time() - start
    print("="*10)
    print("sec:", end)
    print(len(hubocik.Messages))