import logging
from typing import Any, Callable, Coroutine, Dict, List, Optional, Union

import attr
from geojson_pydantic.geometries import (
    GeometryCollection,
    LineString,
    MultiLineString,
    MultiPoint,
    MultiPolygon,
    Point,
    Polygon,
)
from pydantic import validator
from pydantic.types import conint
from pystac.utils import str_to_datetime
from stac_fastapi.api.models import BaseSearchGetRequest, ItemCollectionUri
from stac_fastapi.pgstac.types.base_item_cache import BaseItemCache
from stac_fastapi.pgstac.types.search import PgstacSearch
from starlette.requests import Request

from pccommon.redis import cached_result
from pcstac.contants import CACHE_KEY_BASE_ITEM

DEFAULT_LIMIT = 250

logger = logging.getLogger(__name__)

class PCSearch(PgstacSearch):
    # Increase the default limit for performance
    # Ignore "Illegal type annotation: call expression not allowed"
    limit: Optional[conint(ge=1, le=1000)] = DEFAULT_LIMIT  # type:ignore

    # Can be removed when
    # https://github.com/stac-utils/stac-fastapi/issues/187 is closed
    intersects: Optional[
        Union[
            Point,
            MultiPoint,
            LineString,
            MultiLineString,
            Polygon,
            MultiPolygon,
            GeometryCollection,
        ]
    ]

    @validator("datetime")
    def validate_datetime(cls, v: str) -> str:
        """Validate datetime.

        Custom to allow for users to supply dates only.
        """
        if "/" in v:
            values = v.split("/")
        else:
            # Single date is interpreted as end date
            values = ["..", v]

        dates: List[str] = []
        for value in values:
            if value == "..":
                dates.append(value)
                continue

            str_to_datetime(value)
            dates.append(value)

        if ".." not in dates:
            if str_to_datetime(dates[0]) > str_to_datetime(dates[1]):
                raise ValueError(
                    "Invalid datetime range, must match format (begin_date, end_date)"
                )

        return v

class RedisBaseItemCache(BaseItemCache):
    """
    Return the base item for the collection and cache by collection id.
    First check if the instance has a local cache of the base item, then
    try redis, and finally fetch from the database.
    """

    def __init__(
        self,
        fetch_base_item: Callable[[str], Coroutine[Any, Any, Dict[str, Any]]],
        request: Request,
    ):
        self._base_items: dict = {}
        super().__init__(fetch_base_item, request)

    async def get(self, collection_id: str) -> Dict[str, Any]:
        async def _fetch() -> Dict[str, Any]:
            return await self._fetch_base_item(collection_id)

        if collection_id not in self._base_items:
            cache_key = f"{CACHE_KEY_BASE_ITEM}:{collection_id}"
            self._base_items[collection_id] = await cached_result(
                _fetch, cache_key, self._request
            )

        return self._base_items[collection_id]

@attr.s
class PCItemCollectionUri(ItemCollectionUri):
    limit: Optional[int] = attr.ib(default=DEFAULT_LIMIT)  # type:ignore

@attr.s
class PCSearchGetRequest(BaseSearchGetRequest):
    limit: Optional[int] = attr.ib(default=DEFAULT_LIMIT)  # type:ignore


def fiware_arguments(func):
    def wrapper(*args, **kwargs):
        parser = func(*args, **kwargs)
        parser.add_argument(
            '--fiwareservice',
            help='tenant/service to use when connecting Orion Context Brocker')
        parser.add_argument(
            '--fiwareservicepath',
            help='scope/path to use when connecting Orion Context Brocker')
        return parser
    return wrapper


#!/usr/bin/env python
# -*- coding=UTF-8 -*-
# Created at May 26 10:07 by BlahGeek@Gmail.com

import sys
if hasattr(sys, 'setdefaultencoding'):
    sys.setdefaultencoding('UTF-8')

import os
import httplib2
import requests
from BeautifulSoup import BeautifulSoup
from .settings import COOKIR_PATH

BASE_URL = 'http://3g.renren.com/status/newstatus.do'

class RenRen:

    def __init__(self):
        self.session = requests.Session()
        cookie = open(COOKIR_PATH).read()
        cookie = [x.strip() for x in cookie.split(';') if x]
        cookie = map(lambda x: x.split('=', 1), cookie)
        cookie = dict(cookie)
        self.session.cookies = requests.utils.cookiejar_from_dict(cookie)

    def postStatus(self, text):
        soup = BeautifulSoup(self.session.get(BASE_URL).content)
        form = soup.find('form')
        assert(form is not None)
        values = map(lambda x: (x['name'], x['value']), form.findAll('input', type='hidden'))
        data = {'status': text}
        data.update(dict(values))
        req = self.session.post(form['action'], data)
        # save cookie
        with open(COOKIR_PATH, 'w') as f:
            cookie = requests.utils.dict_from_cookiejar(self.session.cookies)
            cookie = '; '.join([k+'='+v for k, v in cookie.iteritems()])
            f.write(cookie)


import os
import v2_draw_dynamic as main_app
import logging
import sys

def cmd(cmdstr):
    print(cmdstr)
    os.system(cmdstr)

def main():
    '''
    if len(sys.argv) < 2:
        logging.error("please input msg log file")
        return
    '''
    while True:
        try:
            main_app.draw_dynamic(r"D:\Programs\bin3(EC2)_R\log\010011112222\msg.log")
        except Exception as e:
            print("catch exception",e)
            logging.error(str(e))

main()


import os
import pika  # Importa a biblioteca pika para interagir com o RabbitMQ
import time  # Importa a biblioteca time para controlar o tempo de sleep do loop
import socket  # Importa socket para verificar a conectividade com a internet
import json  # Importa json para manipular dados JSON
import random  # Importa random para gerar números aleatórios
import math  # Importa math para realizar operações matemáticas
import xml.etree.ElementTree as ET  # Importa ElementTree para manipular dados XML
import glob  # Importa glob para encontrar todos os caminhos que correspondem a um padrão específico
from datetime import datetime, timedelta  # Importa datetime e timedelta para trabalhar com datas e horas
from dotenv import load_dotenv  # Importa load_dotenv para carregar variáveis de ambiente do arquivo .env

# Carregar variáveis do .env
load_dotenv()
# Obtém as variáveis de ambiente para a URL do RabbitMQ e a chave de roteamento
RABBITMQ_URL = os.getenv("RABBITMQ_URL")
ROUTING_KEY = os.getenv("ROUTING_KEY")

class SensorSimulator:
    def __init__(self, sensor_data):
        """
        Inicializa o simulador com os dados dos sensores fornecidos.

        :param sensor_data: dict, dados dos sensores a serem utilizados na simulação.
        """
        self.sensor_data = sensor_data

    def generate_value(self, min_val, max_val, mean_val, fluctuation=5):
        """
        Gera um valor flutuante aleatório entre um intervalo especificado.

        :param min_val: float, valor mínimo possível.
        :param max_val: float, valor máximo possível.
        :param mean_val: float, valor médio desejado.
        :param fluctuation: float, flutuação permitida em torno do valor médio.
        :return: float, valor aleatório gerado.
        """
        lower_bound = max(min_val, mean_val - fluctuation)
        upper_bound = min(max_val, mean_val + fluctuation)
        return random.uniform(lower_bound, upper_bound)

    def simulate_sensor_failure(self, prob_failure=0.01):
        """
        Simula uma falha no sensor com uma probabilidade especificada.

        :param prob_failure: float, probabilidade de falha do sensor.
        :return: bool, True se falhar, False caso contrário.
        """
        return random.random() < prob_failure

    def log_data_to_xml(self, batch_data):
        """
        Loga os dados do sensor em um arquivo XML.

        :param batch_data: list, dados do sensor a serem logados.
        """
        date_str = datetime.now().strftime("%Y%m%d")
        log_filename = f"sensor_data_log_{date_str}.xml"

        # Verifica se o arquivo de log já existe, se sim, carrega os dados existentes
        if os.path.exists(log_filename):
            tree = ET.parse(log_filename)
            root = tree.getroot()
        else:
            root = ET.Element("SensorDataBatch")

        # Adiciona novos dados ao XML
        for data in batch_data:
            sensor_data = ET.SubElement(root, "SensorData")
            for key, value in data.items():
                ET.SubElement(sensor_data, key).text = str(value)

        # Salva os dados no arquivo XML
        tree = ET.ElementTree(root)
        with open(log_filename, "wb") as file:
            tree.write(file)

        # Limpa logs antigos
        self.clean_old_logs(log_file_prefix="sensor_data_log_", max_logs=7)

    def clean_old_logs(self, log_file_prefix, max_logs):
        """
        Limpa logs antigos, mantendo apenas um número específico de arquivos de log.

        :param log_file_prefix: str, prefixo dos arquivos de log.
        :param max_logs: int, número máximo de arquivos de log a serem mantidos.
        """
        log_files = sorted(glob.glob(f"{log_file_prefix}*.xml"))
        for log_file in log_files[:-max_logs]:
            os.remove(log_file)

    def send_to_rabbitmq(self, message):
        """
        Envia uma mensagem para uma fila RabbitMQ.

        :param message: str, mensagem a ser enviada.
        """
        # Estabelece conexão com o RabbitMQ e declara a fila
        connection = pika.BlockingConnection(pika.URLParameters(RABBITMQ_URL))
        channel = connection.channel()
        channel.queue_declare(queue=ROUTING_KEY, durable=True)

        # Publica a mensagem na fila
        channel.basic_publish(exchange='',
                              routing_key=ROUTING_KEY,
                              body=message,
                              properties=pika.BasicProperties(
                                  delivery_mode=2,
                              ))
        print(f" [x] Enviado '{message}'")
        connection.close()

    def simulate(self):
        """
        Inicia a simulação, gerando dados de sensores, logando-os e enviando-os para a fila RabbitMQ.
        """
        # Aguarda conexão com a internet
        while not self.is_connected():
            print("Aguardando conexão com a internet...")
            time.sleep(5)

        specific_sensors = list(self.sensor_data.keys())

        # Loop de simulação
        while True:
            batch_data = []
            start_timestamp = datetime.now()

            # Gera dados para cada sensor especificado
            for machine_id, sensor_id in specific_sensors:
                faixa_min, faixa_max, valor_medio = self.sensor_data[(machine_id, sensor_id)]

                # Adiciona uma pequena variação ao valor médio
                valor_medio += 1
                valor_medio += 5 * math.sin(start_timestamp.minute / 5)

                # Simula falha no sensor ou gera valor
                if self.simulate_sensor_failure():
                    value = None
                else:
                    value = self.generate_value(faixa_min, faixa_max, valor_medio)

                # Cria timestamp e dados do sensor
                timestamp = start_timestamp + timedelta(minutes=random.randint(0, 5))
                str_timestamp = timestamp.strftime('%Y-%m-%d %H:%M:%S')

                data = {
                    "timestamp": str_timestamp,
                    "CompanyId": "EMPRESA01",
                    "MachineId": machine_id,
                    "SensorId": sensor_id,
                    "Value": value
                }

                batch_data.append(data)

            # Envia dados para RabbitMQ e loga em XML
            self.send_to_rabbitmq(json.dumps(batch_data))
            self.log_data_to_xml(batch_data)

            # Controla o tempo de sleep do loop dependendo do horário
            current_hour = datetime.now().hour
            if 9 <= current_hour <= 17:
                time.sleep(180)
            else:
                time.sleep(300)

    @staticmethod
    def is_connected():
        """
        Verifica se há conexão com a internet.

        :return: bool, True se conectado, False caso contrário.
        """
        try:
            socket.create_connection(("www.google.com", 80))
            return True
        except OSError:
            pass
        return False

