from __future__ import annotations

from typing import List, TYPE_CHECKING, Optional

from . import utils
from .asset import Asset
from .flags import ApplicationFlags
from .permissions import Permissions

if TYPE_CHECKING:
    from .guild import Guild
    from .types.appinfo import (
        AppInfo as AppInfoPayload,
        PartialAppInfo as PartialAppInfoPayload,
        Team as TeamPayload,
        InstallParams as InstallParamsPayload,
    )
    from .user import User
    from .state import ConnectionState

__all__ = (
    'AppInfo',
    'PartialAppInfo',
    'AppInstallParams',
)

class AppInfo:
    """Represents the application info for the bot provided by Discord.

    Attributes
    -------------
    id: :class:`int`
        The application ID.
    name: :class:`str`
        The application name.
    owner: :class:`User`
        The application owner.
    team: Optional[:class:`Team`]
        The application's team.

        .. versionadded:: 1.3

    description: :class:`str`
        The application description.
    bot_public: :class:`bool`
        Whether the bot can be invited by anyone or if it is locked
        to the application owner.
    bot_require_code_grant: :class:`bool`
        Whether the bot requires the completion of the full oauth2 code
        grant flow to join.
    rpc_origins: Optional[List[:class:`str`]]
        A list of RPC origin URLs, if RPC is enabled.

    verify_key: :class:`str`
        The hex encoded key for verification in interactions and the
        GameSDK's :ddocs:`GetTicket <game-sdk/applications#getticket>`.

        .. versionadded:: 1.3

    guild_id: Optional[:class:`int`]
        If this application is a game sold on Discord,
        this field will be the guild to which it has been linked to.

        .. versionadded:: 1.3

    primary_sku_id: Optional[:class:`int`]
        If this application is a game sold on Discord,
        this field will be the id of the "Game SKU" that is created,
        if it exists.

        .. versionadded:: 1.3

    slug: Optional[:class:`str`]
        If this application is a game sold on Discord,
        this field will be the URL slug that links to the store page.

        .. versionadded:: 1.3

    terms_of_service_url: Optional[:class:`str`]
        The application's terms of service URL, if set.

        .. versionadded:: 2.0

    privacy_policy_url: Optional[:class:`str`]
        The application's privacy policy URL, if set.

        .. versionadded:: 2.0

    tags: List[:class:`str`]
        The list of tags describing the functionality of the application.

        .. versionadded:: 2.0

    custom_install_url: List[:class:`str`]
        The custom authorization URL for the application, if enabled.

        .. versionadded:: 2.0

    install_params: Optional[:class:`AppInstallParams`]
        The settings for custom authorization URL of application, if enabled.

        .. versionadded:: 2.0
    role_connections_verification_url: Optional[:class:`str`]
        The application's connection verification URL which will render the application as
        a verification method in the guild's role verification configuration.

        .. versionadded:: 2.2
    """

    __slots__ = (
        '_state',
        'description',
        'id',
        'name',
        'rpc_origins',
        'bot_public',
        'bot_require_code_grant',
        'owner',
        '_icon',
        'verify_key',
        'team',
        'guild_id',
        'primary_sku_id',
        'slug',
        '_cover_image',
        '_flags',
        'terms_of_service_url',
        'privacy_policy_url',
        'tags',
        'custom_install_url',
        'install_params',
        'role_connections_verification_url',
    )

    def __init__(self, state: ConnectionState, data: AppInfoPayload):
        from .team import Team

        self._state: ConnectionState = state
        self.id: int = int(data['id'])
        self.name: str = data['name']
        self.description: str = data['description']
        self._icon: Optional[str] = data['icon']
        self.rpc_origins: Optional[List[str]] = data.get('rpc_origins')
        self.bot_public: bool = data['bot_public']
        self.bot_require_code_grant: bool = data['bot_require_code_grant']
        self.owner: User = state.create_user(data['owner'])

        team: Optional[TeamPayload] = data.get('team')
        self.team: Optional[Team] = Team(state, team) if team else None

        self.verify_key: str = data['verify_key']

        self.guild_id: Optional[int] = utils._get_as_snowflake(data, 'guild_id')

        self.primary_sku_id: Optional[int] = utils._get_as_snowflake(data, 'primary_sku_id')
        self.slug: Optional[str] = data.get('slug')
        self._flags: int = data.get('flags', 0)
        self._cover_image: Optional[str] = data.get('cover_image')
        self.terms_of_service_url: Optional[str] = data.get('terms_of_service_url')
        self.privacy_policy_url: Optional[str] = data.get('privacy_policy_url')
        self.tags: List[str] = data.get('tags', [])
        self.custom_install_url: Optional[str] = data.get('custom_install_url')
        self.role_connections_verification_url: Optional[str] = data.get('role_connections_verification_url')

        params = data.get('install_params')
        self.install_params: Optional[AppInstallParams] = AppInstallParams(params) if params else None

    def __repr__(self) -> str:
        return (
            f'<{self.__class__.__name__} id={self.id} name={self.name!r} '
            f'description={self.description!r} public={self.bot_public} '
            f'owner={self.owner!r}>'
        )

    @property
    def icon(self) -> Optional[Asset]:
        """Optional[:class:`.Asset`]: Retrieves the application's icon asset, if any."""
        if self._icon is None:
            return None
        return Asset._from_icon(self._state, self.id, self._icon, path='app')

    @property
    def cover_image(self) -> Optional[Asset]:
        """Optional[:class:`.Asset`]: Retrieves the cover image on a store embed, if any.

        This is only available if the application is a game sold on Discord.
        """
        if self._cover_image is None:
            return None
        return Asset._from_cover_image(self._state, self.id, self._cover_image)

    @property
    def guild(self) -> Optional[Guild]:
        """Optional[:class:`Guild`]: If this application is a game sold on Discord,
        this field will be the guild to which it has been linked

        .. versionadded:: 1.3
        """
        return self._state._get_guild(self.guild_id)

    @property
    def flags(self) -> ApplicationFlags:
        """:class:`ApplicationFlags`: The application's flags.

        .. versionadded:: 2.0
        """
        return ApplicationFlags._from_value(self._flags)

class PartialAppInfo:
    """Represents a partial AppInfo given by :func:`~discord.abc.GuildChannel.create_invite`

    .. versionadded:: 2.0

    Attributes
    -------------
    id: :class:`int`
        The application ID.
    name: :class:`str`
        The application name.
    description: :class:`str`
        The application description.
    rpc_origins: Optional[List[:class:`str`]]
        A list of RPC origin URLs, if RPC is enabled.
    verify_key: :class:`str`
        The hex encoded key for verification in interactions and the
        GameSDK's :ddocs:`GetTicket <game-sdk/applications#getticket>`.
    terms_of_service_url: Optional[:class:`str`]
        The application's terms of service URL, if set.
    privacy_policy_url: Optional[:class:`str`]
        The application's privacy policy URL, if set.
    approximate_guild_count: :class:`int`
        The approximate count of the guilds the bot was added to.

        .. versionadded:: 2.3
    redirect_uris: List[:class:`str`]
        A list of authentication redirect URIs.

        .. versionadded:: 2.3
    interactions_endpoint_url: Optional[:class:`str`]
        The interactions endpoint url of the application to receive interactions over this endpoint rather than
        over the gateway, if configured.

        .. versionadded:: 2.3
    role_connections_verification_url: Optional[:class:`str`]
        The application's connection verification URL which will render the application as
        a verification method in the guild's role verification configuration.

        .. versionadded:: 2.3
    """

    __slots__ = (
        '_state',
        'id',
        'name',
        'description',
        'rpc_origins',
        'verify_key',
        'terms_of_service_url',
        'privacy_policy_url',
        '_icon',
        '_flags',
        '_cover_image',
        'approximate_guild_count',
        'redirect_uris',
        'interactions_endpoint_url',
        'role_connections_verification_url',
    )

    def __init__(self, *, state: ConnectionState, data: PartialAppInfoPayload):
        self._state: ConnectionState = state
        self.id: int = int(data['id'])
        self.name: str = data['name']
        self._icon: Optional[str] = data.get('icon')
        self._flags: int = data.get('flags', 0)
        self._cover_image: Optional[str] = data.get('cover_image')
        self.description: str = data['description']
        self.rpc_origins: Optional[List[str]] = data.get('rpc_origins')
        self.verify_key: str = data['verify_key']
        self.terms_of_service_url: Optional[str] = data.get('terms_of_service_url')
        self.privacy_policy_url: Optional[str] = data.get('privacy_policy_url')
        self.approximate_guild_count: int = data.get('approximate_guild_count', 0)
        self.redirect_uris: List[str] = data.get('redirect_uris', [])
        self.interactions_endpoint_url: Optional[str] = data.get('interactions_endpoint_url')
        self.role_connections_verification_url: Optional[str] = data.get('role_connections_verification_url')

    def __repr__(self) -> str:
        return f'<{self.__class__.__name__} id={self.id} name={self.name!r} description={self.description!r}>'

    @property
    def icon(self) -> Optional[Asset]:
        """Optional[:class:`.Asset`]: Retrieves the application's icon asset, if any."""
        if self._icon is None:
            return None
        return Asset._from_icon(self._state, self.id, self._icon, path='app')

    @property
    def cover_image(self) -> Optional[Asset]:
        """Optional[:class:`.Asset`]: Retrieves the cover image of the application's default rich presence.

        This is only available if the application is a game sold on Discord.

        .. versionadded:: 2.3
        """
        if self._cover_image is None:
            return None
        return Asset._from_cover_image(self._state, self.id, self._cover_image)

    @property
    def flags(self) -> ApplicationFlags:
        """:class:`ApplicationFlags`: The application's flags.

        .. versionadded:: 2.0
        """
        return ApplicationFlags._from_value(self._flags)

class AppInstallParams:
    """Represents the settings for custom authorization URL of an application.

    .. versionadded:: 2.0

    Attributes
    ----------
    scopes: List[:class:`str`]
        The list of :ddocs:`OAuth2 scopes <topics/oauth2#shared-resources-oauth2-scopes>`
        to add the application to a guild with.
    permissions: :class:`Permissions`
        The permissions to give to application in the guild.
    """

    __slots__ = ('scopes', 'permissions')

    def __init__(self, data: InstallParamsPayload) -> None:
        self.scopes: List[str] = data.get('scopes', [])
        self.permissions: Permissions = Permissions(int(data['permissions']))


import json
colnames = ['cylinders','mpg'];
editable_vars = ['numchar','nature'];
preprocess_json = {
               "$schema":"http://(link to eventual schema)/jsonschema/1-0-0#",
               "self":{
                  "description":"TwoRavens metadata generated by ....",
                  "created":"..time stamp..",
                  "preprocess_id":45,
                  "data_url":"http://metadata.2ravens-url.org/preprocess/data/45",
                  "format":"jsonschema",
                  "preprocess_version":"1-0-0",
                  "schema_version":"1.0.0"
               },
               "variables": {
                  "cylinders": {
                    "varnameTypes": "cylinders",
                     "numchar":"continuous",
                      "nature": "nominal",
                     "mean":213,
                     "median":34

                  },
                  "mpg":{
                        "varnameTypes": "mpg",
                      "numchar": "continuous",
                       "nature": " ordinal",
                       "mean": 313,
                       "median": 54

                   }
               },
               "variable_display":{
                  "cylinders":{
                     "viewable":"true",
                     "omit":[
                     ],
                     "label":"",
                     "images":{
                        "url1":{
                           "type":"pdf",
                           "description":"This is the density of the mpg variable.",
                           "dateCreated":"timestamp",
                           "version":"imagecreate v0.1"
                        }
                     }
                  },
                   "mpg":
{
                     "viewable":"true",
                     "omit":[
                     ],
                     "label":"",
                     "images":{
                        "url1":{
                           "type":"pdf",
                           "description":"This is the density of the mpg variable.",
                           "dateCreated":"timestamp",
                           "version":"imagecreate v0.1"
                        }
                     }
                  }
               }
}
attributes = ['numchar','nature','mean', 'median']

update_json =  {
            "preprocess_id": 45,
            "variable_updates": {
               "cylinders" : {
                  "viewable": True,
                 "omit": ["mean", "median"],
                 "label": {
                     "numchar":"discrete",
                     "nature": "ordinal"
                 }
               },
               "mpg": {
                 "viewable": False,
                 "omit": [],
                 "label": {

                 }
               }
            }
        }

access_object = update_json['variable_updates']

original_json =preprocess_json
access_obj_original = original_json['variables']
access_obj_original_display = original_json['variable_display']
a = list(access_obj_original)
for varname in a:
     print(varname)

# print("original variavles", access_obj_original)
# print("original displayvar", access_obj_original_display)
def modify_original(varname, omit_obj, viewable_obj, label_obj):
    # print("*** INFO WE GET FROM PARAM")
    # print("varname", varname)
    # print("omit_obj", omit_obj)
    # print("viewable_obj", viewable_obj)
    # print("lable", label_obj)

    if not varname in access_obj_original:
        print('"%s" was not found in the "variable" section of the metadata file' % varname)
        return

    elif not varname in access_obj_original_display:
        print('"%s" was not found in the "variable_display" section of the metadata file' % varname)
        return

    else:
        variable_obj = access_obj_original[varname]
        display_variable_obj = access_obj_original_display[varname]
        # print(variable_obj)
        """
        variable_obj contains : "numchar":"continuous",
                    "nature": "nominal",
                   "mean":213,
                   "median":34
        """
        # code for omit
        if omit_obj:
            # start deleting omit objects
            for omit_var in omit_obj:
                del variable_obj[omit_var]
            display_variable_obj['omit'] = omit_obj

        # code for viewable
        if viewable_obj is False:
            # print("delete", var)
            del access_obj_original[varname]
            display_variable_obj['viewable'] = False

        # code for label
        if label_obj:
            for att_name in attributes:
                if att_name in label_obj and att_name in editable_vars:
                    variable_obj[att_name] = label_obj[att_name]

            display_variable_obj['label'] = label_obj

access_object = update_json['variable_updates']
for varname in colnames:
    print(' level 1 var name', varname);
    omit_object = access_object[varname]['omit']
    viewable_object = access_object[varname]['viewable']
    label_object = access_object[varname]['label']

    modify_original(varname, omit_object, viewable_object, label_object)

print("*********")
print(original_json)
print("*********")

"""
output should be =
{
    "$schema": "http://(link to eventual schema)/jsonschema/1-0-0#",
    "self": {
        "description": "TwoRavens metadata generated by ....",
        "created": "..time stamp..",
        "preprocess_id": 45,
        "data_url": "http://metadata.2ravens-url.org/preprocess/data/45",
        "format": "jsonschema",
        "preprocess_version": "1-0-0",
        "schema_version": "1.0.0"
    },
    "variables": {
        "cylinders": {
            "numchar": "discrete",
            "nature": "ordinal",
            "mean": 213,
            "median": 34
        },
        "mpg": {
            "numchar": "continuous",
            "nature": " ordinal",
            "mean": 313,
            "median": 54
        }
    },
    "variable_display": {
        "cylinders": {
            "viewable": "true",
            "omit": [
                "mean",
                "median"
            ],
            "label": {
                "numchar": "discrete",
                "nature": "ordinal"
            },
            "images": {
                "url1": {
                    "type": "pdf",
                    "description": "This is the density of the mpg variable.",
                    "dateCreated": "timestamp",
                    "version": "imagecreate v0.1"
                }
            }
        },
        "mpg": {
            "viewable": false,
            "omit": [],
            "label": "",
            "images": {
                "url1": {
                    "type": "pdf",
                    "description": "This is the density of the mpg variable.",
                    "dateCreated": "timestamp",
                    "version": "imagecreate v0.1"
                }
            }
        }
    }
}

"""


'''
    347. 前 K 个高频元素

    给定一个非空的整数数组，返回其中出现频率前 k 高的元素。

    示例 1:

    输入: nums = [1,1,1,2,2,3], k = 2
    输出: [1,2]
    示例 2:

    输入: nums = [1], k = 1
    输出: [1]
    说明：

    你可以假设给定的 k 总是合理的，且 1 ≤ k ≤ 数组中不相同的元素的个数。
    你的算法的时间复杂度必须优于 O(n log n) , n 是数组的大小。

'''
from collections import Counter
import heapq
class Solution:
    # 方法一：api 法
    def topKFrequent(self, nums, k):
        count = Counter(nums)
        return heapq.nlargest(k, count.keys(), key=count.get)

if __name__ == "__main__":

    solution = Solution()
    while 1:
        str1 = input()
        str2 = input()
        if str1 != "" and str2 != "":
            nums = [int(num) for num in str1.split(",")]
            k = int(str2)
            res = solution.topKFrequent(nums, k)
            print(res)
        else:
            break


# -*- coding: utf-8 -*-
from PyQt4 import QtGui
from modules.classes.custom.QTableWidgetItem import QCustomTableWidgetItem as QCI

def setItemArmEvEsChtbGcscGcsmCscfsCspSpdQty(form, itemIndex, dataPropertiesImplicitExplicitLinesList, typeName):
    if dataPropertiesImplicitExplicitLinesList:
        temp = dataPropertiesImplicitExplicitLinesList
        valueArmour = 0
        valueEvasion = 0
        valueEnergyShield = 0
        valueChanceToBlock = 0
        dataGcsc = []
        dataGcsm = []
        dataCscfs = []
        dataCsp = []
        dataSpd = []
        valueGcsc = 0
        valueGcsm = 0
        valueCscfs = 0
        valueCsp = 0
        valueSpd = 0
        valueStackQuantity = None
        for i in range (len(temp)):
            if 'Stack Size' in temp[i]:
                valueStackQuantity = int(temp[i].split(':')[1].split('/')[0])
                continue
            if typeName == 'Essence':
                break
            if 'Armour:' in temp[i]:
                valueArmour = int(temp[i].split(':')[1].split('%')[0])
                continue
            if 'Evasion Rating:' in temp[i]:
                valueEvasion = int(temp[i].split(':')[1].split('%')[0])
                continue
            if 'Energy Shield:' in temp[i]:
                valueEnergyShield = int(temp[i].split(':')[1].split('%')[0])
                continue
            if 'Chance to Block:' in temp[i]:
                valueChanceToBlock = int(temp[i].split(':')[1].split('%')[0])
                continue
            if '% increased Global Critical Strike Chance' in temp[i]:
                    dataGcsc.append(int(temp[i].split('%')[0]))
                    continue
            if '% to Global Critical Strike Multiplier' in temp[i]:
                    dataGcsm.append(int(temp[i].split('%')[0]))
                    continue
            if '% increased Critical Strike Chance for Spells' in temp[i]:
                    dataCscfs.append(int(temp[i].split('%')[0]))
                    continue
            if ('% increased Cast Speed' in temp[i]) and not ('Minions' in temp[i]) and not ('nearby' in temp[i]):
                    dataCsp.append(int(temp[i].split('%')[0]))
                    continue
            if '% increased Spell Damage' in temp[i]:
                    dataSpd.append(int(temp[i].split('%')[0]))
                    continue

        if dataGcsc:
            valueGcsc = sum(dataGcsc)

        if dataGcsm:
            valueGcsm = sum(dataGcsm)

        if dataCscfs:
            valueCscfs = sum(dataCscfs)

        if dataCsp:
            valueCsp = sum(dataCsp)

        if dataSpd:
            valueSpd = sum(dataSpd)

        #non stackable currency
        if typeName == 'Currency' and not valueStackQuantity:
            valueStackQuantity = 1

        if valueArmour:
            form.tableWidget.setItem(itemIndex, form.ig.columnNameToIndex['Arm'], QCI(valueArmour))
        if valueEvasion:
            form.tableWidget.setItem(itemIndex, form.ig.columnNameToIndex['Ev'], QCI(valueEvasion))
        if valueEnergyShield:
            form.tableWidget.setItem(itemIndex, form.ig.columnNameToIndex['ES'], QCI(valueEnergyShield))
        if valueChanceToBlock:
            form.tableWidget.setItem(itemIndex, form.ig.columnNameToIndex['ChtB'], QCI(valueChanceToBlock))
        if valueGcsc:
            form.tableWidget.setItem(itemIndex, form.ig.columnNameToIndex['GcsC'], QCI(valueGcsc))
        if valueGcsm:
            form.tableWidget.setItem(itemIndex, form.ig.columnNameToIndex['GcsM'], QCI(valueGcsm))
        if valueCscfs:
            form.tableWidget.setItem(itemIndex, form.ig.columnNameToIndex['CscfS'], QCI(valueCscfs))
        if valueCsp:
            form.tableWidget.setItem(itemIndex, form.ig.columnNameToIndex['Csp'], QCI(valueCsp))
        if valueSpd:
            form.tableWidget.setItem(itemIndex, form.ig.columnNameToIndex['SpD'], QCI(valueSpd))
        if valueStackQuantity:
            form.tableWidget.setItem(itemIndex, form.ig.columnNameToIndex['Qty'], QCI(valueStackQuantity))


n = int(input("Enter number of values : "))
a_dict = {}

for i in range(n):
    text = input().split()
    a_dict[text[0]] = int(text[1])

def removeDuplicates(a_dict):
    b_dict = {}
    n = 0
    m = 0
    first = 0
    for key1 in a_dict:
        if first == 0:
            b_dict[key1] = a_dict[key1]
            first = 1
        else:
            for key2 in b_dict:
                m = m + 1
                if a_dict[key1] != b_dict[key2]:
                    n = n + 1

            if n == m:
                b_dict[key1] = a_dict[key1]
            n = 0
            m = 0

    return b_dict

def sortDict(b_dict):
    c_dict = {}
    while b_dict:
        key_min = min(b_dict, key=b_dict.get)
        c_dict[key_min] = b_dict[key_min]
        del b_dict[key_min]

    return c_dict

b_dict = removeDuplicates(a_dict)
print(b_dict)

c_dict = sortDict(b_dict)
print("\nsortDict : ", c_dict)


# -*-coding:utf-8 -*
"""
基于dqn的agent
"""
import numpy as np
import random
from agent import Agent
import sys
sys.path.append(sys.path[0].replace("agent",""))
from config import args
from policy_learning import DQN_dis

class AgentDQN(Agent):
    def __init__(self, slot_set, disease_set, disease_symptom):
        super(AgentDQN, self).__init__(slot_set=slot_set,disease_set=disease_set,disease_symptom=disease_symptom)

        input_size = args.input_size
        hidden_size = args.hidden_size
        output_size = len(self.action_space)
        self.dis2slot=np.load('./data/dis2slot.npy')
        self.slot2dis=np.load('./data/slot2dis.npy')
        self.p_slot=np.load('./data/p_slot.npy')

        self.dqn = DQN_dis(input_size=input_size, hidden_size=hidden_size,output_size=output_size)

    def sample_action(self, state, turn,greedy_strategy):
        '''
        根据greedy策略获取action
        ：1 则采用e-greedy，随机或最大的action(st;θ)
        ：0 就直接采用最大的action(st;θ)
        '''
        self.agent_action["turn"] = turn

        state_rep = self.state_to_rep(state=state)  # （347+22，
        #print('----')
        #print(state_rep[365])
        #print('----')
        state = state_rep[:347].reshape(1,-1)
        mask = state!=1
        mask2 = state!=0
        final_p_slot = self.p_slot.copy()
        final_p_slot[mask] = 0
        final_dis = np.matmul(final_p_slot,self.slot2dis) #（1,6
        final_sym = np.matmul(final_dis,self.dis2slot)
        final_sym[mask2] = 0
        final_p = np.hstack((final_sym,final_dis))
        #print('kg',np.argmax(final_p[0][:347]),max(final_p[0]))
        model_y=self.dqn.predict(Xs=[state_rep])[0]      # （1，347,应该是每个都21轮
        #print('model',np.argmax(model_y[0][:347]),max(model_y[0]))
        final = final_p+model_y
        max_index = np.argmax(final[0][:347])
        #print('final',max_index)
        dis = self.dqn.predict_DIS(Xs=[state_rep])[0] #（1,6

        final_dis = final_dis+dis

        if state_rep[365] == 1:
            #print('进去了')
            max_index = np.argmax(final_dis, axis=1)[0]
            max_index = max_index + 347
            #print(max_index)

        if greedy_strategy == 1:
            greedy = random.random()
            if greedy < args.epsilon:
                action_index = random.randint(0, len(self.action_space) - 1) # random select a action
            else:
                action_index = max_index
        else:
            action_index = max_index

        #print('turn',turn,'insex',action_index)
        agent_action = self.action_space[action_index]
        agent_action["turn"] = turn
        agent_action["speaker"] = "agent"
        return agent_action, action_index

    def train(self, batch):
        '''
        优化一个batch的数据
        '''
        loss = self.dqn.singleBatch(batch=batch)
        return loss

    def update_target_network(self):
        '''
        更新目标网络
        '''
        self.dqn.update_target_network()

    def save_model(self, model_performance,episodes_index, checkpoint_path = None):
        '''
        保存模型
        '''
        self.dqn.save_model(model_performance=model_performance, episodes_index = episodes_index, checkpoint_path=checkpoint_path)