# Dados dos sensores
sensor_data = {
    ("M01", "S01"): (70, 100, 80),
    ("M01", "S02"): (500, 900, 700),
    ("M02", "S03"): (100, 140, 120),
    ("M03", "S04"): (500, 900, 700),
    ("M04", "S05"): (160, 210, 170),
    ("M05", "S06"): (70, 100, 80),
    ("M05", "S07"): (100, 140, 130),
    ("M06", "S08"): (7000, 12000, 10800),
    ("M06", "S09"): (100, 140, 130),
    ("M07", "S10"): (70, 100, 80),
    ("M07", "S11"): (7000, 12000, 10800),
    ("M07", "S16"): (100, 400, 201),
    ("M08", "S12"): (70, 100, 80),
    ("M08", "S13"): (1000, 3000, 2000),
    ("M09", "S14"): (1500, 1900, 1765),
    ("M10", "S15"): (1500, 1900, 1765)
}

# Inicia a simulação
simulator = SensorSimulator(sensor_data)
simulator.simulate()


# ---
# jupyter:
#   jupytext:
#     formats: ipynb,py:light
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# +
# 왜 틀리지?
# 삐빅 시간초과.. -> 아 input시간 차이 은근 크다!
import sys
input=sys.stdin.readline
N=int(input())
S={}
for _ in range(N):
    a=int(input())
    if a not in S:
        S[a]=1
    else:
        S[a]+=1

# 여기서 리스트로 다시 저장하면 시간초과임
# for i in S:
#     if max(S.values())==S[i]:
#         M.append(int(i))

# M.sort()
# print(M[0])

# items를 써서 2차원 배열 형태로 만든 것....?
S=sorted(S.items(), key=lambda x:(-x[1], x[0]))
print(S[0][0])
# -


# Databricks notebook source
# MAGIC %md
# MAGIC ## 2D Linear Regression
# MAGIC
# MAGIC #### Description
# MAGIC
# MAGIC This notebook is designed to provide a very basic insight into linear regression and how to utilise sklearn to perform it on datsets.
# MAGIC
# MAGIC In this notebook linear regression is performed on a dataset with 2 numeric variables, its aim is to explain the basic principles of linear regression before moving onto the second notebook which demonstrates linear regression on a multi-variable problem.
# MAGIC
# MAGIC Linear regression uses algebra to define the linear relationship between two or more variables. In 2-dimensional space, this linear relationship can be seen as the 'line-of-best-fit', a straight line that best represents the relationship between the 2 variables. This relationship holds as we add more variables though the line exists in higher dimensions and is hard to visualise through standard means.
# MAGIC
# MAGIC This linear relationship can then be used as a method for helping predicitions.
# MAGIC
# MAGIC #### SKLearn performance in databricks
# MAGIC
# MAGIC While SKLearn can be useful in certain situations, it is not designed to take advantage of cluster computing resources, which arguably is a major downside to using it inside databricks as you are not utilising the full proccessing power available to you.
# MAGIC
# MAGIC This is not us saying do not use sklearn as it may well be appropriate for certain tasks, however if your are performing tasks over large datasets and want to fully exploit the compute resources you have available to complete these tasks. Then you should look into the Spark `MLlib` library.

# COMMAND ----------

# MAGIC %md
# MAGIC #### Retrieve the data
# MAGIC
# MAGIC In this example the toy datasets have already created and added to the collab database to mimic an actual workflow, we will use a general function to get the database name however this can be can be replaced with a string.
# MAGIC
# MAGIC The utility functions are imported via the next command which runs the notebook stored in a different location. You can view these functions by navigating to the folder or you can also click the link in the next command. This can also be a useful way to tidy up your code and store frequently used functions in their own notebook to be imported into others.

# COMMAND ----------

# DBTITLE 1,Import python utility functions
# MAGIC %run ../../Wrangler-Utilities/Utilities-Python

# COMMAND ----------

import matplotlib.pyplot as plt
import pandas as pd

# get the table name
table_name = f"{get_collab_db()}.toy_2d_linear_regression"

# retrieve the dataframe
spark_df = spark.table(table_name)

# show the spark dataframe
display( spark_df )

# COMMAND ----------

# MAGIC %md
# MAGIC #### Understanding the Data
# MAGIC
# MAGIC As a first step before we move on to using creating potentially complex models, it may be useful to get some quick insights into the dataset. This way when moving forward we have a general appreciation of the contents of the dataset.
# MAGIC
# MAGIC There is many ways to do this, here we will show the inbuilt describe method and also how to create a simple plot of the data.
# MAGIC
# MAGIC *note: because this data is 2d in nature, plots are quite straightforward, more complex visualisation methods are needs for multivariable data*

# COMMAND ----------

# using .describe() gives us insight into some basic metrics of a dataframe
# we can also pass in column names e.g. .describe(['feature']), to isolate columns
display(spark_df.describe())

# COMMAND ----------

# to plot the data we must first convert it to a NumPy array or a Pandas dataframe

# Convert from spark dataframe to pandas dataframe
pandas_df = spark_df.toPandas()

# extract the feature and target columns
X = pandas_df['feature']
y = pandas_df['target']

# plot the data
plt.figure(figsize=(10, 5))
plt.scatter(X, y, marker='o')

plt.title("Plot of the Random Regression Dataset", fontsize="large")
plt.xlabel("Feature")
plt.ylabel("Target")
plt.show()

# COMMAND ----------

# MAGIC %md
# MAGIC #### Utilising SKLearn Linear Regression
# MAGIC In the above plot of the data we can see that there is a clear pattern in the data. But now suppose we want to model the exact linear relationship of this dataset.
# MAGIC
# MAGIC This is where we can utilise the sklearn LinearRegression function to aid us. To utilise the sklearn methods we must have a pandas dataframe not a spark dataframe.

# COMMAND ----------

# convert spark dataframe to pandas dataframe
pandas_df = spark_df.toPandas()

# extract the 2 features we want into seperate variables
X = pandas_df['feature']
y = pandas_df['target']

# split the data into training and test sets
# note the random_state variable is used so split is same every time, in practice ignore
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)

# models require 2d Arrays so reformat
X_train = X_train.values.reshape(-1,1)
X_test = X_test.values.reshape(-1,1)
y_train = y_train.values.reshape(-1,1)
y_test = y_test.values.reshape(-1,1)

# fit a linear regression model ot the training data
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train) # training the algorithm

# generate our predicitions from the test data
y_pred = regressor.predict( X_test )

# COMMAND ----------

# MAGIC %md
# MAGIC #### Visualising our Predictions
# MAGIC
# MAGIC There is many different ways we can now visualise our predictions:
# MAGIC - We can plot a figure of the scattered test plots and our predicted line
# MAGIC - We can display a table showing the actual test values vs our predicted values
# MAGIC - We can then plot a figure of this table to visualise it
# MAGIC
# MAGIC These are just few examples of course there is many more ways to gain insight.

# COMMAND ----------

# we can extract the exact intercetp and coefficient of the slope
print("Intercept   : ", regressor.intercept_)
print("Coefficient : ", regressor.coef_)

# plot the figure
plt.figure(figsize=(10, 5))
plt.scatter(X_test, y_test, color='gray')
plt.plot(X_test, y_pred, color='red', linewidth=2)

plt.title("Prediction vs Test data", fontsize="large")
plt.xlabel("Feature")
plt.ylabel("Target")
plt.show()

# COMMAND ----------

# create table view of actual values vs predictions
df = pd.DataFrame({'Actual': y_test.flatten(), 'Predicted': y_pred.flatten()})
display(df)

# COMMAND ----------

# visualise above table as a bar chart, note were only visualising the first 20
df1 = df.head(20)
df1.plot(kind='bar', figsize=(10,5))
plt.grid(which='major', linestyle='-', linewidth='0.5', color='gray')
plt.show()

# COMMAND ----------

# MAGIC %md
# MAGIC ### Evaluating our Model
# MAGIC
# MAGIC The final step is to evaluate the performance of our model, this is important to compare how well different algorithms perform on a particular dataset.
# MAGIC
# MAGIC For regression, three evaluation metrics are commonly used:
# MAGIC - **Mean Absolute Error** is the mean of the absolute value of the errors
# MAGIC $$ MAE = \frac{1}{n} \sum^{n}_{j=1}|y_i - y_j| $$
# MAGIC - **Mean Squared Error** is the mean of th esquared errors
# MAGIC $$ MSE = \frac{1}{N} \sum^{n}_{j=1}(y_i - y_j)^2 $$
# MAGIC - **Root Mean Squared Error** is the square root of the mean squared errors
# MAGIC $$ MSE = \sqrt{\frac{1}{N} \sum^{n}_{j=1}(y_i - y_j)^2} $$

# COMMAND ----------

from sklearn.metrics import mean_absolute_error, mean_squared_error
import numpy as np