# -*- coding: utf-8 -*-
"""
Project: Psychophysics_exps
Creator: Miao
Create time: 2020-12-20 21:07
IDE: PyCharm
Introduction: preprocessed data of crowding and numerosity exp3a (online pilot)
"""
from src.analysis.exp3a_pilot_analysis import insert_is_resp_ref_more, insert_probeN, insert_refN, insert_probeCrowding, \
    insert_refCrowing, insert_exp_condition, insert_is_resp_probe_more
from src.preprocess.sub import merge_all_data
import pandas as pd

from src.constants.exp3a_pilot_constants import KEPT_COL_NAMES
from src.preprocess.sub.get_data2analysis import drop_df_nan_rows_according2cols, drop_df_rows_according2_one_col, \
    get_col_boundary
from src.commons.process_dataframe import keep_valid_columns, insert_new_col_from_two_cols, \
    insert_new_col_from_three_cols, insert_new_col

def preprocess_exp3a_func(data_path: str, filetype: str, filename_prefix: str) -> pd.DataFrame:
    all_df = merge_all_data.merge_all_file2dataframe(data_path, filetype, filename_prefix)
    return all_df

if __name__ == '__main__':
    DATA_PATH = "../../data/exp3_data/exp3_pilot_data/rawdata/"
    FILENAME_PREFIX = "P"
    FILETYPE = ".csv"
    drop_fastandslow_resp = False
    save_preprocessed = True

    # load raw data
    mydata = preprocess_exp3a_func(DATA_PATH, FILETYPE, FILENAME_PREFIX)
    # preprocess starts here
    mydata = keep_valid_columns(mydata, KEPT_COL_NAMES)

    # drop practice trials: drop all rows with NaNs in key_resp.keys
    col_to_dropna = ['key_resp.keys']
    mydata = drop_df_nan_rows_according2cols(mydata, col_to_dropna)

    # drop too fast and too slow response
    if drop_fastandslow_resp:
        col_to_drop_rows = "key_resp.rt"
        min_rt = 0.15
        max_rt = 3
        mydata = drop_df_rows_according2_one_col(mydata, col_to_drop_rows, min_rt, max_rt)

    # add numerosity difference between D1 and D2
    mydata["dff_D1D2"] = mydata["D1numerosity"] - mydata["D2numerosity"]
    # add correct answer
    insert_new_col_from_two_cols(mydata, "ref_first", "key_resp.keys", "is_resp_ref_more", insert_is_resp_ref_more)
    insert_new_col(mydata, "is_resp_ref_more", "is_resp_probe_more", insert_is_resp_probe_more)
    # add probe numerosity
    insert_new_col_from_three_cols(mydata, "D1numerosity", "D2numerosity", "ref_first", "probeN", insert_probeN)
    # add ref numerosity
    insert_new_col_from_three_cols(mydata, "D1numerosity", "D2numerosity", "ref_first", "refN", insert_refN)
    # add probe crowding condition
    insert_new_col_from_three_cols(mydata, "D1Crowding", "D2Crowding", "ref_first", "probeCrowding",
                                   insert_probeCrowding)
    # add ref crowding condition
    insert_new_col_from_three_cols(mydata, "D1Crowding", "D2Crowding", "ref_first", "refCrowding", insert_refCrowing)

    # %% experiment conditions
    # indicate different condition with extra columns
    insert_new_col_from_two_cols(mydata, "ref_c", "probe_c", "ref_probe_condi", insert_exp_condition)

    if save_preprocessed:
        mydata.to_excel("exp3a_preprocessed.xlsx")


import rclpy
import time
import datetime
import numpy as np

from rclpy.node import Node
from sensor_msgs.msg import Imu, BatteryState
from sensor_msgs.msg import LaserScan
from rclpy.qos import qos_profile_sensor_data

class SensorsSubscriber(Node):
	def __init__(self):
		super().__init__('sensors_subscriber')

		self.scan_ranges = []
		self.init_scan_state = False

		# store last variables for easy access later
		self.current_imu_msg = None
		self.current_scn__msg = None
		self.current_bat_msg = None

		# parameter list for easy reference
		self.SensorSubscriberParameterNames = (
			'imu_hz','scn_hz','bat_hz','imu_en','scn_en','bat_en'
		)
		# message frequency
		self.imu_hz = 1
		self.scn_hz = 1
		self.bat_hz = 1
		self.hz_debug = time.time()
		# enable/disable printing of variables
		self.imu_en = 1
		self.scn_en = 1
		self.bat_en = 1

		# set and get parameters
		self.set_params()
		self.get_params()

		# subscription format: create_subscription(NameOfCallback, 'topic_name', self.listener_callback, qos_profile)
		self.create_subscription(Imu, 'imu', self.imu_callback, qos_profile_sensor_data)
		self.create_subscription(BatteryState, 'battery_state', self.battery_callback, qos_profile=qos_profile_sensor_data)
		self.create_subscription(LaserScan, 'scan', self.scan_callback, qos_profile=qos_profile_sensor_data)
		self.imu_count = 0

		# Creating timers to make sure that they dont print continuously
		self.timer_imu = self.create_timer(self.imu_hz, self.imu_callback)
		#self.timer_imu.duration = self.imu_hz

		self.timer_scn = self.create_timer(self.scn_hz, self.scan_callback)
		#self.timer_scn.duration = self.scn_hz

		self.timer_bat = self.create_timer(self.bat_hz, self.battery_callback)
		#self.timer_bat.duration = self.bat_hz

	def set_params(self):

		for thing in self.SensorSubscriberParameterNames:
			self.declare_parameter(thing,1)

	def get_params(self):

		self.imu_hz = self.get_parameter('imu_hz').get_parameter_value().integer_value
		self.scn_hz = self.get_parameter('scn_hz').get_parameter_value().integer_value
		self.bat_hz = self.get_parameter('bat_hz').get_parameter_value().integer_value
		self.imu_en = self.get_parameter('imu_en').get_parameter_value().integer_value
		self.scn_en = self.get_parameter('scn_en').get_parameter_value().integer_value
		self.bat_en = self.get_parameter('bat_en').get_parameter_value().integer_value

	def imu_callback(self, msg=None):

		if msg != None:
			self.current_imu_msg = msg
		else:
			return

		if self.imu_en:
			abt_x = msg.orientation.x
			abt_y = msg.orientation.y
			abt_z = msg.orientation.z

			av_x, av_y, av_z = msg.angular_velocity.x, msg.angular_velocity.y, msg.angular_velocity.z
			la_x, la_y, la_z = msg.linear_acceleration.x, msg.angular_velocity.y, msg.angular_velocity.z

			self.get_logger().info(f'[Imu]: Orient ({abt_x},{abt_y},{abt_z}) | AV: ({av_x},{av_y},{av_z}) | LA: ({la_x},{la_y},{la_z})', throttle_duration_sec=self.imu_hz)

	def scan_callback(self, msg=None):

		if msg != None:
			self.current_scn_msg = msg
		else:
			return

		if self.scn_en:
			self.scan_ranges = []
			for i in msg.ranges: #filter some of the noise
				if i == 0:
					continue
				else:
					self.scan_ranges.append(i)

			self.scan_ranges = np.array(self.scan_ranges)

			self.get_logger().info(f'[Scanner]: Min {self.scan_ranges.min()} | Max {self.scan_ranges.max()}', throttle_duration_sec=self.scn_hz)

	def battery_callback(self, msg=None):

		if msg != None:
			self.current_bat_msg = msg
		else:
			return

		if self.bat_en:
			self.get_logger().info(f'[Battery]: Bat: {msg.present} | Volt: {msg.voltage} | Temp: {msg.temperature} | Amps: {msg.current}', throttle_duration_sec=self.bat_hz)

def main(args=None):

	rclpy.init(args=args)

	sensors_subscriber = SensorsSubscriber()
	rclpy.spin(sensors_subscriber)
	sensors_subscriber.destroy_node()

	rclpy.shutdown()

if __name__ == '__main__':

	main()


import copy
import warnings
from typing import Optional, Tuple

from vyper import ast as vy_ast
from vyper.lll import compile_lll, optimizer
from vyper.old_codegen import parser
from vyper.old_codegen.global_context import GlobalContext
from vyper.semantics import set_data_positions, validate_semantics
from vyper.typing import InterfaceImports, StorageLayout

class CompilerData:
    """
    Object for fetching and storing compiler data for a Vyper contract.

    This object acts as a wrapper over the pure compiler functions, triggering
    compilation phases as needed and providing the data for use when generating
    the final compiler outputs.

    Attributes
    ----------
    vyper_module : vy_ast.Module
        Top-level Vyper AST node
    vyper_module_folded : vy_ast.Module
        Folded Vyper AST
    global_ctx : GlobalContext
        Sorted, contextualized representation of the Vyper AST
    lll_nodes : LLLnode
        LLL used to generate deployment bytecode
    lll_runtime : LLLnode
        LLL used to generate runtime bytecode
    assembly : list
        Assembly instructions for deployment bytecode
    assembly_runtime : list
        Assembly instructions for runtime bytecode
    bytecode : bytes
        Deployment bytecode
    bytecode_runtime : bytes
        Runtime bytecode
    """

    def __init__(
        self,
        source_code: str,
        contract_name: str = "VyperContract",
        interface_codes: Optional[InterfaceImports] = None,
        source_id: int = 0,
        no_optimize: bool = False,
    ) -> None:
        """
        Initialization method.

        Arguments
        ---------
        source_code : str
            Vyper source code.
        contract_name : str, optional
            The name of the contract being compiled.
        interface_codes: Dict, optional
            Interfaces that may be imported by the contracts during compilation.
            * Formatted as as `{'interface name': {'type': "json/vyper", 'code': "interface code"}}`
            * JSON interfaces are given as lists, vyper interfaces as strings
        source_id : int, optional
            ID number used to identify this contract in the source map.
        """
        self.contract_name = contract_name
        self.source_code = source_code
        self.interface_codes = interface_codes
        self.source_id = source_id
        self.no_optimize = no_optimize

    @property
    def vyper_module(self) -> vy_ast.Module:
        if not hasattr(self, "_vyper_module"):
            self._vyper_module = generate_ast(self.source_code, self.source_id, self.contract_name)

        return self._vyper_module

    @property
    def vyper_module_folded(self) -> vy_ast.Module:
        if not hasattr(self, "_vyper_module_folded"):
            self._vyper_module_folded, self._storage_layout = generate_folded_ast(
                self.vyper_module, self.interface_codes
            )

        return self._vyper_module_folded

    @property
    def storage_layout(self) -> StorageLayout:
        if not hasattr(self, "_storage_layout"):
            self._vyper_module_folded, self._storage_layout = generate_folded_ast(
                self.vyper_module, self.interface_codes
            )

        return self._storage_layout

    @property
    def global_ctx(self) -> GlobalContext:
        if not hasattr(self, "_global_ctx"):
            self._global_ctx = generate_global_context(
                self.vyper_module_folded, self.interface_codes
            )

        return self._global_ctx

    def _gen_lll(self) -> None:
        # fetch both deployment and runtime LLL
        self._lll_nodes, self._lll_runtime = generate_lll_nodes(self.global_ctx, self.no_optimize)

    @property
    def lll_nodes(self) -> parser.LLLnode:
        if not hasattr(self, "_lll_nodes"):
            self._gen_lll()
        return self._lll_nodes

    @property
    def lll_runtime(self) -> parser.LLLnode:
        if not hasattr(self, "_lll_runtime"):
            self._gen_lll()
        return self._lll_runtime

    @property
    def assembly(self) -> list:
        if not hasattr(self, "_assembly"):
            self._assembly = generate_assembly(self.lll_nodes)
        return self._assembly

    @property
    def assembly_runtime(self) -> list:
        if not hasattr(self, "_assembly_runtime"):
            self._assembly_runtime = generate_assembly(self.lll_runtime)
        return self._assembly_runtime

    @property
    def bytecode(self) -> bytes:
        if not hasattr(self, "_bytecode"):
            self._bytecode = generate_bytecode(self.assembly)
        return self._bytecode

    @property
    def bytecode_runtime(self) -> bytes:
        if not hasattr(self, "_bytecode_runtime"):
            self._bytecode_runtime = generate_bytecode(self.assembly_runtime)
        return self._bytecode_runtime