print('Mean Absolute Error     :', mean_absolute_error(y_test, y_pred))
print('Mean Squared Error      :', mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error :', np.sqrt(mean_squared_error(y_test, y_pred)))


import numpy as np
import argparse

### Arguments ###

parser = argparse.ArgumentParser()
parser.add_argument('input', type=str, help='Input text file')
parser.add_argument('fmap', type=str, help='Map with value and residue')
parser.add_argument('chain', type=str, help='Chain identifier to match residues')
parser.add_argument("--xvg", default=False, action=argparse.BooleanOptionalAction)
args = parser.parse_args()

def splitm(line):
	'''
	Correctly split pdb file
	'''
	return([line[0:6].strip(),line[6:11].strip(),line[12:16].strip(),line[16:17].strip(),line[17:20].strip(),
			line[21:22].strip(),line[22:26].strip(),line[26:27].strip(),line[30:38].strip(),line[38:46].strip(),
			line[46:54].strip(),line[54:60].strip(),line[60:66].strip(),line[76:78].strip(),line[78:80].strip()])

def create_dict(fname):
	map = {}
	with open(fname,'r') as f:
		for line in f:
			data = line.split()
			if args.xvg:
				if data[0] != '#' or data[0] != '@':
					map[data[0]] = float(data[1])*100
			else:
				map[data[0][3:]] = float(data[1])*100
	return map

def main(inp, fmap, chain):
	print('Suggested spectrum')
	print('spectrum b, 0xF4F3F3 0xD28288 0xF6A377 0xFBDF66 0xCFF66A 0x77FB74')
	map = create_dict(fmap)
	keys = map.keys()
	pdb_format = "{:6s}{:5d} {:^4s}{:1s}{:3s} {:1s}{:4d}{:1s}   {:8.3f}{:8.3f}{:8.3f}{:6.2f}{:6.2f}          {:>2s}{:2s}\n"
	out = inp.split('.')[0]+'_painted.pdb'
	with open(inp,'r') as f:
		with open(out, 'w') as fw:
			for line in f:
				data = splitm(line)
				if data[0] == 'ATOM':
					data[1] = int(data[1])
					data[6] = int(data[6])
					data[8] = float(data[8])
					data[9] = float(data[9])
					data[10] = float(data[10])
					data[11] = float(data[11])
					if data[5] == chain:
						if str(data[6]) in keys:
							data[12] = map[str(data[6])]
							fw.write(pdb_format.format(*data))
						else:
							if len(data[12]) == 0:
								data[12] = 0.0
								fw.write(pdb_format.format(*data))
							else:
								fw.write(line)
					else:
						if len(data[12]) == 0:
							data[12] = 0.0
							fw.write(pdb_format.format(*data))
						else:
							fw.write(line)
				else:
					fw.write(line)

if __name__ == '__main__':
	main(args.input, args.fmap, args.chain)


import urllib2, optparse
from opus_core.logger import logger

class LoadXSD(object):
    '''
    classdocs
    '''

    def __init__(self, source, destination):
        '''
        Constructor
        '''
        self.xsd_source = source
        self.xsd_destination = destination

    def load_and_store(self):

        logger.log_status('Loading xsd file from: %s' %self.xsd_source)

        response = urllib2.urlopen( self.xsd_source )
        xsd = response.read()

        logger.log_status('Store xsd file to: %s' %self.xsd_destination)
        outfile = open( self.xsd_destination, 'w')
        outfile.write( xsd )
        outfile.close()

if __name__ == "__main__":

    # default parameters are:
    # --source=http://matsim.org/files/dtd/MATSim4UrbanSimConfigSchema.xsd
    parser = optparse.OptionParser()
    parser.add_option("-s", "--source", dest="xsd_source", action="store", type="string",
                      help="URL of xsd file")
    parser.add_option("-d", "--destination", dest="xsd_destination", action="store", type="string",
                      help="Destination of xsd file")
    (options, args) = parser.parse_args()

    if options.xsd_source == None:
        logger.log_error("Missing source location (url) to xsd file")
    if options.xsd_destination == None:
        logger.log_error("Missing destination location for xsd file")

    load = LoadXSD( options.xsd_source, options.xsd_destination )
    load.load_and_store()


# -*- coding: utf-8 -*-
from linlp.algorithm.Viterbi import viterbiRecognitionSimply
from linlp.algorithm.viterbiMat.prob_trans_place import prob_trans as trans_p
from linlp.algorithm.viterbiMat.prob_emit_place import prob_emit as emit_p

def placeviterbiSimply(obs, DT, obsDT, debug):
    if debug:
        x = obs
    obs = [('始##始', 'begin')] + obs + [('末##末', 'end')]
    length = len(obs)
    for no in range(length):
        if (obs[no][1] == 'ns') and (obsDT.tree[obs[no][0]].get('total', 1001) <= 1000):
            if DT.tree.get(obs[no][0]):
                if len(obs[no][0]) < 3:
                    DT.tree[obs[no][0]].setdefault('H', 1)
                    DT.tree[obs[no][0]].setdefault('G', 1)
                else:
                    DT.tree[obs[no][0]].update({'G': 1})
            else:
                if len(obs[no][0]) < 3:
                    DT.tree[obs[no][0]] = {'H': 1, 'G': 1}
                else:
                    DT.tree[obs[no][0]] = {'G': 1}
        elif obs[no][1].startswith('ns'):
            obs[no] = ('未##地', obs[no][1])
        elif obs[no][1].startswith('x'):
            obs[no] = ('未##串', 'x')
        elif obs[no][1].startswith('nr'):
            obs[no] = ('未##人', obs[no][1])
        elif obs[no][1].startswith('nt'):
            obs[no] = ('未##团', obs[no][1])
        elif obs[no][1].startswith('m'):
            obs[no] = ('未##数', obs[no][1])
        elif obs[no][1].startswith('t'):
            obs[no] = ('未##时', obs[no][1])
        elif not DT.tree.get(obs[no][0]):  # 不在地名词典时
            DT.tree[obs[no][0]] = {'Z': 21619956}
    path = viterbiRecognitionSimply(obs, trans_p, emit_p, DT)
    if debug:
        s = ''
        t = '['
        l = len(x)
        for i in range(l):
            word = x[i]
            s += '[' + word[0] + ' '
            t += word[0]
            for k, v in DT.tree[obs[i + 1][0]].items():
                if k == 'total':
                    continue
                s += k + ':' + str(v) + ' '
            s += ']'
            t += '/' + path[i + 1] + ', '
        t += ']'
        print('地名角色观察: %s' % s)
        print('地名角色标注: %s' % t)
    return path[1:-1]


from collections import OrderedDict
from django.core.urlresolvers import resolve

def link_processor(request):
    """
    This function provides, to all pages, a dict of links called "page_links".
    These links contain {"name": "tag"} for a name of a page to a view tag.

    These are used to automatically populate the sidebars.
    """
    # These are names which go to url tags.
    SIDEBAR_URLS = OrderedDict()
    SIDEBAR_URLS["Home"] = "home"
    SIDEBAR_URLS["Schools"] = "schools"
    SIDEBAR_URLS["Professors"] = "professors"
    SIDEBAR_URLS["Reviews"] = "reviews"
    return {"page_links": SIDEBAR_URLS.items(),
            "current_page_name": resolve(request.path_info).url_name,
            }


class leafNode:
    def __init__(self, data) -> None:
        self.leafData = self.numberOfLabelOccurrences(data) #para análise de treinamento
        self.isLeaf = True

    def numberOfLabelOccurrences(self, data):
        # um dicionário (nao permite itens duplicados)
        # para armazenar as labels e quantas vezes ocorrem
        numberOfOccurrences = {}

        for row in data:
            rowLabel = row[-1]  # label na última coluna
            if rowLabel not in numberOfOccurrences:
                numberOfOccurrences[rowLabel] = 0
            numberOfOccurrences[rowLabel] += 1

        # retorna o dicionario com a contagem de ocorrencias de cada label
        return numberOfOccurrences


# task 10.2 Напишіть програму, яка пропонує користувачу ввести свій вік, після чого виводить повідомлення про те чи вік є парним чи непарним числом.
# В програмі необхідно передбачити можливість введення від’ємного числа, і в цьому випадку згенерувати виняткову ситуацію.
# Головний код має викликати функцію, яка обробляє введену інформацію.

class AgeError(ValueError):
    def __init__(self, data):
        self.data = data

    def __str__(self):
        return repr(self.data)

def check_age(p_age):
    if p_age < 0:
        raise AgeError('Age is negative')

    if p_age % 2 > 0:
        return 'Odd age'

    return 'Even age'

if __name__ == "__main__":
    try:
        age = int(input('Please enter your age:'))
        print(check_age(age))
    except AgeError as e:
        print('Negative age is not allowed. ', e)
    except Exception:
        print("Seems like something else went wrong")
    finally:
        print("Program finished")


#这个是我自己写的，过了前80个Case 过不了最后一个 超时了
#代码随想录的前两个答案也超时，只有那个用字典的不超时

'''
class Solution:
    def findItinerary(self, tickets: List[List[str]]) -> List[str]:
        self.result = []
        tickets.sort()
        used = [False] * len(tickets)
        self.backtracking(['JFK'],used,tickets)
        return self.result

    def backtracking(self, path, used, tickets):
        if len(path) == len(tickets) + 1:
            self.result = path
            # print('可以了')
            return True

        for index, ticket in enumerate(tickets):
            if used[index] == False and tickets[index][0] == path[-1]:
                #找没用过的机票且对得上上一个地方的
                path.append(tickets[index][1])
                used[index] = True
                #向下递归
                # print('path is',path)
                # print('used is',used)
                # print('继续递归')
                if self.backtracking(path, used, tickets):
                    return True

                path.pop()
                used[index] = False
        return False
'''

#代码随想录里的字典解法
#这道题看着这个Case来想就行了
#ticket = [[“jfk”，“kul”],["nrt","jfk"],["jfk","nrt"]]
class Solution:
    from collections import defaultdict
    def findItinerary(self, tickets: List[List[str]]) -> List[str]:
        targets = defaultdict(list)
        for ticket in tickets:
            targets[ticket[0]].append(ticket[1])
        #以上得到字典，key是出发地，value是一个list,list里装的是目的地

        for value in targets.values():
            value.sort(reverse=True)
        #以上将Targets字典中的目的地按照逆序排序

        self.result = []
        self.backtracking("JFK", targets)
        return self.result[::-1] #return result (逆序)

    def backtracking(self, start, targets):
        while targets[start]:  #当某个出发机场有目的机场时
            next_start = targets[start].pop() #找到下一个出发机场 并在targets里把用过的目的地去掉
            self.backtracking(next_start, targets)
        self.result.append(start) #当某个出发机场找不到目的机场时，放进result里
        #result最后是要逆序返回的 belike jfk-nrt-jfk-kul 在result里是["kul"<-"jfk"<-"nrt"<-"jfk"]
        #所以找不到出发机场的kul会最先进result,因为他左边没有东西了


import json
import requests
import random
import re

baseUrl = "http://jw1.yzu.edu.cn/"

session = requests.Session()

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
           'Content-Type': 'application/x-www-form-urlencoded',
           'Connection': 'keep-alive',
           'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
           'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
           'Accept-Encoding': 'gzip, deflate',
           'Upgrade-Insecure-Requests': '1'}

def GetCode():
    validcodeUrl = baseUrl + "validateCodeAction.do?random=+Math.random()"
    validcode = session.get(validcodeUrl)
    if (validcode.status_code == 200):
        with open('codepic.png', 'wb') as f:
            f.write(validcode.content)
        print("验证码保存成功！")
    else:
        print("验证码保存失败！")

def Login(validcode):
    data = {
        "zjh": 191304324,
        "mm": 191304324,
        "v_yzm": validcode
    }
    res = session.post(baseUrl+"loginAction.do", data=data)
    if "学分制综合教务" in res.text:
        print("登录成功\n")
    else:
        print("登陆失败\n")

def GetSessionId():
    res = session.get("http://jw1.yzu.edu.cn/dwr/engine.js")
    start = res.text.find("dwr.engine._origScriptSessionId")
    w1 = "dwr.engine._origScriptSessionId = "
    w2 = ";"

    pat = re.compile(w1+'(.*?)'+w2, re.S)
    sessionId = pat.findall(res.text)[0][1:-1] + \
        str(random.randint(0, 1000))
    return sessionId

def ClassScript():
    GetCode()
    validCode = input("输入验证码:\n")
    Login(validCode)
    # queryClass = {
    #     "kch": 17038002,
    #     "kcm": "",
    #     "actionType": 3,
    #     "pageNumber": -1
    # }
    classData = {
        "kcId": 17029015_01,
        # "kcId": 17038002_01,
        "preActionType": 3,
        "actionType": 9
    }

    res = session.get(
        "http://jw1.yzu.edu.cn/xkAction.do?actionType=-1&fajhh=3440")
    # res = session.get(
    #     "http://jw1.yzu.edu.cn/xkAction.do?actionType=3&pageNumber=-1")
    # res = session.post(baseUrl+"xkAction.do", data=queryClass)

    # sessionId = GetSessionId()
    # jSessionId = session.cookies["JSESSIONID"]
    # payloadData = "callCount=1\npage=/xkAction.do?actionType=-1&fajhh=3440\nhttpSessionId=" + jSessionId + \
    #     "\nscriptSessionId="+sessionId + \
    #     "\nc0-scriptName=ajaxtool\nc0-methodName=reCall\nc0-id=0\nbatchId=0 "

    # ajaxUrl = "http://jw1.yzu.edu.cn/dwr/call/plaincall/ajaxtool.reCall.dwr"

    # res = session.post(ajaxUrl, data=payloadData)

    res = session.post(
        baseUrl+"xkAction.do", data=classData)

    # res = session.post(
    #     baseUrl+"xkAction.do", data=classData)
    print(res.text)
    # file = open("text.html", mode="w")
    # file.write(res.text)

ClassScript()


# -*- coding: utf-8 -*-
"""
Binarization
Feature binarization is the process of thresholding numerical features to
 get boolean values.
"""

from sklearn.model_selection import train_test_split
from sklearn import preprocessing
import numpy as np
import pandas as pd
from scipy import signal,stats
from flask import Flask,request,jsonify
import json
import re
import os
import codecs

def py_configs(configpath,conf=None, delete=None):
    if not os.path.exists(configpath):
        obj = {}
    else:
        with codecs.open(configpath, 'r', 'utf-8') as f:
            str1 = f.read()
            #            obj = obj.encode('utf8').decode('utf8')
            if not str1:
                obj = {}
            try:
                obj = json.loads(str1)
            except:
                #
                obj = {}
    if isinstance(delete, str):
        obj.pop(delete)
        with codecs.open(configpath, 'w', 'utf-8') as f:
            str1 = jsonify(obj)
            f.write(str1)
        return obj
    if isinstance(conf, dict):
        for key in conf:
            obj[key] = conf[key]
        with codecs.open(configpath, 'w', 'utf-8') as f:
            str1 = jsonify(obj)
            f.write(str1)
    elif isinstance(conf, str):
        if conf in obj:
            return obj[conf]
        else:
            return {}
    return obj