def generate_ast(source_code: str, source_id: int, contract_name: str) -> vy_ast.Module:
    """
    Generate a Vyper AST from source code.

    Arguments
    ---------
    source_code : str
        Vyper source code.
    source_id : int
        ID number used to identify this contract in the source map.
    contract_name : str
        Name of the contract.

    Returns
    -------
    vy_ast.Module
        Top-level Vyper AST node
    """
    return vy_ast.parse_to_ast(source_code, source_id, contract_name)

def generate_folded_ast(
    vyper_module: vy_ast.Module, interface_codes: Optional[InterfaceImports]
) -> Tuple[vy_ast.Module, StorageLayout]:
    """
    Perform constant folding operations on the Vyper AST.

    Arguments
    ---------
    vyper_module : vy_ast.Module
        Top-level Vyper AST node

    Returns
    -------
    vy_ast.Module
        Folded Vyper AST
    StorageLayout
        Layout of variables in storage
    """
    vy_ast.validation.validate_literal_nodes(vyper_module)

    vyper_module_folded = copy.deepcopy(vyper_module)
    vy_ast.folding.fold(vyper_module_folded)
    validate_semantics(vyper_module_folded, interface_codes)
    vy_ast.expansion.expand_annotated_ast(vyper_module_folded)
    symbol_tables = set_data_positions(vyper_module_folded)

    return vyper_module_folded, symbol_tables

def generate_global_context(
    vyper_module: vy_ast.Module,
    interface_codes: Optional[InterfaceImports],
) -> GlobalContext:
    """
    Generate a contextualized AST from the Vyper AST.

    Arguments
    ---------
    vyper_module : vy_ast.Module
        Top-level Vyper AST node
    interface_codes: Dict, optional
        Interfaces that may be imported by the contracts.

    Returns
    -------
    GlobalContext
        Sorted, contextualized representation of the Vyper AST
    """
    return GlobalContext.get_global_context(vyper_module, interface_codes=interface_codes)

def generate_lll_nodes(
    global_ctx: GlobalContext, no_optimize: bool
) -> Tuple[parser.LLLnode, parser.LLLnode]:
    """
    Generate the intermediate representation (LLL) from the contextualized AST.

    This phase also includes LLL-level optimizations.

    This function returns two values, one for generating deployment bytecode and
    the other for generating runtime bytecode. The remaining compilation phases
    may be called with either value, depending on the desired final output.

    Arguments
    ---------
    global_ctx : GlobalContext
        Contextualized Vyper AST

    Returns
    -------
    (LLLnode, LLLnode)
        LLL to generate deployment bytecode
        LLL to generate runtime bytecode
    """
    lll_nodes, lll_runtime = parser.parse_tree_to_lll(global_ctx)
    if not no_optimize:
        lll_nodes = optimizer.optimize(lll_nodes)
        lll_runtime = optimizer.optimize(lll_runtime)
    return lll_nodes, lll_runtime

def generate_assembly(lll_nodes: parser.LLLnode, no_optimize: bool = False) -> list:
    """
    Generate assembly instructions from LLL.

    Arguments
    ---------
    lll_nodes : str
        Top-level LLL nodes. Can be deployment or runtime LLL.

    Returns
    -------
    list
        List of assembly instructions.
    """
    assembly = compile_lll.compile_to_assembly(lll_nodes, no_optimize=no_optimize)

    if _find_nested_opcode(assembly, "DEBUG"):
        warnings.warn(
            "This code contains DEBUG opcodes! The DEBUG opcode will only work in "
            "a supported EVM! It will FAIL on all other nodes!"
        )
    return assembly

def _find_nested_opcode(assembly, key):
    if key in assembly:
        return True
    else:
        sublists = [sub for sub in assembly if isinstance(sub, list)]
        return any(_find_nested_opcode(x, key) for x in sublists)

def generate_bytecode(assembly: list) -> bytes:
    """
    Generate bytecode from assembly instructions.

    Arguments
    ---------
    assembly : list
        Assembly instructions. Can be deployment or runtime assembly.

    Returns
    -------
    bytes
        Final compiled bytecode.
    """
    return compile_lll.assembly_to_evm(assembly)[0]


import numpy as np
import networkx as nx
import random
import timeit
import gym
import matplotlib.pyplot as plt
from utils import get_node_values
from utils import interventional_selection

env = gym.make('Taxi-v3')
n_actions = env.action_space.n
n_states = env.observation_space.n

qtable = np.zeros((n_states,n_actions))

n_episodes = 1000
n_steps = 100
epsilon = 1
max_epsilon = 1
min_epsilon = 0.01
decay_rate = 0.01
alpha = 0.8
gamma = 0.9
average_training_rewards = []
flag = 0
steps = []
G = nx.read_graphml('taxi_graph.graphml')

for episode in range(n_episodes):
	state = env.reset()
	total_reward = 0
	for step in range(n_steps):
		flag_a = 1
		observables = [i for i in env.decode(state)]
		V = get_node_values(observables)
		action = interventional_selection(state, G, V)
		#print(action)
		if(action == None):
			flag_a = 0
			if(epsilon > np.random.uniform(0,1)):
				while(action == None or action >= 4):
					action = env.action_space.sample()
				#print(action)
			else:
				action = np.argmax(qtable[state,:])
				#print(action)
		next_state, reward, done, info = env.step(action)
		#print(reward)
		total_reward += reward
		qtable[state, action] = qtable[state, action] + alpha * (reward + gamma * np.max(qtable[next_state, :]) - qtable[state, action])
		state = next_state

		if(done):
			if(flag == 0 and total_reward >= 9):
				optimum_reward_ep = episode+1
				flag = 1
			#print('Episode_'+str(episode+1)+' Total reward: '+str(total_reward))
			break

	average_training_rewards.append(total_reward/step+1)
	steps.append(step)

	if(flag_a == 0):
		epsilon = min_epsilon + (max_epsilon - min_epsilon)*np.exp(-decay_rate*episode)

	#if(episode%10 == 0):
	#	print('Average reward at episode ' + str(episode+1) + ' : ' +str(sum(average_training_rewards[:-10])/10))

print('Optimum reward achieved at episode: ' +str(optimum_reward_ep))

plt.ylim(-5,5)
plt.xlabel('Episode number')
plt.ylabel('Total reward')
plt.plot(average_training_rewards)
#plt.plot([i for i in range(n_episodes)], steps)

plt.savefig('observations/taxi_causal_greedy_axisfixed.png')


from random import randint, shuffle

import discord
import modules.math.strmath as strmath
import modules.math.wolfram as wolfram

import modules.math.latex as latex
from command import command
from util import client, theme_color, error_color
from logger import log

# import urllib.parse

@command('math')
async def math(message, args):
    result = strmath.evaluate(args)
    if result.type == 'error':
        if result.data.traceback != '':
            embed = discord.Embed(color=error_color, title=result.data.message, description='```\n%s```' % result.data.traceback)
        else:
            embed = discord.Embed(color=error_color, title=result.data.message)
    else:
        embed = discord.Embed(color=theme_color, title='Result', description=result.data)
    await client.send_message(message.channel, embed=embed)

@command('rand')
async def rand(message, args):
    try:
        if ',' in args:
            split_args = args.split(',')
            a, b = int(split_args[0].strip()), int(split_args[1].strip())
            await client.send_message(message.channel, str(randint(a, b)))
        else:
            await client.send_message(message.channel, str(randint(1, int(args))))
    except (TypeError, ValueError):
        await client.send_message(message.channel, '**Invalid position(s).**')

@command('shuffle')
async def shuffle_text(message, args):
    lst = [x.strip(',') for x in args.split(' ')]
    shuffle(lst)
    await client.send_message(message.channel, ', '.join(lst))

@command('tex')
async def generate_latex(message, args):
    code = args.strip('`')
    try:
        image = latex.generate(code)
    except Exception as e:
        await client.send_message(message.channel, embed=discord.Embed(color=error_color, description=str(e)))
        return
    await client.send_file(message.channel, image, filename='latex.png')

@command('wolfram')
async def wolfram_alpha(message, args):
    try:
        result = wolfram.query(args)
        image = wolfram.join_images(result)
        await client.send_file(message.channel, image, filename='wolfram.png')
        icon_url = 'https://cdn.iconscout.com/public/images/icon/free/png-512/wolfram-alpha-logo-3225dad81e65984f-512x512.png'
        embed = discord.Embed(color=16039746)
        embed.set_footer(text='Powered by Wolfram | Alpha Computational Knowledge Engine', icon_url=icon_url)
        await client.send_message(message.channel, embed=embed)
    except Exception as e:
        await client.send_message(message.channel, embed=discord.Embed(color=error_color, description=str(e)))

@command('equation')
async def generate_equation(message, args):
    try:
        image = latex.generate_eqn(args)
        await client.send_file(message.channel, image, filename='equation.png')
    except Exception as e:
        log(message.server.id, str(e))
        await client.send_message(message.channel, embed=discord.Embed(color=error_color, description='Failed to generate equation.'))


import sys
import json
import sql
from dateutil import parser as dt

def getPackageId(package, ecosystem):
    ''' returns packageId if exists, else creates '''
    selectQ = 'select id from package where name=%s and ecosystem=%s'
    results = sql.execute(selectQ,(package,ecosystem))
    if not results:
        insertQ = 'insert into package values (null,%s,%s)'
        sql.execute(insertQ,(package,ecosystem))
        results = sql.execute(selectQ,(package,ecosystem))
    return results[0]['id']

with open("../snyk/data/ghsa.json") as file:
    data = json.load(file)

for advisory in data:

    id = advisory['ghsaId']
    score = advisory['cvss'].get('score',None)
    vector = advisory['cvss'].get('vectorString',None)

    cve = None
    for e in advisory['identifiers']:
        if e['type']=='CVE':
            if e['value'] != '':
                cve=e['value']
                assert cve.count('CVE') == 1

    origin = advisory['origin']
    publish = dt.parse(advisory['publishedAt'])
    severity = advisory['severity']
    summary = advisory['summary']
    withdrawn =  advisory['withdrawnAt']
    if withdrawn:
        withdrawn = dt.parse(withdrawn)

    q='insert into advisory values(%s,%s,%s,%s,%s,%s,%s,%s,%s)'
    print(id,score,vector,cve,origin,publish,severity, summary,withdrawn)
    sql.execute(q,(id,score,vector,cve,origin,publish,severity, summary,withdrawn))

    for e in advisory['vulnerabilities']['edges']:
        data = e['node']
        package = data['package']['name']
        ecosystem = data['package']['ecosystem']
        packageId = getPackageId(package, ecosystem)
        if data['firstPatchedVersion'] is None:
            patch = None
        else:
            patch = data['firstPatchedVersion']['identifier']
        range = data['vulnerableVersionRange']
        q='insert into advisory_package values(%s,%s,%s,%s)'
        print(id,packageId,patch,range)
        sql.execute(q,(id,packageId,patch,range))


import requests

api_url = 'http://3.109.224.64:8080'

def turn_motor_off():
    try:
        data = {'status': 'off'}
        response = requests.post(f'{api_url}/motor/update', json=data)
        response_data = response.json()
        return response_data
    except Exception as e:
        return {'error': str(e)}

if __name__ == '__main__':
    motor_status = turn_motor_off()
    print('Motor Status After Turning Off:', motor_status)


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

import pika
from multiprocessing import Process, current_process
from settings import *

class FlowCollect(Process):
    """流量收集类"""
    def __init__(self, url, instance):
        super(FlowCollect, self).__init__()
        self.url = url
        self.instance = instance
        self.name = instance["id"]
        self.flag = True

    def run(self):
        flowID = -1
        headers = {"Accept": "application/json"}
        connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.1.180'))
        channel = connection.channel()
        channel.queue_declare(queue='flow', durable=True)
        while True and self.flag:
            r = requests.get(self.url + "&flowID=" + str(flowID), headers=headers, timeout=3)
            if r.status_code != 200:
                break
            flows = r.json()
            if len(flows) == 0:
                connection.sleep(1)
                continue
            flowID = flows[0]["flowID"]
            flows_insert = []
            for f in flows:
                flow = {}
                flow["tenant_id"] = self.instance["tenant_id"]
                flow["tenant_name"] = self.instance["tenant_name"]
                flow["instance_id"] = self.instance["id"]
                flow["instance_name"] = self.instance["name"]
                flow["port_id"] = self.instance["interfaceAttachments"][0]["port_id"]
                flow["mac_addr"] = self.instance["interfaceAttachments"][0]["mac_addr"].replace(":", "").upper()
                flow["fixed_ip"] = self.instance["interfaceAttachments"][0]["fixed_ips"][0]["ip_address"]
                for ips in self.instance["addresses"].values():
                    if len(ips) == 2:
                        for ip in ips:
                            if ip["OS-EXT-IPS:type"] == "floating":
                                flow["floating_ip"] = ip["addr"]
                    else:
                        flow["floating_ip"] = "0.0.0.0"

                flow["hypervisor_name"] = self.instance["OS-EXT-SRV-ATTR:hypervisor_hostname"]
                flow["timestap"] = f["end"]
                flowkeys = f["flowKeys"].split(',')
                flow["macsource"] = flowkeys[0]
                flow["macdestination"] = flowkeys[1]
                flow["ipsource"] = flowkeys[2]
                flow["ipdestination"] = flowkeys[3]
                flow["ipprotocol"] = int(flowkeys[4])
                flow["size"] = float(f["value"])
                flow["srcport_or_icmptype"] = int(flowkeys[5])
                flow["dstport_or_icmpcode"] = int(flowkeys[6])
                flow["tcpflags"] = flowkeys[7]
                flow["httpmethod"] = flowkeys[8]
                flow["extension"] = "{}"
                flows_insert.append(flow)

            channel.basic_publish(exchange='',
                                  routing_key='flow',
                                  body=json.dumps(flows_insert),
                                  properties=pika.BasicProperties(
                                      delivery_mode=2,  # make message persistent
                                      )
                                  )
            # print " [%s] Sent %r" % (current_process().pid, json.dumps(flows_insert))
            connection.sleep(1)
        channel.close()
        connection.close()


# © 2021 Solvos Consultoría Informática (<http://www.solvos.es>)
# License LGPL-3.0 (https://www.gnu.org/licenses/lgpl-3.0.html)

from odoo import models

class StockPicking(models.Model):
    _inherit = "stock.picking"

    def lines_category_product(self):
        self.ensure_one()
        categories = {}

        category_attribute = self.env.ref("stock_picking_eco_tag.product_attribute_category").id
        for product in self.move_line_ids.mapped("product_id"):
            categories[product.id] = ""
            for attribute in product.attribute_line_ids.filtered(
                lambda x: x.attribute_id.id == category_attribute
            ):
                categories[product.id] = attribute.value_ids and attribute.value_ids[0].name or ""

        return categories


from pydicom import dcmread
import numpy as np
from scipy.sparse import csc_matrix
import matplotlib.pyplot as plt
import math
from mpl_toolkits.mplot3d import Axes3D  # noqa: F401 unused import
import pandas as pd
import scipy.stats as stats

def fun(contour_dataset, image, zcoord):
    img_ID = image.SOPInstanceUID
    img = image
    img_arr = img.pixel_array
    x_spacing, y_spacing = float(img.PixelSpacing[0]), float(img.PixelSpacing[1])
    origin_x, origin_y, _ = img.ImagePositionPatient
    rows = []
    cols = []
    #массивы для хранения номеров контуров
    CTNumbers = []
    T1Numbers = []
    T2Numbers = []
    #массивы контуров
    CTX = []
    CTY = []
    T1X = []
    T1Y = []
    T2X = []
    T2Y = []
    #буфер
    CTXT = []
    CTYT = []
    T1XT = []
    T1YT = []
    T2XT = []
    T2YT = []
    #массивы для центров контуров
    CTXPoints = []
    T1XPoints = []
    T2XPoints = []
    CTYPoints = []
    T1YPoints = []
    T2YPoints = []
    #инициализация графиков
    fig, ax = plt.subplots(1, figsize=(60, 50))
    #заполнение массивов номерами контуров, чтобы произвести дальнейшее распределение контуров по своим массивам, согласно их номераам
    for ObservationT in contour_dataset.RTROIObservationsSequence:
        if 'CT' in ObservationT.ROIObservationLabel:
            CTNumbers.append(int(ObservationT.ReferencedROINumber))
        if 'T1' in ObservationT.ROIObservationLabel:
            T1Numbers.append(int(ObservationT.ReferencedROINumber))
        if 'T2' in ObservationT.ROIObservationLabel:
            T2Numbers.append(int(ObservationT.ReferencedROINumber))
#обработка данных
    for ROIContourSequenceNumber in contour_dataset.ROIContourSequence:
        for t in range(0, len(ROIContourSequenceNumber.ContourSequence)):
            contour_coordinates = ROIContourSequenceNumber.ContourSequence[t].ContourData
            # x, y, z координаты контура в миллиметрах
            x0 = contour_coordinates[len(contour_coordinates) - 3]
            y0 = contour_coordinates[len(contour_coordinates) - 2]
            z0 = contour_coordinates[len(contour_coordinates) - 1]
            coordinates = []
            if (contour_coordinates[2] == zcoord):
                tempry = -1
                #преобразование
                for i in range(0, len(contour_coordinates), 3):
                    x = contour_coordinates[i]
                    y = contour_coordinates[i + 1]
                    z = contour_coordinates[i + 2]
                    l = math.sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0) + (z - z0) * (z - z0))
                    l = math.ceil(l * 2) + 1
                    for k in range(1, l + 1):
                        coordinates.append([(x - x0) * k / l + x0, (y - y0) * k / l + y0, (z - z0) * k / l + z0])
                    x0 = x
                    y0 = y
                    z0 = z
                    #заполнение массивов контуров CT T1 T2
                    if ROIContourSequenceNumber.ReferencedROINumber in CTNumbers:
                        CTX.append((x - origin_x) / x_spacing)
                        CTY.append((y - origin_y) / y_spacing)
                        CTXT.append((x - origin_x) / x_spacing)
                        CTYT.append((y - origin_y) / y_spacing)
                        #этот блок ниже отвечает за прорисовку контура
                        if tempry != ROIContourSequenceNumber.ReferencedROINumber:
                            xt = CTXT[-1]
                            yt = CTYT[-1]
                            CTXT.pop() #ПОСЛЕДНИЙ ЭЛЕМЕНТ НАДО ПЕРЕНЕСТИ В НАЧАЛО СЛЕДУЮЩЕГО КОНТУРА
                            CTYT.pop()
                            # на ноль делить нельзя, поэтому откидываем эти варианты (это не выкинет точки, все на месте)
                            if len(CTXT) != 0:
                                CTXPoints.append(sum(CTXT) / len(CTXT))
                                CTYPoints.append(sum(CTYT) / len(CTYT))
                            # if len(CTXT) == 0:
                            #     CTXPoints.append(sum(CTX) / len(CTX))
                            # if len(CTYT) == 0:
                            #     CTYPoints.append(sum(CTY) / len(CTY))
                            ax.plot(CTXT, CTYT, color='g', linestyle='-')
                            CTXT.clear()
                            CTYT.clear()
                            CTXT.append(xt)
                            CTYT.append(yt)
                    if ROIContourSequenceNumber.ReferencedROINumber in T1Numbers:
                        T1X.append((x - origin_x) / x_spacing)
                        T1Y.append((y - origin_y) / y_spacing)
                        T1XT.append((x - origin_x) / x_spacing)
                        T1YT.append((y - origin_y) / y_spacing)
                        # этот блок ниже отвечает за прорисовку контура
                        if tempry != ROIContourSequenceNumber.ReferencedROINumber:
                            xt = T1XT[-1]
                            yt = T1YT[-1]
                            T1XT.pop()
                            T1YT.pop()
                            #на ноль делить нельзя, поэтому откидываем эти варианты (это не выкинет точки, все на месте)
                            if len(T1XT) != 0 :
                                T1XPoints.append(sum(T1XT) / len(T1XT))
                                T1YPoints.append(sum(T1YT) / len(T1YT))
                            # if len(T1XT) == 0:
                            #     T1XPoints.append(sum(T1X) / len(T1X))
                            # if len(CTYT) == 0:
                            #     T1YPoints.append(sum(T1Y) / len(T1Y))
                            ax.plot(T1XT, T1YT, color='r', linestyle='-')
                            T1XT.clear()
                            T1YT.clear()
                            T1XT.append(xt)
                            T1YT.append(yt)
                    if ROIContourSequenceNumber.ReferencedROINumber in T2Numbers:
                        T2X.append((x - origin_x) / x_spacing)
                        T2Y.append((y - origin_y) / y_spacing)
                        T2XT.append((x - origin_x) / x_spacing)
                        T2YT.append((y - origin_y) / y_spacing)
                        # этот блок ниже отвечает за прорисовку контура
                        if tempry != ROIContourSequenceNumber.ReferencedROINumber:
                            xt = T2XT[-1]
                            yt = T2YT[-1]
                            T2XT.pop()
                            T2YT.pop()
                            # на ноль делить нельзя, поэтому откидываем эти варианты (это не выкинет точки, все на месте)
                            if len(T2XT) != 0:
                                T2XPoints.append(sum(T2XT) / len(T2XT))
                                T2YPoints.append(sum(T2YT) / len(T2YT))
                            # if len(T2XT) == 0:
                            #     T2XPoints.append(sum(T2X) / len(T2X))
                            # if len(CTYT) == 0:
                            #     T2YPoints.append(sum(T2Y) / len(T2Y))
                            ax.plot(T2XT, T2YT, color='b', linestyle='-')
                            T2XT.clear()
                            T2YT.clear()
                            T2XT.append(xt)
                            T2YT.append(yt)
                    tempry = ROIContourSequenceNumber.ReferencedROINumber
            pixel_coordinates = [(np.round((y - origin_y) / y_spacing), np.round((x - origin_x) / x_spacing)) for x, y, _ in coordinates]
            for i, j in list(set(pixel_coordinates)):
                rows.append(i)
                cols.append(j)
            contour_arr = csc_matrix((np.ones_like(rows), (rows, cols)), dtype=np.int8, shape=(img_arr.shape[0], img_arr.shape[1])).toarray()

    #графики для центров контуров
    plt.scatter(CTXPoints, CTYPoints, color='g', marker='.')
    plt.scatter(T1XPoints, T1YPoints, color='r', marker='.')
    plt.scatter(T2XPoints, T2YPoints, color='b', marker='.')
    # fig = plt.gcf()
    plt.show()
    # plt.draw()
    # fig.savefig('contours.png', dpi=300)
    plt.figure(1)
    # обсчет точек по тройкам CT-T1 CT-T2 T1-T2
    distancesCTT1 = []
    distancesCTT2 = []
    # ручная обработка первых трех выбивающихся точек (не очень сработало :с )
    # xa = CTXPoints[0]
    # ya = CTYPoints[0]
    # xb = CTXPoints[1]
    # yb = CTYPoints[1]
    #
    # CTXPoints[0] = CTXPoints[2]
    # CTYPoints[0] = CTYPoints[2]
    # CTXPoints[2] = xa
    # CTYPoints[2] = ya
    # CTXPoints[1] = CTXPoints[0]
    # CTYPoints[1] = CTYPoints[0]
    # CTXPoints[0] = xb
    # CTYPoints[0] = yb
    # начинается не от 0, а от 3, потому что отброшены первые три значения
    for i in range(3, len(T1XPoints)):
        distancesCTT1.append(math.sqrt((CTXPoints[i] - T1XPoints[i]) ** 2 + (CTYPoints[i] - T1YPoints[i]) ** 2))
    for i in range(3, len(T2XPoints)):
        distancesCTT2.append(math.sqrt((CTXPoints[i] - T2XPoints[i]) ** 2 + (CTYPoints[i] - T2YPoints[i]) ** 2))
    print(distancesCTT1)
    print(len(distancesCTT1), "/ 88")
    print(distancesCTT2)
    print(len(distancesCTT2), "/ 88")
    meandistanceCTT1 = np.mean(distancesCTT1)
    meandistanceCTT2 = np.mean(distancesCTT2)
    print("mean distance CT T1 = ", round(meandistanceCTT1, 5), " mm")
    print("max distance CT T1 = ", round(max(distancesCTT1), 5), " mm")
    print("min distance CT T1 = ", round(min(distancesCTT1), 5), " mm")

    print("mean distance CT T2 = ", round(meandistanceCTT2, 5), " mm")
    print("max distance CT T2 = ", round(max(distancesCTT2), 5), " mm")
    print("min distance CT T2 = ", round(min(distancesCTT2), 5), " mm")

    # графики для отклонений T1 и T2 от СТ
    d = dict(T1 = np.array(distancesCTT1), T2 = np.array(distancesCTT2))
    df = pd.DataFrame(dict([(k, pd.Series(v)) for k, v in d.items()]))

    # df = pd.DataFrame({
    #     'T1': distancesCTT1,
    #     'T2': distancesCTT2,
    # })
    df = df.plot.kde()
    plt.show()

    return img_arr, contour_arr, img_ID, CTX, CTY, T1X, T1Y, T2X, T2Y