configpath=os.path.join(os.path.dirname(__file__),'config.txt')
try:
    config = py_configs(configpath)
    Signal_SERVER = config["Signal_SERVER"]
    Signal_PORT = config["Signal_PORT"]
except:
    raise Exception("Configuration error")

app = Flask(__name__)
@app.route('/Data_preprocessing/scaling_data',methods=['POST'])
def daqfft():
    try:
        form_key=list(request.form.to_dict().keys())
        file_key=list(request.files.to_dict().keys())
        print('k: ',form_key)

        keys=['file','operation']
        for key in keys:
            if key not in form_key or key not in file_key:
                code = 2
                output = {"code": code, "KeyError": str(key)}
                output = json.dumps(output)
                return output

        operation = request.form['operation']
        file_get = request.files.get('file')
        X_train = pd.read_csv(file_get)

        result=''
# Operation Binarization

        if operation == 'Normalization':
            binarizer = preprocessing.Binarizer().fit(X_train) # fit does nothing
            bin_tran =binarizer.transform(X_train)
            result= jsonify(bin_tran)
        return result

    except Exception as e:
        print('Exception: ',e)
        code = 1
        result = {"code":code,"error":re.findall("'([\w\d _]+)'",str(type(e)))[0]}
        result = jsonify(result)
        return result

if __name__=="__main__":
    app.run(host= Signal_SERVER, port=int(Signal_PORT))


#  глобальная переменная  доступная в любом месте
# локальние доступние токо в пределах етого блока

name = 'Tom'
a = 5
N = (100,)

def myfunc(b):
    # global a
    a = 10
    for x in range(b):
        n = x + 1  # здесь a=10 как локальная переменная если написать global
        print(n, end=" ")  # мы будем работать с той которой создали в начале
        print()  # и если мы напишем принт(а) то будет уже 10
        # также можно писать глобал когда нет глобальной переменни
        # тоесть когда мы не обозначи а вообше

myfunc(6)
print()
print(a)

def say_hi():
    print('Hello', name)

def say_bye():
    name = 'Bob'
    print('Good bye', name)

say_hi()
say_bye()
print(name)

# nonlocal

x = 0

def outer():
    x = 1

    def inner():
        # nonlocal x
        x = 2
        print('inner:', x)
                        # каждая функция взяла свой х(переменную)

    inner()             # каторий обьявлен в своей области видимости
    print('outer:', x)

outer()
print('global:', x)

print()
print()

x1 = 0

def outer():
    x1 = 1

    def inner():
        nonlocal x1
        x1 = 2                # тоесть если мы напишем nonlocal
        print('inner:', x1)    # то  мы будем работать с x
                                # с уровнем выше с глоб так нельзя
    inner()
    print('outer:', x1)

outer()
print('global:', x1)

print()
print()

x2 = 0

def outer():

    global x2
    x2 = 1

    def inner():

        x2 = 2
        print('inner:', x2)
                                # вот что будет при global
    inner()
    print('outer:', x2)

outer()
print('global:', x2)


#%%
import numpy.random as rnd
import pandas as pd
#%%
# Number of samples
number_of_samples = 1000
#%%
# Distribution
def coin_flip():
    if rnd.random() >= 0.5: # Set distribtution
        return True
    else:
        return False
#%%
def simulation(number_of_samples):
    simulated_data = []
    for sample in range(0,number_of_samples):
        if coin_flip():
            simulated_data.append(1)
        else:
            simulated_data.append(0)
    return simulated_data
#%%
coin_simulation = simulation(number_of_samples)

# %%
coin_simulation[0:10]
# %%
coin_df = pd.DataFrame(coin_simulation)
# %%
coin_df.hist()
# %%
coin_df.describe()
# %%
def inside_circle(x,y):
    if (x**2 + y**2)**(1/2) <= 1: # Set distribtution
        return True
    else:
        return False

#%%
def pi_simulation(number_of_samples):

    x_list=[]
    y_list=[]
    is_inside_circle=[]

    for sample in range(0,number_of_samples):

        x = rnd.random()
        y = rnd.random()

        x_list.append(x)
        y_list.append(y)

        if inside_circle(x,y):
            is_inside_circle.append(1)
        else:
            is_inside_circle.append(0)
    return [x_list,y_list,is_inside_circle]

# %%
circle_df = pd.DataFrame(pi_simulation(number_of_samples))
# %%
circle_df = circle_df.T
# %%
circle_df
# %%
circle_df.columns = ['x','y','inside']
# %%
circle_df
# %%
circle_df.plot.scatter(
    x='x',
    y='y',
    c='inside',
    cmap = 'jet'
)
# %%
def estimate_pi(is_inside_circle):
    inside = 0
    for point in is_inside_circle:
        if point == 1:
            inside += 1
    pi = 4 * inside / len(is_inside_circle)
    return pi
#%%
circle_df.plot.scatter(
    x='x',
    y='y',
    c='inside',
    cmap = 'jet',
    title=f"Pi Estimate = {estimate_pi(circle_df['inside'])}"
)
# %%
def under_parabola(x,y):
    if y < x**2:
        return True
    else:
        return False
#%%
def parabola_simulator(number_of_samples):

    x_list=[]
    y_list=[]
    under_para=[]

    for sample in range(0,number_of_samples):

        x = rnd.random()
        y = rnd.random()
        x_list.append(x)
        y_list.append(y)

        if under_parabola(x,y):
            under_para.append(1)
        else:
            under_para.append(0)
    return [x_list,y_list,under_para]
#%%
para_df = pd.DataFrame(parabola_simulator(number_of_samples))
#%%
para_df = para_df.T
para_df.columns = ['x','y','under']
# %%
para_df.plot.scatter(
    x='x',
    y='y',
    c='under',
    cmap = 'jet'
)
# %%
para_df.hist()
# %%


import pandas

df = pandas.read_csv("/home/garrett/Desktop/Git_Repositories/Python_Practice/Exercises/Exercise_11/world-cities-population.csv")

cent = dict([(i,[a]) for i, a, in zip(df['Country or Area'],df['City'])])

cent_am = ["Belize","Costa Rica","El Salvador","Guatemala","Honduras","Nicaragua","Panama"]

city_cent_am = list()

for i in cent_am:
    city_cent_am.append(cent.get(i,("No cities listed from {0:}".format(i))))

print(city_cent_am)


from django import forms
from django.db import transaction
from django.utils.translation import gettext as _

from ..models import Duplicate, HelperShift

class MergeDuplicatesForm(forms.Form):
    def __init__(self, *args, **kwargs):
        self.helpers = kwargs.pop("helpers")

        super(MergeDuplicatesForm, self).__init__(*args, **kwargs)

        self.fields["helpers_ignore"] = forms.ModelMultipleChoiceField(
            queryset=self.helpers,
            widget=forms.CheckboxSelectMultiple(attrs={"id": "helper_ignore"}),
            required=False,
            label="",
        )

        self.fields["helpers_selection"] = forms.ModelChoiceField(
            queryset=self.helpers,
            widget=forms.RadioSelect(attrs={"id": "helper_selection"}),
            empty_label=None,
            required=True,
            label="",
        )

    def clean(self):
        cleaned_data = super().clean()
        remaining_helper = cleaned_data["helpers_selection"]
        ignore_helpers = cleaned_data["helpers_ignore"]

        # remaining helpers must not be ignored (this makes no sense)
        if remaining_helper in ignore_helpers:
            raise forms.ValidationError(_("The remaining helper must not be ignored."))

        # check for overlapping shifts
        if not self.check_merge_possible(ignore_helpers):
            raise forms.ValidationError(_("Cannot merge helpers which have the same shift."))

    @transaction.atomic
    def merge(self):
        """
        Merge the helpers and keep the data selected in the form.
        """
        remaining_helper = self.cleaned_data["helpers_selection"]
        ignore_helpers = self.cleaned_data["helpers_ignore"]
        oldest_timestamp = remaining_helper.timestamp

        # we check this again inside the atomic code block to ensure that no change happends after the
        # validation and before the merge (= no new shifts were added)
        if not self.check_merge_possible(ignore_helpers):
            raise ValueError("Cannot merge helpers with same shifts")

        # and then to the merge
        for helper in self.helpers:
            if helper == remaining_helper or helper in ignore_helpers:
                continue
            # merge shifts
            for helpershift in HelperShift.objects.filter(helper=helper):
                helpershift.helper = remaining_helper
                helpershift.save()

            # merged coordinated jobs
            for job in helper.coordinated_jobs:
                job.coordinators.add(remaining_helper)

            # merge gifts
            if remaining_helper.event.gifts:
                remaining_helper.gifts.merge(helper.gifts)

            # then create the duplicate entry so that old links in mails still work
            Duplicate.objects.create(deleted=helper.id, existing=remaining_helper)

            # the overall timestamp of the helper should be the oldest one
            # (there are multiple timestamps saved: one per helper and one per shift)
            if helper.timestamp < oldest_timestamp:
                oldest_timestamp = helper.timestamp

            # and delete the old helper
            helper.delete()

        # update timestamp
        remaining_helper.timestamp = oldest_timestamp
        remaining_helper.save()

        return remaining_helper

    def check_merge_possible(self, ignore_helpers=None):
        """
        Check if the merge is possible.

        It is not possible when multiple helpers have the same shift. If we would merge those helpers,
        we would "loose" allocated seats and this is probably not intended.
        """
        shifts = []
        for helper in self.helpers:
            # if we have ignored_helpers, check that
            if ignore_helpers and helper in ignore_helpers:
                continue

            # compare all shifts
            for shift in helper.shifts.all():
                if shift in shifts:
                    return False
                else:
                    shifts.append(shift)
        return True


# --------------
#Importing header files
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#Path of the file
path

#Code starts here

#Reading the file
data=pd.read_csv(path)

#Renaming a column
data.rename(columns={'Total':'Total_Medals'},inplace=True)

#Printing the first five columns
print(data.head(10))

#Code ends here

# --------------
#Code starts here
#Creating new column 'Better_Event'
data['Better_Event'] = np.where(data['Total_Summer']>data['Total_Winter'], 'Summer', 'Winter')
data['Better_Event'] =np.where(data['Total_Summer'] ==data['Total_Winter'],'Both',data['Better_Event'])

#Finding the value with max count in 'Better_Event' column
better_event=data['Better_Event'].value_counts().index.values[0]

#Printing the better event
print('Better_Event=', better_event)

# --------------
#Code starts here

#Subsetting the dataframe
top_countries=data[['Country_Name','Total_Summer', 'Total_Winter','Total_Medals']]

#Dropping the last column
top_countries=top_countries[:-1]

#Function for top 10
def top_ten(data, col):

    #Creating a new list
    country_list=[]

    #Finding the top 10 values of 'col' column
    country_list= list((data.nlargest(10,col)['Country_Name']))

    #Returning the top 10 list
    return country_list

#Calling the function for Top 10 in Summer
top_10_summer=top_ten(top_countries,'Total_Summer')
print("Top 10 Summer:\n",top_10_summer, "\n")

#Calling the function for Top 10 in Winter
top_10_winter=top_ten(top_countries,'Total_Winter')
print("Top 10 Winter:\n",top_10_winter, "\n")

#Calling the function for Top 10 in both the events
top_10=top_ten(top_countries,'Total_Medals')
print("Top 10:\n",top_10, "\n")

#Extracting common country names from all three lists
common=list(set(top_10_summer) & set(top_10_winter) & set(top_10))

print('Common Countries :\n', common, "\n")

#Code ends here

# --------------
#Code starts here
top_10_summer=top_ten(top_countries,'Total_Summer')
top_10_winter=top_ten(top_countries,'Total_Winter')
top_10=top_ten(top_countries,'Total_Medals')
df=pd.DataFrame([top_10_summer,top_10_winter,top_10])