#найти все доступные координаты z контуров
def zcoords(contour_dataset):
    print("Possible z coordinates")
    possibleZ = []
    for q in range(0, len(contour_dataset.ROIContourSequence)):
        for p in range(0, len(contour_dataset.ROIContourSequence[q].ContourSequence)):
            contour_coordinates = contour_dataset.ROIContourSequence[q].ContourSequence[p].ContourData
            for i in range(0, len(contour_coordinates), 3):
                z = contour_coordinates[i + 2]
                possibleZ.append(z)
    possibleZ = list(set(possibleZ))
    possibleZ.sort()
    for x in possibleZ: print(x)

if __name__ == "__main__":
    #путь до папки
    path = 'C:\\Users\\bzavo\\PycharmProjects\\MainProject\\DICOM\\'
    filename = 'RTSS.dcm'
    image = 'IMG0000000100.dcm'
    ds = dcmread(path + filename)
    image = dcmread(path + image)
    #все данные есть при z=-14
    zcoords(ds)
    print("z = -14 или -24 отлично подходит")
    zcoord = float(input("Enter Z coordinate from the list above \n"))
    t = fun(ds, image, zcoord)


import numpy

from typing import Optional, cast

from UM.Qt.Bindings.Theme import Theme
from UM.Qt.QtApplication import QtApplication
from UM.Logger import Logger

class LayerPolygon:
    NoneType = 0
    Inset0Type = 1
    InsetXType = 2
    SkinType = 3
    SupportType = 4
    SkirtType = 5
    InfillType = 6
    SupportInfillType = 7
    MoveCombingType = 8
    MoveRetractionType = 9
    SupportInterfaceType = 10
    PrimeTowerType = 11
    __number_of_types = 12

    __jump_map = numpy.logical_or(numpy.logical_or(numpy.arange(__number_of_types) == NoneType,
                                                   numpy.arange(__number_of_types) == MoveCombingType),
                                                   numpy.arange(__number_of_types) == MoveRetractionType)

    def __init__(self, extruder: int, line_types: numpy.ndarray, data: numpy.ndarray,
                 line_widths: numpy.ndarray, line_thicknesses: numpy.ndarray, line_feedrates: numpy.ndarray) -> None:
        """LayerPolygon, used in ProcessSlicedLayersJob

        :param extruder: The position of the extruder
        :param line_types: array with line_types
        :param data: new_points
        :param line_widths: array with line widths
        :param line_thicknesses: array with type as index and thickness as value
        :param line_feedrates: array with line feedrates
        """

        self._extruder = extruder
        self._types = line_types
        unknown_types = numpy.where(self._types >= self.__number_of_types, self._types, None)
        if unknown_types.any():
            # Got faulty line data from the engine.
            for idx in unknown_types:
                Logger.warning(f"Found an unknown line type at: {idx}")
                self._types[idx] = self.NoneType
        self._data = data
        self._line_widths = line_widths
        self._line_thicknesses = line_thicknesses
        self._line_feedrates = line_feedrates

        self._vertex_begin = 0
        self._vertex_end = 0
        self._index_begin = 0
        self._index_end = 0

        self._jump_mask = self.__jump_map[self._types]
        self._jump_count = numpy.sum(self._jump_mask)
        self._mesh_line_count = len(self._types) - self._jump_count
        self._vertex_count = self._mesh_line_count + numpy.sum(self._types[1:] == self._types[:-1])

        # Buffering the colors shouldn't be necessary as it is not
        # re-used and can save a lot of memory usage.
        self._color_map = LayerPolygon.getColorMap()
        self._colors = self._color_map[self._types]  # type: numpy.ndarray

        # When type is used as index returns true if type == LayerPolygon.InfillType
        # or type == LayerPolygon.SkinType
        # or type == LayerPolygon.SupportInfillType
        # Should be generated in better way, not hardcoded.
        self._is_infill_or_skin_type_map = numpy.array([0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0], dtype=bool)

        self._build_cache_line_mesh_mask = None  # type: Optional[numpy.ndarray]
        self._build_cache_needed_points = None  # type: Optional[numpy.ndarray]

    def buildCache(self) -> None:
        # For the line mesh we do not draw Infill or Jumps. Therefore those lines are filtered out.
        self._build_cache_line_mesh_mask = numpy.ones(self._jump_mask.shape, dtype = bool)
        self._index_begin = 0
        self._index_end = cast(int, numpy.sum(self._build_cache_line_mesh_mask))

        self._build_cache_needed_points = numpy.ones((len(self._types), 2), dtype = bool)
        # Only if the type of line segment changes do we need to add an extra vertex to change colors
        self._build_cache_needed_points[1:, 0][:, numpy.newaxis] = self._types[1:] != self._types[:-1]
        # Mark points as unneeded if they are of types we don't want in the line mesh according to the calculated mask
        numpy.logical_and(self._build_cache_needed_points, self._build_cache_line_mesh_mask, self._build_cache_needed_points)

        self._vertex_begin = 0
        self._vertex_end = cast(int, numpy.sum(self._build_cache_needed_points))

    def build(self, vertex_offset: int, index_offset: int, vertices: numpy.ndarray,
              colors: numpy.ndarray, line_dimensions: numpy.ndarray, feedrates: numpy.ndarray,
              extruders: numpy.ndarray, line_types: numpy.ndarray, indices: numpy.ndarray) -> None:
        """Set all the arrays provided by the function caller, representing the LayerPolygon

        The arrays are either by vertex or by indices.

        :param vertex_offset: determines where to start and end filling the arrays
        :param index_offset: determines where to start and end filling the arrays
        :param vertices: vertex numpy array to be filled
        :param colors: vertex numpy array to be filled
        :param line_dimensions: vertex numpy array to be filled
        :param feedrates: vertex numpy array to be filled
        :param extruders: vertex numpy array to be filled
        :param line_types: vertex numpy array to be filled
        :param indices: index numpy array to be filled
        """

        if self._build_cache_line_mesh_mask is None or self._build_cache_needed_points is None:
            self.buildCache()

        if self._build_cache_line_mesh_mask is None or self._build_cache_needed_points is None:
            Logger.log("w", "Failed to build cache for layer polygon")
            return

        line_mesh_mask = self._build_cache_line_mesh_mask
        needed_points_list = self._build_cache_needed_points

        # Index to the points we need to represent the line mesh.
        # This is constructed by generating simple start and end points for each line.
        # For line segment n, these are points n and n+1. Row n reads [n n+1]
        # Then the indices for the points we don't need are thrown away based on the pre-calculated list.
        index_list = (numpy.arange(len(self._types)).reshape((-1, 1)) + numpy.array([[0, 1]])).reshape((-1, 1))[needed_points_list.reshape((-1, 1))]

        # The relative values of begin and end indices have already been set in buildCache, so we only need to offset them to the parents offset.
        self._vertex_begin += vertex_offset
        self._vertex_end += vertex_offset

        # Points are picked based on the index list to get the vertices needed.
        vertices[self._vertex_begin:self._vertex_end, :] = self._data[index_list, :]

        # Create an array with colors for each vertex and remove the color data for the points that has been thrown away.
        colors[self._vertex_begin:self._vertex_end, :] = numpy.tile(self._colors, (1, 2)).reshape((-1, 4))[needed_points_list.ravel()]

        # Create an array with line widths and thicknesses for each vertex.
        line_dimensions[self._vertex_begin:self._vertex_end, 0] = numpy.tile(self._line_widths, (1, 2)).reshape((-1, 1))[needed_points_list.ravel()][:, 0]
        line_dimensions[self._vertex_begin:self._vertex_end, 1] = numpy.tile(self._line_thicknesses, (1, 2)).reshape((-1, 1))[needed_points_list.ravel()][:, 0]

        # Create an array with feedrates for each line
        feedrates[self._vertex_begin:self._vertex_end] = numpy.tile(self._line_feedrates, (1, 2)).reshape((-1, 1))[needed_points_list.ravel()][:, 0]

        extruders[self._vertex_begin:self._vertex_end] = self._extruder

        # Convert type per vertex to type per line
        line_types[self._vertex_begin:self._vertex_end] = numpy.tile(self._types, (1, 2)).reshape((-1, 1))[needed_points_list.ravel()][:, 0]

        # The relative values of begin and end indices have already been set in buildCache,
        # so we only need to offset them to the parents offset.
        self._index_begin += index_offset
        self._index_end += index_offset

        indices[self._index_begin:self._index_end, :] = numpy.arange(self._index_end-self._index_begin, dtype=numpy.int32).reshape((-1, 1))
        # When the line type changes the index needs to be increased by 2.
        indices[self._index_begin:self._index_end, :] += numpy.cumsum(needed_points_list[line_mesh_mask.ravel(), 0], dtype = numpy.int32).reshape((-1, 1))
        # Each line segment goes from it's starting point p to p+1, offset by the vertex index.
        # The -1 is to compensate for the necessarily True value of needed_points_list[0,0] which causes an unwanted +1 in cumsum above.
        indices[self._index_begin:self._index_end, :] += numpy.array([self._vertex_begin - 1, self._vertex_begin])

        self._build_cache_line_mesh_mask = None
        self._build_cache_needed_points = None

    def getColors(self):
        return self._colors

    def mapLineTypeToColor(self, line_types: numpy.ndarray) -> numpy.ndarray:
        return self._color_map[line_types]

    def isInfillOrSkinType(self, line_types: numpy.ndarray) -> numpy.ndarray:
        return self._is_infill_or_skin_type_map[line_types]

    def lineMeshVertexCount(self) -> int:
        return self._vertex_end - self._vertex_begin

    def lineMeshElementCount(self) -> int:
        return self._index_end - self._index_begin

    @property
    def extruder(self):
        return self._extruder

    @property
    def types(self):
        return self._types

    @property
    def data(self):
        return self._data

    @property
    def elementCount(self):
        return (self._index_end - self._index_begin) * 2  # The range of vertices multiplied by 2 since each vertex is used twice

    @property
    def lineWidths(self):
        return self._line_widths

    @property
    def lineThicknesses(self):
        return self._line_thicknesses

    @property
    def lineFeedrates(self):
        return self._line_feedrates

    @property
    def jumpMask(self):
        return self._jump_mask

    @property
    def meshLineCount(self):
        return self._mesh_line_count

    @property
    def jumpCount(self):
        return self._jump_count

    def getNormals(self) -> numpy.ndarray:
        """Calculate normals for the entire polygon using numpy.

        :return: normals for the entire polygon
        """

        normals = numpy.copy(self._data)
        normals[:, 1] = 0.0  # We are only interested in 2D normals

        # Calculate the edges between points.
        # The call to numpy.roll shifts the entire array by one
        # so that we end up subtracting each next point from the current, wrapping around.
        # This gives us the edges from the next point to the current point.
        normals = numpy.diff(normals, 1, 0)

        # Calculate the length of each edge using standard Pythagoras
        lengths = numpy.sqrt(normals[:, 0] ** 2 + normals[:, 2] ** 2)
        # The normal of a 2D vector is equal to its x and y coordinates swapped
        # and then x inverted. This code does that.
        normals[:, [0, 2]] = normals[:, [2, 0]]
        normals[:, 0] *= -1

        # Normalize the normals.
        normals[:, 0] /= lengths
        normals[:, 2] /= lengths

        return normals

    __color_map = None  # type: numpy.ndarray

    @classmethod
    def getColorMap(cls) -> numpy.ndarray:
        """Gets the instance of the VersionUpgradeManager, or creates one."""

        if cls.__color_map is None:
            theme = cast(Theme, QtApplication.getInstance().getTheme())
            cls.__color_map = numpy.array([
                theme.getColor("layerview_none").getRgbF(),  # NoneType
                theme.getColor("layerview_inset_0").getRgbF(),  # Inset0Type
                theme.getColor("layerview_inset_x").getRgbF(),  # InsetXType
                theme.getColor("layerview_skin").getRgbF(),  # SkinType
                theme.getColor("layerview_support").getRgbF(),  # SupportType
                theme.getColor("layerview_skirt").getRgbF(),  # SkirtType
                theme.getColor("layerview_infill").getRgbF(),  # InfillType
                theme.getColor("layerview_support_infill").getRgbF(),  # SupportInfillType
                theme.getColor("layerview_move_combing").getRgbF(),  # MoveCombingType
                theme.getColor("layerview_move_retraction").getRgbF(),  # MoveRetractionType
                theme.getColor("layerview_support_interface").getRgbF(),   # SupportInterfaceType
                theme.getColor("layerview_prime_tower").getRgbF()   # PrimeTowerType
            ])

        return cls.__color_map


from preprocessing.loading import *
from utils import *
import logging, os
import gcsfs
from joblib import Parallel, delayed
from tqdm import tqdm
from db import RedisDB
from math import ceil

logging.basicConfig(level = logging.INFO)

GOOGLE_APPLICATION_CREDENTIALS = os.getenv('GOOGLE_APPLICATION_CREDENTIALS')
GOOGLE_PROJECT_ID = os.getenv('GOOGLE_PROJECT_ID')
BELLY_LOCATION = os.getenv('BELLY_LOCATION')
CHUNK_SIZE = int(os.getenv('GUT_CHUNK_SIZE'))
REDIS_HOST = os.getenv('REDIS_HOST')
REDIS_PORT = int(os.getenv('REDIS_PORT'))

def load_tweets(fps, fs, tz):
    db = RedisDB(REDIS_HOST, REDIS_PORT)
    tweets = get_tweets(fps, fs, tz)
    db.load(tweets)

def process(limit=None, mode='parallel'):
    fs = gcsfs.GCSFileSystem(project=GOOGLE_PROJECT_ID,
                             token=GOOGLE_APPLICATION_CREDENTIALS,
                             access='read_write')
    fps = sorted(fs.ls(BELLY_LOCATION))

    tz = 'Asia/Kolkata'
    fps = filter_start_time(fps, tz)

    if limit:
        fps = fps[:int(limit)]

    logging.info(f'Processing {len(fps)} files')

    if mode == 'parallel':
        Parallel(n_jobs=-1)(delayed(load_tweets)(list(f), fs, tz)
                            for f in tqdm(chunk(CHUNK_SIZE, fps),
                                          total=ceil(len(fps)/CHUNK_SIZE)))
    else:
        load_tweets(fps, fs, tz)

    logging.info(f'Loaded all tweets')

from clize import run
if __name__ == '__main__':
    run(process)


import math
import sys
import time
from typing import Tuple
from codebase.real_world.base.getters import Getters
from codebase.real_world.base.senders import Senders
from socket import socket
from codebase.real_world.base.base_client import BaseClient

class PTP(BaseClient):
    def __init__(self, host: str, port: int, trans: Tuple, sock: socket) -> None:
        super().__init__(host, port, trans)

        self.set_socket(sock)
        self.sender = Senders(host, port, trans, sock)
        self.getter = Getters(host, port, trans, sock)

    def _send(self, data: str):
        data = data + "\n"
        self.send(data)
        message = self.receive()
        # print(message)
        # sys.stdout.flush()
        time.sleep(0.05)

    def awaitConfirmation(self):
        message = self.sock.recv(1024).decode("utf-8")
        # print(message)
        sys.stdout.flush()

    ## Arc motions
    def movePTPArc_AC(self, theta, c, k, vel):
        # print(theta)
        # print(c)
        # print(k)
        # print(vel)
        assert len(c) == 3, "Center of circle should be an array of 3 elements"
        assert len(k) == 3, "Orientation vector should be an array of 3 elements"
        assert len(theta) == 1, "Angle of an arc should be a scalar"
        assert len(vel) == 1, "Relative velocity should be a scalar"

        theta_ = theta[0]
        pos = self.getter.get_EEF_pos()
        r_2 = (
            math.pow(c[0] - pos[0], 2)
            + math.pow(c[1] - pos[1], 2)
            + math.pow(c[2] - pos[2], 2)
        )
        r = math.pow(r_2, 0.5)

        assert r != 0, "Radius can not be zero"
        assert theta_ != 0, "Angle can not be zero"

        # calculate unit vector
        r_2 = math.pow(k[0], 2) + math.pow(k[1], 2) + math.pow(k[2], 2)
        normK = math.pow(r_2, 0.5)
        assert normK != 0, "Norm of direction vector k shall not be zero"

        k[0] = k[0] / normK
        k[1] = k[1] / normK
        k[2] = k[2] / normK
        # print('k normalized successfully')
        s = [c[0] - pos[0], c[1] - pos[1], c[2] - pos[2]]
        s[0] = -s[0] / r
        s[1] = -s[1] / r
        s[2] = -s[2] / r
        n = [
            (k[1] * s[2] - k[2] * s[1]),
            (k[2] * s[0] - k[0] * s[2]),
            (k[0] * s[1] - k[1] * s[0]),
        ]
        # print('Performing arc motion based on c1 and c2')
        angle = theta_ / 2
        c1 = self.rotTheThing(angle, r, s, n, c)
        angle = theta_
        c2 = self.rotTheThing(angle, r, s, n, c)
        for i in range(3, 6):
            c1.append(pos[i])
            c2.append(pos[i])
        self.movePTPCirc1OrintationInter(c1, c2, vel)

    def rotTheThing(self, theta, r, s, n, c):
        c1 = [0, 0, 0]
        cos_ = math.cos(theta)
        sin_ = math.sin(theta)
        c1[0] = r * cos_ * s[0] + r * sin_ * n[0] + c[0]
        c1[1] = r * cos_ * s[1] + r * sin_ * n[1] + c[1]
        c1[2] = r * cos_ * s[2] + r * sin_ * n[2] + c[2]
        print(c1)
        return c1

    def checkErrorInRelVel(self, relVel):
        if not isinstance(relVel, (int, float)):
            print("Relative velocity should be a scalar")
            return True
        if not 0 < relVel < 1:
            print("Relative velocity should be in the range (0, 1)")
            return True
        return False

    def movePTPArcXY_AC(self, theta, c, vel):
        assert (
            len(theta) == 1
        ), "Error in function [movePTPArcXY_AC]: Rotation angle should be a scalar"
        assert (
            len(c) == 2
        ), "Error in function [movePTPArcXY_AC]: Center of rotation should be an array of two elements [x, y]"
        assert (
            len(vel) == 1
        ), "Error in function [movePTPArcXY_AC]: Velocity should be a scalar"
        k = [0, 0, 1]
        pos = self.getter.get_EEF_pos()
        c1 = [c[0], c[1], pos[2]]
        self.movePTPArc_AC(theta, c1, k, vel)

    def movePTPArcXZ_AC(self, theta, c, vel):
        assert (
            len(theta) == 1
        ), "Error in function [movePTPArcXZ_AC]: Rotation angle should be a scalar"
        assert (
            len(c) == 2
        ), "Error in function [movePTPArcXZ_AC]: Center of rotation should be an array of two elements [x, z]"
        assert (
            len(vel) == 1
        ), "Error in function [movePTPArcXZ_AC]: Velocity should be a scalar"

        k = [0, 1, 0]
        pos = self.getter.get_EEF_pos()
        c1 = [c[0], pos[1], c[1]]
        self.movePTPArc_AC(theta, c1, k, vel)

    def movePTPArcYZ_AC(self, theta, c, vel):
        assert (
            len(theta) == 1
        ), "Error in function [movePTPArcYZ_AC]: Rotation angle should be a scalar"
        assert (
            len(c) == 2
        ), "Error in function [movePTPArcYZ_AC]: Center of rotation should be an array of two elements [x, z]"
        assert (
            len(vel) == 1
        ), "Error in function [movePTPArcYZ_AC]: Velocity should be a scalar"

        k = [1, 0, 0]
        pos = self.getter.get_EEF_pos()
        c1 = [pos[0], c[1], c[2]]
        self.movePTPArc_AC(theta, c1, k, vel)

    def movePTPCirc1OrintationInter(self, f1, f2, relVel):
        assert (
            len(f1) == 6
        ), "Error in function [movePTPCirc1OrintationInter]: The first frame should be an array of 6 elements [x, y, z, alpha, beta, gamma]"
        assert (
            len(f2) == 6
        ), "Error in function [movePTPCirc1OrintationInter]: The second frame should be an array of 6 elements [x, y, z, alpha, beta, gamma]"
        assert (
            len(relVel) == 1
        ), "Error in function [movePTPCirc1OrintationInter]: Relative velocity should be a scalar"

        buff = "jRelVel_"
        buff = buff + str(relVel[0])
        buff = buff + "_"
        self._send(buff)
        self.sender.sendCirc1FramePos(f1)
        self.sender.sendCirc2FramePos(f2)
        theCommand = "doPTPinCSCircle1_"
        self._send(theCommand)
        self.awaitConfirmation()  # bug fixed on 1st October 2019, awaiting end of blocking motion

    def movePTPLineEEF(self, pos, vel):
        assert (
            len(vel) == 1
        ), "Error in function [movePTPLineEEF]: Velocity shall be a scalar"
        assert (
            len(pos) == 6
        ), "Error in function [movePTPLineEEF]: Position should be an array of 6 elements"

        buff = "jRelVel_" + str(vel[0]) + "_"
        command = buff
        self._send(command)
        self.sender.sendEEfPositions(pos)
        theCommand = "doPTPinCS"
        self._send(theCommand)
        self.awaitConfirmation()  # bug fixed on 1st October 2019, awaiting end of blocking motion

    def movePTPLineEefRelEef(self, pos, vel):
        assert (
            len(vel) == 1
        ), "Error in function [movePTPLineEefRelEef]: Velocity should be a scalar"
        assert (
            len(pos) == 3
        ), "Error in function [movePTPLineEefRelEef]: Position should be an array of 3 elements [x, y, z]"

        buff = "jRelVel_" + str(vel[0]) + "_"
        command = buff
        self._send(command)

        newPos = [0, 0, 0, 0, 0, 0]
        newPos[0] = pos[0]
        newPos[1] = pos[1]
        newPos[2] = pos[2]

        self.sender.sendEEfPositions(newPos)

        theCommand = "doPTPinCSRelEEF"
        self._send(theCommand)
        self.awaitConfirmation()  # bug fixed on 1st October 2019, awaiting end of blocking motion

    def movePTPLineEefRelBase(self, pos, vel):
        assert len(pos) == 3, "Position should be an array of three elements [x, y, z]"
        assert len(vel) == 1, "Velocity should be a scalar"

        buff = "jRelVel_" + str(vel[0]) + "_"
        command = buff
        self._send(command)

        newPos = [0, 0, 0, 0, 0, 0]
        newPos[0] = pos[0]
        newPos[1] = pos[1]
        newPos[2] = pos[2]

        self.sender.sendEEfPositions(newPos)

        theCommand = "doPTPinCSRelBase"
        self._send(theCommand)
        self.awaitConfirmation()  # bug fixed on 1st October 2019, awaiting end of blocking motion

    # joint space
    def movePTPJointSpace(self, jpos, relVel):
        assert (
            len(jpos) == 7
        ), "Error in function [movePTPJointSpace]: Joints positions shall be an array of 7 elements"
        assert (
            len(relVel) == 1
        ), "Error in function [movePTPJointSpace]: Relative velocity should be a scalar"

        buff = "jRelVel_" + str(relVel[0]) + "_"
        command = buff
        self._send(command)
        self.sender.sendJointsPositions(jpos)
        theCommand = "doPTPinJS"
        self._send(theCommand)
        self.awaitConfirmation()

    def movePTPHomeJointSpace(self, relVel):
        assert (
            len(relVel) == 1
        ), "Error in function [movePTPHomeJointSpace]: Relative velocity should be a scalar"

        buff = "jRelVel_" + str(relVel[0]) + "_"
        command = buff
        self._send(command)
        jpos = [0, 0, 0, 0, 0, 0, 0]
        self.sender.sendJointsPositions(jpos)
        theCommand = "doPTPinJS"
        self._send(theCommand)
        self.awaitConfirmation()

    def movePTPTransportPositionJointSpace(self, relVel):
        assert (
            len(relVel) == 1
        ), "Error in function [movePTPTransportPositionJointSpace]: Relative velocity should be a scalar"
        jpos = [0, 0, 0, 0, 0, 0, 0]
        jpos[3] = 25 * math.pi / 180
        jpos[5] = 90 * math.pi / 180
        self.movePTPJointSpace(jpos, relVel)