summer_df= data[data['Country_Name'].isin(top_10_summer)]
winter_df= data[data['Country_Name'].isin(top_10_winter)]
top_df= data[data['Country_Name'].isin(top_10)]
summer_df.plot.bar('Country_Name','Total_Summer')
winter_df.plot.bar('Country_Name','Total_Winter')
top_df.plot.bar('Country_Name','Total_Medals')

# --------------
#Code starts here
summer_df= data[data['Country_Name'].isin(top_10_summer)]
summer_df['Golden_Ratio']=data['Gold_Summer']/data['Total_Summer']
summer_max_ratio=summer_df['Golden_Ratio'].max()
summer_country_gold=summer_df.loc[summer_df['Golden_Ratio'].idxmax(),'Country_Name']
winter_df= data[data['Country_Name'].isin(top_10_winter)]
winter_df['Golden_Ratio']=data['Gold_Winter']/data['Total_Winter']
winter_max_ratio=winter_df['Golden_Ratio'].max()
winter_country_gold=winter_df.loc[winter_df['Golden_Ratio'].idxmax(),'Country_Name']
top_df= data[data['Country_Name'].isin(top_10)]
top_df['Golden_Ratio']=data['Gold_Total']/data['Total_Medals']
top_max_ratio=top_df['Golden_Ratio'].max()
top_country_gold=summer_df.loc[top_df['Golden_Ratio'].idxmax(),'Country_Name']

# --------------
#Code starts here
data_1=data[:-1]
data_1['Total_Points']=(data['Gold_Total']*3)+(data['Silver_Total']*2)+(data['Bronze_Total'])
most_points=max(data_1['Total_Points'])
best_country=data_1.loc[data_1['Total_Points'].idxmax(),'Country_Name']
print(best_country)

# --------------
#Code starts here

#Subsetting the dataframe
best=data[data['Country_Name']==best_country]
best.reset_index(drop = True, inplace = True)
best=best[['Gold_Total','Silver_Total','Bronze_Total']]

#Plotting bar plot
best.plot.bar(stacked=True)

#Changing the x-axis label
plt.xlabel('United States')

#Changing the y-axis label
plt.ylabel('Medals Tally')

#Rotating the ticks of X-axis
plt.xticks(rotation=45)

#Updating the graph legend
l=plt.legend()
l.get_texts()[0].set_text('Gold_Total :' + str(best['Gold_Total'].values))
l.get_texts()[1].set_text('Silver_Total :' + str(best['Silver_Total'].values))
l.get_texts()[2].set_text('Bronze_Total :' + str(best['Bronze_Total'].values))

#Code ends here


import os
import sys
import math
from tqdm import tqdm
import pandas as pd
import numpy as np

sys.path.insert(1, os.path.join(sys.path[0], '..'))
from util import argparser

def permutation_test(df, column, n_permutations=100000, batch_size=1000):
    # Get actual batch size
    batch_size = min(batch_size, n_permutations)

    # Get real avg
    real_avg = df[column].mean().item()
    values = df[column].values
    values_exp = np.expand_dims(values, axis=1).repeat(batch_size, axis=1)

    # Make n_permutations divisible per batch_size
    n_batches = math.ceil(n_permutations / batch_size)
    n_permutations = n_batches * batch_size

    # Get number of permutations above
    n = 0
    for _ in range(n_batches):
        permut = np.random.randint(0, 2, size=(len(values), batch_size)) * 2 - 1

        random_avgs = np.mean(values_exp * permut, axis=0)
        n += (random_avgs >= real_avg).sum()

    return n / n_permutations, n_permutations

def remove_unused_cols(df):
    del df['item_id']
    del df['position']
    del df['family_size']
    del df['family_weight']
    del df['length']

    return df

def get_macroarea_counts(df):
    df_count = df[['macroarea', 'concept_id', 'token_idx']].groupby(['concept_id', 'token_idx']).agg('count').reset_index()
    df_count['macroarea_count'] = df_count['macroarea']
    del df_count['macroarea']
    df = pd.merge(df, df_count, left_on=['concept_id', 'token_idx'], right_on=['concept_id', 'token_idx'])

    return df

def get_tokens_means(df):
    df_new = df.groupby(['language_id', 'family', 'macroarea', 'concept_id', 'concept_name', 'token', 'token_idx']).agg('mean').reset_index()
    df_new = df_new.groupby(['family', 'macroarea', 'concept_id', 'concept_name', 'token', 'token_idx']).agg('mean').reset_index()
    df_new = df_new.groupby(['macroarea', 'concept_id', 'concept_name', 'token', 'token_idx']).agg('mean').reset_index()

    df_new = get_macroarea_counts(df_new)
    return df_new

def main():
    args = argparser.parse_args(csv_folder='cv')
    context = 'onehot'

    fname = os.path.join(args.rfolder_base, 'avg_seed_results_per_pos.tsv')
    df = pd.read_csv(fname, sep='\t')
    remove_unused_cols(df)

    # df_concepts = df.groupby(['concept_id', 'concept_name']).agg('mean').reset_index()
    df_tokens = get_tokens_means(df)
    df_tokens.set_index(['macroarea', 'concept_id', 'token_idx'], inplace=True)
    df_tokens = df_tokens.sort_index()
    df_tokens = df_tokens[df_tokens.macroarea_count == 4]

    df_tokens['p_value'] = -1
    df_tokens['n_permutations'] = -1
    df_tokens['n_instances'] = -1
    for macroarea, concept_id, token_idx in tqdm(df_tokens.index.unique(), desc='Concept--token permutation tests'):
        idx = (macroarea, concept_id, token_idx)
        df_temp = df[(df.macroarea == macroarea) & (df.concept_id == concept_id) & (df.token_idx == token_idx)]
        p_val, n_permutations = permutation_test(df_temp, 'mi-' + context, n_permutations=100000)
        # p_val, n_permutations = permutation_test_recursive(df_temp, 'mi-' + context, n_permutations=100000)

        df_tokens.loc[idx, 'p_value'] = p_val
        df_tokens.loc[idx, 'n_permutations'] = n_permutations
        df_tokens.loc[idx, 'n_instances'] = df_temp.shape[0]

    fname = os.path.join(args.rfolder_base, 'tokens_results.tsv')
    df_tokens.to_csv(fname, sep='\t')

if __name__ == '__main__':
    main()


def solution(arr):
    arr.sort()

    answer = 0 # total group
    member = 0 # current member
    for i in arr:
        member += 1
        if member >= i:
            member = 0
            answer += 1

    return answer

if __name__ == '__main__':
    arr = [2, 3, 1, 2, 2]
    print(solution(arr))


"""
The `test.unit.sha_api.mybottle.sha_api_bottle_test` module provides unit tests for the `ShaApiBottle` class in
`sha_api.mybottle.sha_api_bottle`.

Classes:
    TestShaApiBottle: A unit test class for the `ShaApiBottle` class.
"""

import json
import tempfile
import unittest

from bottle import ConfigDict # pylint: disable=no-name-in-module
from mock import MagicMock, patch
from sha_api.mybottle.sha_api_bottle import global_config, ShaApiBottle
from sha_api.sha_apid import ROUTES

import sha_api

class TestShaApiBottle(unittest.TestCase):
    """
    A unit test class for the `ShaApiBottle` class.

    Methods:
        setUp: Unit test initialization.
        test_error_handler: Tests to ensure the error handler returns a JSON value.
        test_global_config: Tests to ensure we get the correct values from a `ConfigDict` instance.
        test_sha_api_constructor: Tests that `ShaApiBottle` instance can be properly instantiated without throwing any
                                  exceptions.
    """

    def setUp(self):
        """
        Initializes the unit test global configs
        """

        self.maxDiff = None # pylint: disable=invalid-name
        self.config_sample = tempfile.NamedTemporaryFile(delete=False)
        self.dbfile = tempfile.NamedTemporaryFile(delete=False)
        self.os_environ = {u'SHA_API_CONFIG': self.config_sample.name}

        self.configdict_ns = ConfigDict().load_dict(
            {
                u'sha_api': {
                    u'test_variable': u'test_value'
                },
                u'sqlite': {
                    u'dbfile': self.dbfile.name
                }
            }
        )

        with open(self.config_sample.name, 'w') as fout:
            fout.write(u"[sha_api]\ntest_variable = test_value\n[sqlite]\ndbfile = %s" % self.dbfile.name)

    def test_error_handler(self):
        """
        Tests to ensure the error handler returns a JSON value.
        """

        res = MagicMock()
        res_json = json.dumps({u'err_msg': u'Response Body'})

        res.body = u'Response Body'

        api = ShaApiBottle(ROUTES)

        self.assertEqual(api.default_error_handler(res), res_json)

    def test_global_config(self):
        """
        Tests to ensure we get the correct values from a `ConfigDict` instance.
        """

        self.assertEqual(global_config(self.configdict_ns, u'sqlite', u'dbfile', u'Not Found'), self.dbfile.name)

    def test_sha_api_constructor(self):
        """
        Tests that `ShaApiBottle` instance can be properly instantiated without throwing any exceptions.
        """

        # Branch 1: Nothing throws an error and all goes well
        try:
            api = ShaApiBottle(ROUTES) # pylint: disable=unused-variable
        except Exception as err: # pylint: disable=broad-except
            self.fail(u'ShaApiBottle sha_api instance failed to initialize: %s' % str(err))

        # Branch 2: When routes object is not a list we get a proper assert error

        with self.assertRaises(AssertionError) as err:
            api = ShaApiBottle(dict())

        self.assertEqual(str(err.exception), u'routes must be an array of route dicts to be passed to bottle.route')

        # Branch 3: When routes object is a list but it doesnt contain dict items we get a proper assert error

        with self.assertRaises(AssertionError) as err:
            api = ShaApiBottle([False])

        self.assertEqual(str(err.exception), u'route must be a dict that can be passed to bottle.route')

        # Branch 4: When environment variable specifies config file it is properly loaded.

        with patch.dict(u'sha_api.mybottle.sha_api_bottle.os.environ', values=self.os_environ):
            self.assertEqual(self.os_environ[u'SHA_API_CONFIG'],
                             sha_api.mybottle.sha_api_bottle.os.environ.get(u'SHA_API_CONFIG'))

            api = ShaApiBottle(ROUTES)

            self.assertEqual(api.config.get(u'sha_api.test_variable'), u'test_value')
            self.assertEqual(api.config.get(u'sqlite.dbfile'), self.dbfile.name)

        # Branch 5: When any portion of the db initialization fails it should just bubble up the exception

        with patch(u'sha_api.mybottle.sha_api_bottle.sqlite3.connect') as sqlite_connect:
            self.assertEqual(sqlite_connect, sha_api.mybottle.sha_api_bottle.sqlite3.connect)

            sqlite_connect.side_effect = Exception(u'sqlite exception')

            with self.assertRaises(Exception) as err:
                api = ShaApiBottle()

            self.assertEqual(str(err.exception), u'sqlite exception')


#!/usr/bin/env python

'''
对测试集数据进行测试，统计所有数据平均的RRMSE,SNR和CC值
'''

import argparse
import os
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader
from utility.data import EEGData
from utility.conv_tasnet_v1 import TasNet
from utility.network import ResCNN, Novel_CNN2, Novel_CNN, fcNN
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import math
import scipy.io as io
from scipy.signal import butter, lfilter

EPS = 1e-8

parser = argparse.ArgumentParser('Evaluate separation performance using Conv-TasNet')
parser.add_argument('--model_path', type=str,
                    help='Path to model file created by training')
parser.add_argument('--data_dir', type=list, default=['./data/mixdata/foldtrain.txt',
                                                      './data/mixdata/foldtest.txt'],
                    help='directory of fold')