import numpy as np
import matplotlib.pyplot as plt
from part4 import kf_smooth

def time_and_meas_update(Sigma, A, Sigma_w, C, R):
    '''Your code here'''
    # all the definitions are similar to KF implementation, performing time update and measurement update
    # here Sigma_w is Q in the slides. You need to output the Sigma for the measurement update.
    n = A.shape[0]
    Sigma_new = Sigma # this line is given
    Sigma_new = A @ Sigma_new @ A.T + Sigma_w
    S = C @ Sigma_new @ C.T + R
    K = Sigma_new @ C.T @ np.linalg.inv(S)
    Sigma_new = (np.identity(n) - K @ C) @ Sigma_new
    '''your code end'''
    return Sigma_new

# initialize the three sensors
n=3
A  = np.array([[-0.6, 0.8, 0.5], [-0.1, 1.5, -1.1], [1.1, 0.4, -0.2]])
Sigma_w = np.eye(n)
S1 = np.array([[0.74, -0.21, -0.64]])
S2 = np.array([[0.37, 0.86, 0.37]])
S3 = np.array([[0, 0, 1]])
Sigma_S1 = np.array([[0.1**2]])
Sigma_S2 = np.array([[0.1**2]])
Sigma_S3 = np.array([[0.1**2]])

Sigma_0 = np.eye(n)

T = 50
s1_trace = []
s2_trace = []
s3_trace = []

# same sensor version code provided as an example
Sigma = Sigma_0
for t in range(T):
    Sigma = time_and_meas_update(Sigma, A, Sigma_w, S1, Sigma_S1)
    s1_trace.append(np.trace(Sigma))

Sigma = Sigma_0
for t in range(T):
    Sigma = time_and_meas_update(Sigma, A, Sigma_w, S2, Sigma_S2)
    s2_trace.append(np.trace(Sigma))

Sigma = Sigma_0
for t in range(T):
    Sigma = time_and_meas_update(Sigma, A, Sigma_w, S3, Sigma_S3)
    s3_trace.append(np.trace(Sigma))

plt.figure()
plt.plot(s1_trace, 'b', label='Sigma_S1')
plt.plot(s2_trace,'g', label='Sigma_S2')
plt.plot(s3_trace,'r', label='Sigma_S3')
plt.legend()
plt.show()

Sigma = Sigma_0
s123_trace = []
for t in range(T):
    '''Your code here'''
    # you need to write the round robin algo to choose which Sigma to use
    # note C is selected from Sigma_S*
    if t % n == 0:
        C = S1
        R = Sigma_S1
    elif t % n == 1:
        C = S2
        R = Sigma_S2
    else:
        C = S3
        R = Sigma_S3
    '''Your code end'''

    Sigma = time_and_meas_update(Sigma, A, Sigma_w, C, R)
    s123_trace.append(np.trace(Sigma))

plt.figure()
plt.plot(s123_trace, '--')
plt.show()

# greedy:

Sigma = Sigma_0
s1_greedy_trace = []
s2_greedy_trace = []
s3_greedy_trace = []
sgreedy_choice = []
sgreedy_trace = []
for t in range(T):
    C = S1
    R = Sigma_S1
    Sigma_try1 = time_and_meas_update(Sigma, A, Sigma_w, C, R)
    s1_greedy_trace.append(np.trace(Sigma_try1))

    C = S2
    R = Sigma_S2
    Sigma_try2 = time_and_meas_update(Sigma, A, Sigma_w, C, R)
    s2_greedy_trace.append(np.trace(Sigma_try2))

    C = S3
    R = Sigma_S3
    Sigma_try3 = time_and_meas_update(Sigma, A, Sigma_w, C, R)
    s3_greedy_trace.append(np.trace(Sigma_try3))
    '''Your code here'''
    # your greedy algorithm
    # select your Sigma based on tries!
    greedy_choice = np.argmin([s1_greedy_trace[t], s2_greedy_trace[t], s3_greedy_trace[t]])
    if greedy_choice == 0:
        Sigma = Sigma_try1
    elif greedy_choice == 1:
        Sigma = Sigma_try2
    else:
        Sigma = Sigma_try3
    sgreedy_trace.append(np.trace(Sigma))
    '''Your code end'''

plt.figure()
plt.plot(sgreedy_trace, 'g--')
plt.show()


# -*- coding: utf-8 -*-
"""
Created on Thu Jan 12 00:23:46 2023

@author: calic
"""

import random

options = ["rock", "paper", "scissors"]

def get_player_choice():
    player_choice = input("Enter a choice(rock, paper, scissors: ")
    return str(player_choice)

def check_input(user_input):
    if user_input == "rock" or user_input =="paper" or user_input =="scissors":
        return str(user_input)
    else:
        print("Try again!")
        return False

def check_loop():
    choice = str(get_player_choice())
    check = check_input(choice)
    if check == False:
        check_loop()
    else:
        return str(choice)

def get_choices():
    player_choice = str(check_loop())
    computer_choice = random.choice(options)
    choices = {"player": str(player_choice), "computer": computer_choice}
    return choices

def check_win(player, computer):
    print(f"You chose {player}, computer chose {computer}")
    if player == computer:

        return "It's a tie"
    elif player == "scissors":
        if computer == "rock":
            return "Rock smashes scissors! You Lose!"
        else:# computer =="paper":
            return"Scissors cut paper! You Win!"
    elif player == "rock":
        if computer == "scissors":
            return "Rock smashes scissors! You Win!"
        else:# if computer =="paper":
            return"Paper wraps rock! You Lose!"
    elif player == "paper":
        if computer == "rock":
            return "Paper wraps rock! You Win!"
        else:# if computer =="scissors":
            return"Scissors cut paper! You Lose!"

def main():
    choices = get_choices()
    #check_win(choices["player"],choices["computer"])

    return(check_win(
        str(choices["player"]),
        choices["computer"])
    )


import random
from Plemiona.terrain import Terrain
import math

class Map:
    """
    Class resposnible for providing acess to map and for performing operations on terrains
    """

    def __init__(self, name, config):
        """
        Parameters
        ----------
        name : str
            The name of the map (Pangea)
        config : Config
            Instance of configuration class
        """
        self.name = name
        self.terrains = {}
        self.config = config
        self.tribes = []
        self.x = 0
        self.y = 0

    def get_terrain(self, x, y) -> bool:
        """
        Method used to get terrain by coordinates
        Parameters
        ----------
        x : int
            Horizontal posistion of terrain
        y : int
            Horizontal posistion of terrain
        """
        if x < self.x and x >= 0:
            if y < self.y and y >= 0:
                return True, self.terrains[x, y]
        return False, None

    def get_surrounding_terrains(self, terrain: Terrain) -> list[Terrain]:
        """
        Method used to get surrounding terrains by terrain
        Parameters
        ----------
        terrain : Terrain
            Instance of class terrain
        """
        if terrain == None:
            return []
        list = []
        for b in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
            resp, ter = self.get_terrain(terrain.x + b[0], terrain.y + b[1])
            if resp:
                list.append(ter)
        return list

    def is_terrain_ocupied(self, x, y) -> bool:
        """
        Method used to check if terrain is ocupied.
        Returns true or false.

        Parameters
        ----------
        x : int
            Horizontal posistion of terrain
        y : int
            Horizontal posistion of terrain
        """
        if self.terrains[x, y].owner != None:
            return True
        else:
            return False

    def get_distance(self, terrain_a: Terrain, terrain_b: Terrain) -> float:
        """
        Method used to get distance between terrains
        Parameters
        ----------
        terrain_a : Terrain
            Instance of class terrain
        terrain_a : Terrain
            Instance of class terrain
        """
        return math.sqrt(
            math.pow((terrain_a.x - terrain_b.x), 2)
            + math.pow((terrain_b.y - terrain_a.y), 2)
        )

    def change_owner(self, x, y, owner):
        """
        Method used to change ownership between tribes
        Parameters
        ----------
        x : int
            Horizontal posistion of terrain
        y : int
            Horizontal posistion of terrain
        owner : Tribe
            Tribe we want to transfer ownership to
        """
        ter = self.terrains[x, y]
        if hasattr(ter, "owner"):
            if ter.owner != None:
                ter.owner.remove_terrain(ter)
        self.terrains[x, y].owner = owner
        if owner != None:
            owner.add_terrain(ter)


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

from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse
import re
from django.conf import settings

class RbacMiddleware(MiddlewareMixin):
    '''
    1.获取当前用户的url
    2.获取当前用户在session中的url权限列表
    3.权限信息进行匹配
    '''

    def process_request(self, request):
        '''
        当用户请求刚进入时执行
        :param request:
        :return:
        '''

        # 获取当前用户的url
        current_url = request.path_info
        # 如果当前用户访问的url在白名单内则可以访问
        for valid in settings.VALID_URL_LIST:
            if re.match(valid, current_url):
                return None

        # print(current_url)
        # 获取当前用户session中的存放的url
        permission_dict = request.session.get(settings.PERMISSION_SESSION_KEY)
        # print('permission', permission_list)
        # 如果没有session中不存在当前用户的信息则返回错误
        if not permission_dict:
            return HttpResponse('未获取到用户信息，请登陆')

        flag = False

        url_record = [
            {'title': '首页', 'url': '#'}
        ]
        # 循环session中的url,判断url是否与当前用户访问的url匹配，如果匹配则可以访问，匹配不成功则返回错误信息
        for item in permission_dict.values():
            reg = '^%s$' % item['url']
            if re.match(reg, current_url):
                flag = True
                # 获取pid或id,如果pid为True传给current_selected_permission，为False则把ID传给current_selected_permission
                request.current_selected_permission = item['pid'] or item['id']
                if not item['pid']:
                    url_record.extend([{'title': item['title'], 'url': item['url'],'class':'active'}])
                else:
                    url_record.extend(
                        [{'title': item['p_title'], 'url': item['p_url']},
                         {'title': item['title'], 'url': item['url'],'class':'active'},
                         ]
                    )
                request.url_record = url_record
                break

        if not flag:
            return HttpResponse('无权访问')