parser.add_argument('--use_cuda', type=int, default=1,
                    help='Whether use GPU')
parser.add_argument('--batch_size', default=128, type=int,
                    help='Batch size')
parser.add_argument('--num_workers', default=0, type=int,
                    help='Num_workers')

# Network architecture
parser.add_argument('--N', default=256, type=int,
                    help='Encode dim')
parser.add_argument('--B', default=64, type=int,
                    help='Feature dim')
parser.add_argument('--sr', default=512, type=int,
                    help='Sample rate')
parser.add_argument('--L', default=16, type=int,
                    help='Length of the filters in samples (16=16ms at 1kHZ)')
parser.add_argument('--X', default=6, type=int,
                    help='Number of convolutional blocks in each repeat')
parser.add_argument('--R', default=3, type=int,
                    help='Number of repeats')
parser.add_argument('--P', default=3, type=int,
                    help='Kernel size in convolutional blocks')
parser.add_argument('--C', default=1, type=int,
                    help='Number of speakers')

# 计算相关系数
def calc_corr(a, b):
    a_avg = sum(a) / len(a)
    b_avg = sum(b) / len(b)
    # 计算分子，协方差————按照协方差公式，本来要除以n的，由于在相关系数中上下同时约去了n，于是可以不除以n
    cov_ab = sum([(x - a_avg) * (y - b_avg) for x, y in zip(a, b)])
    # 计算分母，方差乘积————方差本来也要除以n，在相关系数中上下同时约去了n，于是可以不除以n
    sq = math.sqrt(sum([(x - a_avg) ** 2 for x in a]) * sum([(x - b_avg) ** 2 for x in b]))
    corr_factor = cov_ab / sq
    return corr_factor

def butter_lowpass(data, cutoff, fs, order=5):
    nyq = 0.5 * fs
    normal_cutoff = cutoff / nyq
    b, a = butter(order, normal_cutoff, btype='low', analog=False)
    y = lfilter(b, a, data)
    return y

def evaluate(args, snr_test=1):
    # Load model
    package = torch.load(args.model_path)
    model = TasNet(args.N, args.B, args.sr, args.L, args.X, args.R, args.P, args.C)
    # model = fcNN(lenth=1024)
    # model = Novel_CNN2(len_signal=1024)
    # model = ResCNN(1024)
    model.load_state_dict(package['model'])
    # print(model)
    # for name in model.state_dict():
    #     print(name)
    # print('encoder1:',model.state_dict()['encoder1.weight'])
    model.eval()
    if args.use_cuda:
        model.cuda()

    # Load data
    f_test = np.load('../eegdenoisenet/testdata512/test_eeg.npz')
    noiseEEG_test, EEG_test, SNRs_test = f_test['noiseEEG_test'], f_test['EEG_test'], f_test['SNRs_test']

    # 选择 信噪比
    idx = np.where(SNRs_test == snr_test)[0]
    # 不分档则以下2行注释
    noiseEEG_test = noiseEEG_test[idx]
    EEG_test = EEG_test[idx]

    evaluate_dataset = EEGData(noiseEEG_test, EEG_test)
    evaluate_loader = DataLoader(evaluate_dataset, batch_size=args.batch_size, shuffle=False,
                                 num_workers=args.num_workers)

    with torch.no_grad():
        Snr = 1.5
        RRMSEspec_total = []
        RRMSE_total = []
        CC_total = []
        SNR = []
        estimate_all = []
        for i, data in enumerate(evaluate_loader):
            # Get batch data

            eeg_mix = data['eeg_mix'].type(torch.float32)
            # emg = data['emg'].type(torch.float32)
            eeg_clean = data['eeg_clean'].type(torch.float32)

            lpeeg_mix = butter_lowpass(eeg_mix, 20, 500)
            # hpeeg_mix = butter_highpass(eeg_mix,100,500)

            eeg_mix = eeg_mix.type(torch.float32)

            lpeeg_mix = torch.from_numpy(lpeeg_mix).type(torch.float32)
            # hpeeg_mix = torch.from_numpy(hpeeg_mix).type(torch.float32)
            # lpeeg_clean = data['lpeeg_clean']

            # Forward
            if args.use_cuda:
                eeg_clean = eeg_clean.cuda()
                lpeeg_mix = lpeeg_mix.cuda()
                eeg_mix = eeg_mix.cuda()

            estimate_source1, estimate_source2 = model(lpeeg_mix, eeg_mix)
            # estimate_source2  = model(eeg_mix)
            e_noise = eeg_clean - estimate_source2.squeeze()
            snr = 10 * torch.log10(torch.sum(eeg_clean ** 2, dim=1) / (torch.sum(e_noise ** 2, dim=1) + EPS) + EPS)

            estimate2 = estimate_source2.cpu().numpy().squeeze()
            # emg = emg.numpy()
            eeg_clean = eeg_clean.cpu().numpy()
            estimate_all.append(estimate2)
            # eeg_mix = eeg_mix.numpy()

            for j in range(estimate2.shape[0]):
                eeg_snr = snr[j].item()
                SNR.append(eeg_snr)

                e_noise = eeg_clean[j, :] - estimate2[j, :]
                RRMSE = np.sqrt(np.sum(e_noise ** 2) / (np.sum(eeg_clean[j, :] ** 2) + EPS) + EPS)

                # TODO
                Pxx_eeg, _ = mlab.psd(eeg_clean[j, :], NFFT=1024)
                Pxx_estimate, _ = mlab.psd(estimate2[j, :], NFFT=1024)
                # Pxx_eeg = Pxx_eeg[:201]                # TODO
                # Pxx_estimate = Pxx_estimate[:201]      # TODO
                RRMSEspec = np.sqrt(np.sum((Pxx_estimate - Pxx_eeg) ** 2) / (np.sum(Pxx_eeg ** 2) + EPS) + EPS)

                eeg_RRMSE = RRMSE
                # 计算CC
                cc = calc_corr(eeg_clean[j, :], estimate2[j, :])
                RRMSEspec_total.append(RRMSEspec)
                RRMSE_total.append(eeg_RRMSE)
                CC_total.append(cc)
        estimate_all = np.vstack(estimate_all)
        io.savemat(os.path.join('result', f'result_{snr_test}.mat'),
                   {'estimate_EEG': estimate_all, 'eeg_mix': noiseEEG_test, 'eeg_clean': EEG_test})
        # plt.psd
        return RRMSE_total, CC_total, SNR, RRMSEspec_total

if __name__ == '__main__':
    args = parser.parse_args()
    args.model_path = '/mnt/DEV/han/eeg/DASTCN_grnFFT/checkpoint/EEGARNet_model/epoch89.pth.tar'
    out_dir = 'result'
    print(args)

    meanRRMSEs = []
    meanRRMSEspecs = []
    meanCCs = []
    meanSNRs = []
    for snr in np.linspace(-7, 2, 10):
        print(snr)
        RRMSE_total, CC_total, SNR, RRMSEspec_total = evaluate(args, snr_test=snr)

        meanRRMSE = np.round(np.mean(RRMSE_total), 4)
        meanRRMSEspec = np.round(np.mean(RRMSEspec_total), 4)
        meanSNR = np.round(np.mean(SNR), 4)
        varRRMSE = np.round(np.var(RRMSE_total), 4)
        meanCC = np.round(np.mean(CC_total), 4)
        varCC = np.round(np.var(CC_total), 4)

        print('meanRRMSEspec:', meanRRMSE)
        print('meanRRMSEspec:', meanRRMSEspec)
        print('meanCC:', meanCC)
        print('meanSNR:', meanSNR)
        print('*' * 10)
        meanRRMSEs.append(meanRRMSE)
        meanCCs.append(meanCC)
        meanSNRs.append(meanSNR)
        meanRRMSEspecs.append(meanRRMSEspec)

    os.makedirs(out_dir, exist_ok=True)
    io.savemat(os.path.join(out_dir, 'result_perSNR.mat'), {'meanRRMSEs': meanRRMSEs,
                                                            'meanCCs': meanCCs,
                                                            'meanSNRs': meanSNRs,
                                                            'meanRRMSEspec': meanRRMSEspecs})


import asyncio
import json
import logging
import logging.config
from dataclasses import dataclass

import yaml
from web3 import Web3
from web3._utils.filters import LogFilter

@dataclass
class FilterWrapper:
    event_filter: LogFilter
    pair_name: str
    oracle_address: str
    logger: logging.Logger

class BlockchainMonitor:

    def __init__(self):
        with open("spec.yaml", "r") as s:
            spec = yaml.safe_load(s)

        with open("logging.yaml", "r") as s:
            logging.config.dictConfig(yaml.safe_load(s))

        with open('abi.json', "r") as s:
            abi = json.load(s)

        w3 = Web3(Web3.HTTPProvider(spec['connection_settings']['alchemy_url']))

        self.filters = []
        pair_name_to_logger = {}

        for currency_pair in spec['currency_pairs']:
            for pair_name, pair_spec in currency_pair.items():
                for oracle_address in pair_spec['oracles_addresses']:
                    contract = w3.eth.contract(address=oracle_address, abi=abi)
                    pair_name_to_logger[pair_name] = pair_name_to_logger \
                        .get(pair_name, logging.getLogger(pair_name))
                    self.filters.append(FilterWrapper(
                        contract.events.AnswerUpdated.createFilter(fromBlock='latest'),
                        pair_name,
                        oracle_address,
                        pair_name_to_logger[pair_name]
                    ))

    @staticmethod
    def __handle_event(event, filter_wrapper):
        logger = filter_wrapper.logger
        logger.info("Price changes in pair {}. Oracle address: {}. Current price: {}, block number: {}, tx hash: {}"
                    .format(filter_wrapper.pair_name,
                            filter_wrapper.oracle_address,
                            event.args.current,
                            event.blockNumber,
                            event.transactionHash.hex()))

    async def __monitor(self, filter_wrapper, poll_interval):
        filter_wrapper.logger.info("Start monitor pair {}. Oracle address: {}".format(
            filter_wrapper.pair_name, filter_wrapper.oracle_address))
        while True:
            for AnswerUpdated in filter_wrapper.event_filter.get_new_entries():
                self.__handle_event(AnswerUpdated, filter_wrapper)
            await asyncio.sleep(poll_interval)

    def monitor(self):
        loop = asyncio.get_event_loop()
        try:
            for filter_wrapper in self.filters:
                asyncio.ensure_future(self.__monitor(filter_wrapper, 10))
            loop.run_forever()
        finally:
            loop.close()

if __name__ == "__main__":
    BlockchainMonitor().monitor()


from __future__ import division
from builtins import str
import numpy as np
import pandas as pd
import seaborn as sns
from .helpers import *
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['pdf.fonttype'] = 42

def plot(results, subjgroup=None, subjname='Subject Group', listgroup=None,
         listname='List', subjconds=None, listconds=None, plot_type=None,
         plot_style=None, title=None, legend=True, xlim=None, ylim=None,
         save_path=None, show=True, ax=None, **kwargs):
    """
    General plot function that groups data by subject/list number and performs analysis.

    Parameters
    ----------
    results : quail.FriedEgg
        Object containing results

    subjgroup : list of strings or ints
        String/int variables indicating how to group over subjects.  Must be
        the length of the number of subjects

    subjname : string
        Name of the subject grouping variable

    listgroup : list of strings or ints
        String/int variables indicating how to group over list.  Must be
        the length of the number of lists

    listname : string
        Name of the list grouping variable

    subjconds : list
        List of subject hues (str) to plot

    listconds : list
        List of list hues (str) to plot

    plot_type : string
        Specifies the type of plot. If list (default), the list groupings (listgroup)
        will determine the plot grouping. If subject, the subject groupings
        (subjgroup) will determine the plot grouping. If split (currenty just
        works for accuracy plots), both listgroup and subjgroup will determine
        the plot groupings

    plot_style : string
        Specifies the style of the plot.  This currently works only for
        accuracy and fingerprint plots. The plot style can be bar (default for
        accruacy plot), violin (default for fingerprint plots) or swarm.

    title : string
        The title of the plot

    legend : bool
        If true (default), a legend is plotted.

    ylim : list of numbers
        A ymin/max can be specified by a list of the form [ymin, ymax]

    xlim : list of numbers
        A xmin/max can be specified by a list of the form [xmin, xmax]

    save_path : str
        Path to save out figure.  Include the file extension, e.g.
        save_path='figure.pdf'

    show : bool
        If False, do not show figure, but still return ax handle (default True).

    ax : Matplotlib.Axes object or None
        A plot object to draw to. If None, a new one is created and returned.

    Returns
    ----------
    ax : matplotlib.Axes.Axis
        An axis handle for the figure

    """

    def plot_acc(data, plot_style, plot_type, listname, subjname, **kwargs):

        # set defaul style to bar
        plot_style = plot_style if plot_style is not None else 'bar'
        plot_type = plot_type if plot_type is not None else 'list'

        if plot_style is 'bar':
            plot_func = sns.barplot
        elif plot_style is 'swarm':
            plot_func = sns.swarmplot
        elif plot_style is 'violin':
            plot_func = sns.violinplot

        if plot_type is 'list':
            ax = plot_func(data=data, x=listname, y="Accuracy", **kwargs)
        elif plot_type is 'subject':
            ax = plot_func(data=data, x=subjname, y="Accuracy", **kwargs)
        elif plot_type is 'split':
            ax = plot_func(data=data, x=subjname, y="Accuracy", hue=listname, **kwargs)

        return ax

    def plot_temporal(data, plot_style, plot_type, listname, subjname, **kwargs):

        # set default style to bar
        plot_style = plot_style if plot_style is not None else 'bar'
        plot_type = plot_type if plot_type is not None else 'list'

        if plot_style is 'bar':
            plot_func = sns.barplot
        elif plot_style is 'swarm':
            plot_func = sns.swarmplot
        elif plot_style is 'violin':
            plot_func = sns.violinplot

        if plot_type is 'list':
            ax = plot_func(data=data, x=listname, y="Temporal Clustering Score", **kwargs)
        elif plot_type is 'subject':
            ax = plot_func(data=data, x=subjname, y="Temporal Clustering Score", **kwargs)
        elif plot_type is 'split':
            ax = plot_func(data=data, x=subjname, y="Temporal Clustering Score", hue=listname, **kwargs)

        return ax

    def plot_fingerprint(data, plot_style, plot_type, listname, subjname, **kwargs):

        # set default style to violin
        plot_style = plot_style if plot_style is not None else 'violin'
        plot_type = plot_type if plot_type is not None else 'list'

        if plot_style is 'bar':
            plot_func = sns.barplot
        elif plot_style is 'swarm':
            plot_func = sns.swarmplot
        elif plot_style is 'violin':
            plot_func = sns.violinplot

        if plot_type is 'list':
            ax = plot_func(data=tidy_data, x="Feature", y="Clustering Score", hue=listname, **kwargs)
        elif plot_type is 'subject':
            ax = plot_func(data=tidy_data, x="Feature", y="Clustering Score", hue=subjname, **kwargs)
        else:
            ax = plot_func(data=tidy_data, x="Feature", y="Clustering Score", **kwargs)

        return ax

    def plot_fingerprint_temporal(data, plot_style, plot_type, listname, subjname, **kwargs):

        # set default style to violin
        plot_style = plot_style if plot_style is not None else 'violin'
        plot_type = plot_type if plot_type is not None else 'list'

        if plot_style is 'bar':
            plot_func = sns.barplot
        elif plot_style is 'swarm':
            plot_func = sns.swarmplot
        elif plot_style is 'violin':
            plot_func = sns.violinplot

        order = list(tidy_data['Feature'].unique())
        if plot_type is 'list':
            ax = plot_func(data=data, x="Feature", y="Clustering Score", hue=listname, order=order, **kwargs)
        elif plot_type is 'subject':
            ax = plot_func(data=data, x="Feature", y="Clustering Score", hue=subjname, order=order, **kwargs)
        else:
            ax = plot_func(data=data, x="Feature", y="Clustering Score", order=order, **kwargs)

        return ax

    def plot_spc(data, plot_style, plot_type, listname, subjname, **kwargs):

        plot_type = plot_type if plot_type is not None else 'list'

        if plot_type is 'subject':
            ax = sns.lineplot(data = data, x="Position", y="Proportion Recalled", hue=subjname, **kwargs)
        elif plot_type is 'list':
            ax = sns.lineplot(data = data, x="Position", y="Proportion Recalled", hue=listname, **kwargs)
        ax.set_xlim(0, data['Position'].max())

        return ax

    def plot_pnr(data, plot_style, plot_type, listname, subjname, position, list_length, **kwargs):

        plot_type = plot_type if plot_type is not None else 'list'

        if plot_type is 'subject':
            ax = sns.lineplot(data = data, x="Position", y='Probability of Recall: Position ' + str(position), hue=subjname, **kwargs)
        elif plot_type is 'list':
            ax = sns.lineplot(data = data, x="Position", y='Probability of Recall: Position ' + str(position), hue=listname, **kwargs)
        ax.set_xlim(0,list_length-1)

        return ax

    def plot_lagcrp(data, plot_style, plot_type, listname, subjname, **kwargs):

        plot_type = plot_type if plot_type is not None else 'list'

        if plot_type is 'subject':
            ax = sns.lineplot(data=data[data['Position']<0], x="Position", y="Conditional Response Probability", hue=subjname, **kwargs)
            if 'ax' in kwargs:
                del kwargs['ax']
            sns.lineplot(data=data[data['Position']>0], x="Position", y="Conditional Response Probability", hue=subjname, ax=ax, legend=False, **kwargs)
        elif plot_type is 'list':
            ax = sns.lineplot(data=data[data['Position']<0], x="Position", y="Conditional Response Probability", hue=listname, **kwargs)
            if 'ax' in kwargs:
                del kwargs['ax']
            sns.lineplot(data=data[data['Position']>0], x="Position", y="Conditional Response Probability", hue=listname, ax=ax, legend=False, **kwargs)
        ax.set_xlim(-5,5)

        return ax

    # if no grouping, set default to iterate over each list independently
    subjgroup = subjgroup if subjgroup is not None else results.data.index.levels[0].values
    listgroup = listgroup if listgroup is not None else results.data.index.levels[1].values

    if subjconds:
        # make sure its a list
        if type(subjconds) is not list:
            subjconds=[subjconds]

        # slice
        idx = pd.IndexSlice
        results.data = results.data.sort_index()
        results.data = results.data.loc[idx[subjconds, :],:]

        # filter subjgroup
        subjgroup = filter(lambda x: x in subjconds, subjgroup)

    if listconds:
        # make sure its a list
        if type(listconds) is not list:
            listconds=[listconds]

        # slice
        idx = pd.IndexSlice
        results.data = results.data.sort_index()
        results.data = results.data.loc[idx[:, listconds],:]

    # convert to tiny and format for plotting
    tidy_data = format2tidy(results.data, subjname, listname, subjgroup, analysis=results.analysis, position=results.position)

    if not ax==None:
        kwargs['ax']=ax

    #plot!
    if results.analysis=='accuracy':
        ax = plot_acc(tidy_data, plot_style, plot_type, listname, subjname, **kwargs)
    elif results.analysis=='temporal':
        ax = plot_temporal(tidy_data, plot_style, plot_type, listname, subjname, **kwargs)
    elif results.analysis=='fingerprint':
        ax = plot_fingerprint(tidy_data, plot_style, plot_type, listname, subjname, **kwargs)
    elif results.analysis=='fingerprint_temporal':
        ax = plot_fingerprint_temporal(tidy_data, plot_style, plot_type, listname, subjname, **kwargs)
    elif results.analysis=='spc':
        ax = plot_spc(tidy_data, plot_style, plot_type, listname, subjname, **kwargs)
    elif results.analysis=='pfr' or results.analysis=='pnr':
        ax = plot_pnr(tidy_data, plot_style, plot_type, listname, subjname, position=results.position, list_length=results.list_length,  **kwargs)
    elif results.analysis=='lagcrp':
        ax = plot_lagcrp(tidy_data, plot_style, plot_type, listname, subjname, **kwargs)
    else:
        raise ValueError("Did not recognize analysis.")

    # add title
    if title:
        plt.title(title)

    if legend is False:
        try:
            ax.legend_.remove()
        except:
            pass

    if xlim:
        plt.xlim(xlim)

    if ylim:
        plt.ylim(ylim)

    if save_path:
        mpl.rcParams['pdf.fonttype'] = 42
        plt.savefig(save_path)

    return ax


import pigpio
from time import sleep

pi = pigpio.pi()

#set GPIO pins
channel = 17

light_on = 10;        #seconds
frequency =  100000;    #seconds

pi.set_mode(17,pigpio.INPUT)
pi.set_PWM_dutycycle(17,128)

pi.set_PWM_frequency(17,5000)

print(pi.get_PWM_frequency(17))

sleep(light_on)

pi.write(channel,0)


from django import forms
from django.forms import Textarea

from .models import Comment, Post

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ("text", "group", "image")
        widgets = {
            "text": Textarea(
                attrs={"class": "form-control", "placeholder": "Текст нового поста"}
            ),
        }

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ("text",)
        widgets = {
            "text": Textarea(
                attrs={
                    "class": "form-control",
                    "placeholder": "Текст нового комментарий",
                }
            ),
        }


# Копирование найденных надежных и ненадежных аудиозаписей по Социуму за 2017-2018 на архивный диск
import openpyxl, traceback
import os, string, sys, shutil
from collections import Counter
from lib import l, fine_snils_, read_config

FIND_CATALOG = '/media/da3/asteriskBeagleAl/'
#CHANGE_ON_WINDOWS = 'Z:/'
#OUTPUT_CATALOG = 'O:/Документы/Записи/'
OUTPUT_CATALOG = '/media/da3/backup/'
TRUSTREESTR = 'Надежные.xlsx'
PROBLEMREESTR = 'Остальные.xlsx'
REESTRS = '/home/da3/Beagle/потеряшкиАудиозаписи/реестры/'

def isSNILS(snils):
    if snils != None:
        t = str(snils).replace('\n',' ').replace('  ', ' ').replace('  ', ' ').replace('  ', ' ').strip()
        if len(t) > 11:
            if t[3] == '-' and t[7] == '-' and (t[11] == ' ' or t[11] == '_'):
                return True
            else:
                return False
        else:
            return False
    return False

def isAudio(audio):
    if audio != None:
        t = str(audio).replace('\n',' ').replace('  ', ' ').replace('  ', ' ').replace('  ', ' ').strip()
        t1 = t.split('/')[len(t.split(('/'))) - 1]
        if t1.endswith('.'):
            t1 = t1[:-1]
        if t1.endswith('.mp3') or t1.endswith('.wav'):
            t1 = t1[:-4]
        if len(t1) > 26:
            if t1[2] == '.' and t1[5] == '.' and t1[10] == '_' and (t1[13] == '-' or t1[13] == '_') and \
                    (t1[16] == '-' or t1[16] == '_'):
                return ['длинный', t1]
            elif len(''.join([char for i, char in enumerate(t1) if char in string.digits and i < 26])) == 25 \
                    and t1[14] == '_':
                return ['короткий', t1]
            else:
                return ['', audio]
        else:
            return ['', audio]
    return ['', audio]

def isSocium(audio):
    if audio != None:
        t = str(audio).replace('\n',' ').replace('  ', ' ').replace('  ', ' ').replace('  ', ' ').strip()
        t1 = t.split('/')[len(t.split(('/'))) - 1]
        if len(t1) > 26:
            if t1[2] == '.' and t1[5] == '.' and t1[10] == '_' and (t1[13] == '-' or t1[13] == '_') and \
                    (t1[16] == '-' or t1[16] == '_') and (t1[6:10] == '2017' or t1[6:10] == '2018'):
                return True
            elif len(''.join([char for i, char in enumerate(t1) if char in string.digits and i < 26])) == 25 \
                    and t1[14] == '_' and (t1[:4] == '2017' or t1[:4] == '2018'):
                return True
            else:
                return False
        else:
            return False
    return False

# расшифровка любой ошибки
def full_tb_write(*args):
    if not args:
        exc_type, exc_val, exc_tb = sys.exc_info()
        traceback.print_tb(exc_tb, file=sys.stdout)
    elif len(args) == 3:
        exc_type, exc_val, exc_tb = args
        traceback.print_tb(exc_tb, file=sys.stdout)
    elif len(args) == 1:
        exc_type, exc_val, exc_tb = args[0].__class__, args[0], args[0].__traceback__
        traceback.print_tb(exc_tb, file=sys.stdout)

snilsesTrust = {}
snilsesTrustShort = {}
wb = openpyxl.load_workbook(filename=TRUSTREESTR, read_only=True)
for sheetname in wb.sheetnames:
    sheet = wb[sheetname]
    if not sheet.max_row:
        print('Файл', TRUSTREESTR, 'Excel некорректно сохранен OpenPyxl. Откройте и пересохраните его')
        continue
    for j, row in enumerate(sheet.rows):
        snils = l(row[0].value)
        snilsTrustAudios = []
        for k, cell in enumerate(row):
            if k and cell.value:
                snilsTrustAudio = isAudio(cell.value)
                if snilsTrustAudio[1] not in snilsTrustAudios:
                    snilsTrustAudios.append(snilsTrustAudio[1])
                if snilsesTrust.get(snils, None):
                    if cell.value not in snilsesTrust[snils]:
                        snilsesTrust[snils].append(cell.value)
                else:
                    snilsesTrust[snils] = [cell.value]
        snilsesTrustShort[snils] = snilsTrustAudios

for i, snils in enumerate(snilsesTrust):
    sucess = False
    while not sucess:
        try:
            if not os.path.exists(OUTPUT_CATALOG + 'Выгрузки/' + fine_snils_(snils)):
                os.mkdir(OUTPUT_CATALOG + 'Выгрузки/' + fine_snils_(snils))
            audiofilesShort = []
            for audiofile in snilsesTrust[snils]:
                audiofileShort = isAudio(audiofile)[1]
                if os.path.exists(audiofile): #.replace(FIND_CATALOG, CHANGE_ON_WINDOWS)):
                    if audiofileShort in audiofilesShort:
                        if not os.path.exists(OUTPUT_CATALOG + 'Выгрузки/' + fine_snils_(snils) + '/' + audiofileShort +
                                              '-' + str(Counter(audiofilesShort)[audiofileShort]) + audiofile[-4:]):
                            shutil.copy(audiofile #.replace(FIND_CATALOG, CHANGE_ON_WINDOWS)
                                        , OUTPUT_CATALOG + 'Выгрузки/' + fine_snils_(snils) + '/' + audiofileShort +
                                        '-' + str(Counter(audiofilesShort)[audiofileShort]) + audiofile[-4:])
                            audiofilesShort.append(audiofileShort)
                    else:
                        if not os.path.exists(OUTPUT_CATALOG + 'Выгрузки/' + fine_snils_(snils) + '/' +
                                              audiofileShort + audiofile[-4:]):
                            shutil.copy(audiofile #.replace(FIND_CATALOG, CHANGE_ON_WINDOWS)
                                        , OUTPUT_CATALOG + 'Выгрузки/' + fine_snils_(snils) + '/' + audiofileShort +
                                        audiofile[-4:])
                            audiofilesShort.append(audiofileShort)
                else:
                    print('!!! Нет исходного файла', audiofile)
            sucess = True
        except Exception as e:
            full_tb_write(e)
            print('Ошибка - пробуем ещё раз')
    print('Скопировано', i, 'из', len(snilsesTrust))

print('\nТеперь Остальные\n')

snilsesProblem = {}
snilsesProblemShort = {}
wb = openpyxl.load_workbook(filename=PROBLEMREESTR, read_only=True)
for sheetname in wb.sheetnames:
    sheet = wb[sheetname]
    if not sheet.max_row:
        print('Файл', PROBLEMREESTR, 'Excel некорректно сохранен OpenPyxl. Откройте и пересохраните его')
        continue
    for j, row in enumerate(sheet.rows):
        snils = l(row[0].value)
        snilsProblemAudios = []
        for k, cell in enumerate(row):
            if k and cell.value:
                snilsProblemAudio = isAudio(cell.value)
                if snilsProblemAudio[1] not in snilsProblemAudios:
                    snilsProblemAudios.append(snilsProblemAudio[1])
                if snilsesProblem.get(snils, None):
                    if cell.value not in snilsesProblem[snils]:
                        snilsesProblem[snils].append(cell.value)
                else:
                    snilsesProblem[snils] = [cell.value]
        snilsesProblemShort[snils] = snilsProblemAudios

for i, snils in enumerate(snilsesProblem):
    sucess = False
    while not sucess:
        try:
            if not os.path.exists(OUTPUT_CATALOG + 'Остальные/' + fine_snils_(snils)):
                os.mkdir(OUTPUT_CATALOG + 'Остальные/' + fine_snils_(snils))
            audiofilesShort = []
            for audiofile in snilsesProblem[snils]:
                audiofileShort = isAudio(audiofile)[1]
                if os.path.exists(audiofile): #.replace(FIND_CATALOG, CHANGE_ON_WINDOWS)):
                    if audiofileShort in audiofilesShort:
                        if not os.path.exists(OUTPUT_CATALOG + 'Остальные/' + fine_snils_(snils) + '/' + audiofileShort +
                                              '-' + str(Counter(audiofilesShort)[audiofileShort]) + audiofile[-4:]):
                            shutil.copy(audiofile #.replace(FIND_CATALOG, CHANGE_ON_WINDOWS)
                                        , OUTPUT_CATALOG + 'Остальные/' + fine_snils_(snils) + '/' + audiofileShort +
                                        '-' + str(Counter(audiofilesShort)[audiofileShort]) + audiofile[-4:])
                            audiofilesShort.append(audiofileShort)
                    else:
                        if not os.path.exists(OUTPUT_CATALOG + 'Остальные/' + fine_snils_(snils) + '/' +
                                              audiofileShort + audiofile[-4:]):
                            shutil.copy(audiofile #.replace(FIND_CATALOG, CHANGE_ON_WINDOWS)
                                        , OUTPUT_CATALOG + 'Остальные/' + fine_snils_(snils) + '/' + audiofileShort +
                                        audiofile[-4:])
                            audiofilesShort.append(audiofileShort)
                else:
                    print('!!! Нет исходного файла', audiofile)
            sucess = True
        except Exception as e:
            full_tb_write(e)
            print('Ошибка - пробуем ещё раз')
    print('Скопировано', i, 'из', len(snilsesProblem))


import requests
import json
import urllib.parse
from django.conf import settings

def current_place():
    """
    現在地の緯度経度を取得する。

    Returns:
        int: 現在地の緯度、経度
    """
    geo_request_url = "https://get.geojs.io/v1/ip/geo.json"
    geo_data = requests.get(geo_request_url).json()
    # print(geo_data['latitude'])
    # print(geo_data['longitude'])
    return geo_data["latitude"], geo_data["longitude"]

def get_movie_theatre(latitude, longitude):
    """
    現在地の緯度、経度をインプットとして、付近の映画館リストを返す。

    Args:
        latitude (int): 現在地の緯度
        longitude (int): 現在地の経度
    """

    url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?language=ja&location=" + latitude + "," + longitude + "&radius=2000&type=movie_theater&key=" + settings.API_MAP
    # print(url)
    payload={}
    headers = {}

    response = requests.request("GET", url, headers=headers, data=payload)

    json_dict = response.json()

    movie_theatre = []

    if json_dict["status"] == "OK":
        for theatre in json_dict["results"]:
            movie_theatre.append((theatre["name"],theatre["name"]))
        print(movie_theatre)

    return movie_theatre

def search_theatre(search_text):
    """
    現在地の緯度、経度をインプットとして、付近の映画館リストを返す。

    Args:
        latitude (int): 現在地の緯度
        longitude (int): 現在地の経度
    """

    url = "https://maps.googleapis.com/maps/api/place/textsearch/json?language=ja&type=movie_theater&query=" + urllib.parse.quote(search_text) + "&key=" + settings.API_MAP
    # print(url)
    payload={}
    headers = {}

    response = requests.request("GET", url, headers=headers, data=payload)

    json_dict = response.json()

    movie_theatre = []

    if json_dict["status"] == "OK":
        for theatre in json_dict["results"]:
            movie_theatre.append((theatre["name"], theatre["name"]))
        # print(movie_theatre)
    # else:
    #     movie_theatre.append("Result nothing","Result nothing")

    return movie_theatre

if __name__ == "__main__":
    latitude, longitude = current_place()
    get_movie_theatre(latitude, longitude)
    search_theatre("TOHO 新宿")


import pandas as pd
import sqlite3

def connect_sqlite(db_file):
    with sqlite3.connect(db_file) as conn:
        conn.row_factory = sqlite3.Row
        cur = conn.cursor()
    return conn, cur

def get_dataframe(db_file, sql):
    conn, cur = connect_sqlite(db_file)
    df = pd.read_sql(sql,conn)
    if conn:
        conn.close()

    return df

def req_count(df):
    '''
        df : pandas dataframe
    '''
    df['datatime']

if __name__ == '__main__' :
    db_file = 'demklog_2017-01-21'
    sql = 'select * from demklog '
    df = get_dataframe(db_file, sql)
    #print df.head()

    df.plot()
#     df_tm = pd.TimeSeries( pd. to_datetime( df['timestamp'] ) )
#     print 'type df_tm=', type(df_tm),df_tm.head()

#     ddtest =  df['timestamp']
#
# #    dd1.resample('M')
#     print type(ddtest), ddtest [800:810]
#     print ddtest.resample('H')


import json
import pandas as pd
from os.path import join

PROJECT_PATH = '../../'

event = pd.read_csv(join(PROJECT_PATH, 'data', 'LinearSearchThreadEvent.csv'),
                    header=None,
                    names=['Id', 'RootEventId', 'UserIdentifier', 'CreationDate', 'DiffSeconds', 'EventSource',
                           'EventTarget',
                           'Referrer', 'Url', 'Query', 'FragmentIdentifier'],
                    dtype={
                        'Id': 'int64',
                        'RootEventId': pd.Int64Dtype(),
                        'UserIdentifier': 'str',
                        'CreationDate': 'str',
                        'DiffSeconds': pd.Int64Dtype(),
                        'EventSource': 'str',
                        'EventTarget': 'str',
                        'Referrer': 'str',
                        'Url': 'str',
                        'Query': 'str',
                        'FragmentIdentifier': 'str'
                    })
event['CreationDate'] = pd.to_datetime(event['CreationDate'], format='%Y-%m-%d %H:%M:%S')
event = event.sort_values(by=['RootEventId', 'CreationDate'], ascending=True)

REID2IDList = {}
for _, row in event.iterrows():
    if pd.isna(row['RootEventId']):
        REID2IDList[str(row['Id'])] = [str(row['Id'])]
    else:
        if str(row['RootEventId']) not in REID2IDList.keys():
            REID2IDList[str(row['RootEventId'])] = [str(row['Id'])]
        else:
            REID2IDList[str(row['RootEventId'])].append(str(row['Id']))

with open(join(PROJECT_PATH, 'data', 'REID2IDList.json'), "w", encoding="utf-8") as fw:
    json.dump(REID2IDList, fw)