from aiogram import types
from keyboards.inline.analitic_inline import tasks, tasks_sen
from loader import dp
from states.state import StateBot
from aiogram.dispatcher import FSMContext
from mail.googlemail import mail
from handlers.users.quiz import cur, con
from keyboards.inline.get_mail import get_mail

anal_count = 0

@dp.message_handler(text='🚪Начать🚪', state=StateBot.In_choice_an)
async def bot_an_st(message: types.Message, state: FSMContext):
    await message.answer('Настал ваш первый рабочий день в новой квалификации. Вам выпала задача выбрать все значения из таблицы Orders. Как это можно сделать?')
    await StateBot.In_huaysk_an.set()

@dp.message_handler(state=StateBot.In_huaysk_an)
async def bot_an(message: types.Message, state: FSMContext):
    global anal_count
    if message.text == "SELECT * FROM Orders":
        anal_count += 1
        await message.answer("Необходимо осуществить формализацию модели. А что такое собственно формализаиция?", reply_markup=tasks)
        await StateBot.In_task_an.set()
        await message.delete()
    else:
        await message.answer("Необходимо осуществить формализацию модели. А что такое собственно формализаиция?", reply_markup=tasks)
        await StateBot.In_task_an.set()
        await message.delete()

@dp.callback_query_handler(text = ["true", "false"], state=StateBot.In_task_an)
async def bot_ann(call: types.CallbackQuery, state: FSMContext):
    global anal_count
    if call.data == "true":
        await call.message.delete()
        anal_count += 1
        await StateBot.In_Result.set()
        await call.message.answer("Как в excel таблице отделить целую часть от дробной?", reply_markup=tasks_sen)
    elif call.data == "false":
        await StateBot.In_Result.set()
        await call.message.delete()
        await call.message.answer("Как в excel таблице отделить целую часть от дробной?", reply_markup=tasks_sen)

@dp.callback_query_handler(text=["true_sen", "false_sen"], state=StateBot.In_Result)
async def bot_an_last(call: types.CallbackQuery, state: FSMContext):
    global anal_count
    if call.data == "true_sen":
        anal_count += 1
        if anal_count >= 2:
            counts = ["Аналитик", "Оффер", call.from_user.id]
            cur.execute("UPDATE PLAYERS SET PROFESSION = %s,FINISH = %s WHERE TG_ID = %s", counts)
            con.commit()
            await call.message.answer("Вы показали себя неплохо. \n\nМы считаем, что вы готовы к полноценной работе. \n\nЧтобы отправить заявку, заполните анкету по ссылке: \n\nhttps://talents.centrinvest.ru", reply_markup=get_mail)
            await StateBot.Set_mail_an.set()
        elif anal_count <= 1:
            counts = ["Аналитик", "Не оффер", call.from_user.id]
            cur.execute("UPDATE PLAYERS SET PROFESSION = %s,FINISH = %s WHERE TG_ID = %s", counts)
            con.commit()
            msg = f"К сожалению, вы еще не готовы работать в крупной кампании в качестве тестировщика" + \
                  f"\nОбогатиться знаниями помогут следующие источники:" + \
                  f"\n\nhttps://stepik.org/course/96145?search=991959224" + \
                  f"\n\nhttps://stepik.org/course/102480?search=991959217" + \
                  f"\n\nhttps://stepik.org/course/129?search=991959212"
            await call.message.answer(msg, reply_markup=get_mail)
            await StateBot.Set_mail_an.set()
    elif call.data == "false_sen":
        counts = ["Аналитик", "Не оффер", call.from_user.id]
        cur.execute("UPDATE PLAYERS SET PROFESSION = %s,FINISH = %s WHERE TG_ID = %s", counts)
        con.commit()
        msg = f"К сожалению, вы еще не готовы работать в крупной кампании в качестве тестировщика" + \
              f"\nОбогатиться знаниями помогут следующие источники:" + \
              f"\n\nhttps://stepik.org/course/96145?search=991959224" + \
              f"\n\nhttps://stepik.org/course/102480?search=991959217"+ \
              f"\n\nhttps://stepik.org/course/129?search=991959212"
        await call.message.answer(msg, reply_markup=get_mail)
        await StateBot.Set_mail_an.set()

@dp.callback_query_handler(text=['mail', 'stope_kuda_gonish'], state=StateBot.Set_mail_an)
async def bot_gm(call: types.CallbackQuery, state: FSMContext):
    if call.data == 'mail':
        await call.message.answer("Напишите свою почту пожалуйста")
        await StateBot.Get_mail_an.set()
    if call.data == 'stope_kuda_gonish':
        await state.finish()
        await call.message.answer("Спасибо за игру!")

@dp.message_handler(state=StateBot.Get_mail_an)
async def bot_sm(message: types.Message, state: FSMContext):
    id = [message.from_user.id]
    cur.execute("SELECT NAME, PROG_COUNT, TESTER_COUNT, ANALYSE_COUNT, ADMIN_COUNT FROM PLAYERS WHERE TG_ID = %s", id)
    raws = cur.fetchall()
    p = raws[0][1]
    t = raws[0][2]
    a = raws[0][3]
    an = raws[0][4]
    cur.execute("SELECT PROFESSION, FINISH FROM PLAYERS WHERE TG_ID = %s", id)
    raws_t = cur.fetchall()
    prof = raws_t[0][0]
    fin = raws_t[0][1]
    msg_mail = "Ваша статистика по результатам игры:" + \
               f"\nИмя::{raws[0][0]}" + \
               f"\nКоличество очков программиста:{p}" + \
               f"\nКоличество очков тестировщика:{t}" + \
               f"\nКоличество очков аналитика:{a}" + \
               f"\nКоличество очков администратора:{an}" + \
               f"\nИтоговая специальность: {prof}" + \
               f"\n{fin}"
    try:
        mail(msg_mail, message.text)
        await message.answer("Сообщение успешно отправлено")
        await state.finish()
    except Exception:
        await message.answer("Неправильная почта!")


import pyaudio
import struct
import subprocess
import math
import numpy as np
from scipy.fftpack import fft
import RPi.GPIO as GPIO

CHUNK = 1024 * 2
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100

p = pyaudio.PyAudio()
PinLowFreq = 27
PinMidFreq = 14
PinHighFreq = 17

micVolume = 8

maxVolume = 120

dutyCycle = 100

GPIO.setmode(GPIO.BCM)
GPIO.setup(PinLowFreq, GPIO.OUT)
GPIO.setup(PinMidFreq, GPIO.OUT)
GPIO.setup(PinHighFreq, GPIO.OUT)

PWMControllerLow = GPIO.PWM(PinLowFreq, dutyCycle)
PWMControllerMid = GPIO.PWM(PinMidFreq, dutyCycle)
PWMControllerHigh = GPIO.PWM(PinHighFreq, dutyCycle)

PWMControllerLow.start(dutyCycle)
PWMControllerMid.start(dutyCycle)
PWMControllerHigh.start(dutyCycle)

stream = p.open(
    format=FORMAT,
    channels=CHANNELS,
    rate=RATE,
    input=True,
    output=True,
    frames_per_buffer=CHUNK
)

x = np.arange(0, 2 * CHUNK, 2)
x_fft = np.linspace(0, RATE, CHUNK)
print(x_fft[int(len(x_fft)/2)])

pastFiveMicSamples = [0,0,0,0,0]

i = 0
x_fft = x_fft[:len(x_fft) // 2]
while True:
    data = stream.read(CHUNK, exception_on_overflow = False)
    data_int = np.array(struct.unpack(str(2 * CHUNK) + "B", data), dtype='b')[::2] #+ 127

    y_fft = fft(data_int) # core algorithm
    # was temp_fft = np.abs(y_fft[0:CHUNK]) * 2 / (256 * CHUNK)
    y_fft = np.abs(y_fft[0:CHUNK]) * 2 / (256 * CHUNK)

    y_fft = y_fft[:len(y_fft)//2]
    maxVolumeOccurances = np.count_nonzero(data_int >= maxVolume)
    pastFiveMicSamples.pop(4)
    pastFiveMicSamples.insert(0, maxVolumeOccurances)
    mean = 0.0
    for i in range(len(pastFiveMicSamples)):
        mean = mean + pastFiveMicSamples[i]
    mean = mean / 10.0
    if mean >= 10.0 and micVolume != 1:
        pastFiveMicSamples = [0,0,0,0,0]
        micVolume = micVolume - 1
        print("Lowering volume to: " + str(micVolume) + "%")
        subprocess.Popen(['amixer', 'set', 'Micro', str(micVolume) + '%', '>', '/dev/null'], cwd="./")
    elif mean == 0 and micVolume < 8:
        pastFiveMicSamples = [0,0,0,0,0]
        micVolume = micVolume + 1
        print("Increasing volume to: " + str(micVolume) + "%")
        subprocess.Popen(['amixer', 'set', 'Micro', str(micVolume) + '%', '>', '/dev/null'], cwd="./")

    frequencyIndexes = np.argsort(y_fft)    # will have the positions of the lowest to highest frequency

    low = 0
    mid = 0
    high = 0
    # now add up the fft values
    for j in range(int(len(x_fft)/8)):
        # now split the frequencies
        if x_fft[frequencyIndexes[-1 * j]] > 20 and x_fft[frequencyIndexes[-1 * j]] < 600:
            low = low + y_fft[frequencyIndexes[-1 * j]] # was (len(x_fft) - j ) #* credit
        elif x_fft[frequencyIndexes[-1 * j]] >= 600 and x_fft[frequencyIndexes[-1 * j]] < 1100:
            mid = mid + y_fft[frequencyIndexes[-1 * j]] # 1 # was (len(x_fft) - j) #* credit
        elif x_fft[frequencyIndexes[-1 * j]] >= 1100 and x_fft[frequencyIndexes[-1 * j]] < 2000:
            high = high + y_fft[frequencyIndexes[-1 * j]] #1 # was (len(x_fft) - j) #* credit

    low = round(low, 5)
    mid = round(mid, 5)
    high = round(high, 5)
    total = low + mid + high
    if total > 0:
        # get the percentage and make the percentage relative to the highest one
        lowPercentage = round((((low / total) * 100.0 * 1) ** 1), 2)
        midPercentage = round((((mid / total) * 100.0 * 1) ** 1), 2)
        highPercentage = round((((high / total) * 100.0 * 1) ** 1), 2)
        if lowPercentage > midPercentage and lowPercentage > highPercentage:
            lowPercentage = (lowPercentage / lowPercentage) * 100
            midPercentage = (midPercentage / lowPercentage) * 100
            highPercentage =  (highPercentage / lowPercentage) * 100

        elif midPercentage > lowPercentage and midPercentage > highPercentage:
            lowPercentage = (lowPercentage / midPercentage) * 100
            midPercentage = (midPercentage / midPercentage) * 100
            highPercentage = (highPercentage / midPercentage) * 100

        elif highPercentage > lowPercentage and highPercentage > midPercentage:
            lowPercentage = (lowPercentage / highPercentage) * 100
            midPercentage = (midPercentage / highPercentage) * 100
            highPercentage = (highPercentage / highPercentage) * 100

        if lowPercentage < midPercentage and lowPercentage < highPercentage:
            lowPercentage = 0
        elif midPercentage < lowPercentage and midPercentage < highPercentage:
            midPercentage = 0
        elif highPercentage < lowPercentage and highPercentage < midPercentage:
            highPercentage = 0

        i = i + 1

        PWMControllerLow.ChangeDutyCycle(lowPercentage)
        PWMControllerMid.ChangeDutyCycle(midPercentage)
        PWMControllerHigh.ChangeDutyCycle(highPercentage)


# coding: utf-8

import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot = True)

# visualize an example and the label

samX = mnist.train.images[50]
samy = mnist.train.labels[50]
plt.imshow(samX.reshape(28,28))
plt.show()
samy

# initialize the network size
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500
n_classes = 10
batch_size = 128
x = tf.placeholder('float', [None, 784])
y = tf.placeholder('float')

# Build the neural network
def neural_network_model(data):
    # define weights and biases for all each layer
    hidden_layer_1 = {'weights':tf.Variable(tf.random_normal([784, n_nodes_hl1])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
    hidden_layer_2 = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
    hidden_layer_3 = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}
    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
                    'biases':tf.Variable(tf.random_normal([n_classes]))}
    # feed forward and activations
    l1 = tf.add(tf.matmul(data, hidden_layer_1['weights']), hidden_layer_1['biases'])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(tf.matmul(l1, hidden_layer_2['weights']), hidden_layer_2['biases'])
    l2 = tf.nn.relu(l2)

    l3 = tf.add(tf.matmul(l2, hidden_layer_3['weights']), hidden_layer_3['biases'])
    l3 = tf.nn.relu(l3)

    output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']

    return output

# train the network
def train_neural_network(x):
    prediction = neural_network_model(x)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction, y))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

    epochs = 10
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        for epoch in range(epochs):
            epoch_loss = 0
            for batch in range(int(mnist.train.num_examples/batch_size)):
                epoch_x, epoch_y = mnist.train.next_batch(batch_size)
                batch, c = sess.run([optimizer, cost], feed_dict = {x: epoch_x, y:epoch_y})
                epoch_loss += c

            print('Epoch', epoch, 'completed out of', epochs, 'loss:', epoch_loss)

        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
        print('Accuracy: ', accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))

# run

train_neural_network(x)


from brownie import network, Collectible
from numpy import number
from scripts.helpful_scripts import OPENSEA_URL, get_account, get_nft
import json

def main():

    print(f"Working on {network.show_active()}")
    collectible = Collectible[-1]
    number_of_collectibles = collectible.tokenCounter()
    print(f"You have {number_of_collectibles} tokenIds")
    for token_id in range(number_of_collectibles):
        girl = get_nft(collectible.tokenIdToGirl(token_id))
        if not collectible.tokenURI(token_id).startswith("https://"):
            print(f"Setting tokenURI of {token_id}")
            set_tokenURI(token_id, collectible, girl)

def set_tokenURI(token_id, nft_contract, girl):
    account = get_account()
    with open("./tokensURIs.json", "r") as file:
        nftTotokenURI = json.load(file)
        tokenURI = nftTotokenURI[girl]
    tx = nft_contract.setTokenURI(token_id, tokenURI, {"from": account})
    tx.wait(1)
    print(
        f"Awesome! You can view your NFT at {OPENSEA_URL.format(nft_contract.address, token_id)}"
    )
    print("Please wait up to 20 minutes, and hit the refresh metadata button")


from django.contrib import auth
from django.core.exceptions import ImproperlyConfigured
from django.utils.functional import SimpleLazyObject
from publication_backbone.utils.auth import basic_auth

#---------------------------------
import logging
from django.conf import settings
logger = logging.getLogger(settings.PROJECT_NAME)
#---------------------------------

def get_user(request):
    if not hasattr(request, '_cached_http_auth_user'):
        request._cached_http_auth_user = basic_auth(request)
    return request._cached_http_auth_user

class HTTPAuthRemoteUserMiddleware(object):
    """
    Middleware for utilizing Web-server-provided authentication.

    If request.user is not authenticated, then this middleware attempts to
    authenticate the username passed in the ``HTTP_AUTHORIZATION`` request header.
    If authentication is successful, the user is automatically logged in to
    persist the user in the session.

    The header used is configurable and defaults to ``HTTP_AUTHORIZATION``.  Subclass
    this class and change the ``header`` attribute if you need to use a
    different header.
    """

    # Name of request header to grab username from.  This will be the key as
    # used in the request.META dictionary, i.e. the normalization of headers to
    # all uppercase and the addition of "HTTP_" prefix apply.
    header = "HTTP_AUTHORIZATION"

    def process_request(self, request):
        # AuthenticationMiddleware is required so that request.user exists.
        if not hasattr(request, 'user'):
            raise ImproperlyConfigured(
                "The Django remote user auth middleware requires the"
                " authentication middleware to be installed.  Edit your"
                " MIDDLEWARE_CLASSES setting to insert"
                " 'django.contrib.auth.middleware.AuthenticationMiddleware'"
                " before the HTTPAuthRemoteUserMiddleware class.")

        if not self.header in request.META:
            return

        # If the user is already authenticated and that user is the user we are
        # getting passed in the headers, then the correct user is already
        # persisted in the session and we don't need to continue.

        user = SimpleLazyObject(lambda: get_user(request))

        if user:
            # User is valid.  Set request.user and persist user in the session
            # by logging the user in.
            request.user = user
            auth.login(request, user)


# © 2018–2020 io mintz <io@mintz.cc>
#
# Emote Manager is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Emote Manager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Emote Manager. If not, see <https://www.gnu.org/licenses/>.

"""various utilities for use within the bot"""

import asyncio

import aiohttp
import discord

def format_user(bot, id, *, mention=False):
    """Format a user ID for human readable display."""
    user = bot.get_user(id)
    if user is None:
        return f"Unknown user with ID {id}"
    # not mention: @null byte#8191 (140516693242937345)
    # mention: <@140516693242937345> (null byte#8191)
    # this allows people to still see the username and discrim
    # if they don't share a server with that user
    if mention:
        return f"{user.mention} (@{user})"
    else:
        return f"@{user} ({user.id})"

def format_http_exception(exception: discord.HTTPException):
    """Formats a discord.HTTPException for relaying to the user.
    Sample return value:

    BAD REQUEST (status code: 400):
    Invalid Form Body
    In image: File cannot be larger than 256 kb.
    """

    # why is the name different between response types?
    if isinstance(exception.response, aiohttp.ClientResponse):
        status_code = exception.response.status
    else:
        status_code = exception.response.status_code

    return (
        f"{exception.response.reason} (status code: {status_code}):"
        f"\n{exception.text}"
    )

def strip_angle_brackets(string):
    """Strip leading < and trailing > from a string.
    Useful if a user sends you a url like <this> to avoid embeds, or to convert emotes to reactions.
    """
    if string.startswith("<") and string.endswith(">"):
        return string[1:-1]
    return string

async def gather_or_cancel(*awaitables, loop=None):
    """run the awaitables in the sequence concurrently. If any of them raise an exception,
    propagate the first exception raised and cancel all other awaitables.
    """
    gather_task = asyncio.gather(*awaitables)
    try:
        return await gather_task
    except asyncio.CancelledError:
        raise
    except:
        gather_task.cancel()
        raise


import sys
import torch
from torchinfo import summary

sys.path.insert(0, '../')
import constants
from models.diffusion.denoising_diffusion import Unet, GaussianDiffusion, Trainer

if __name__ == "__main__":

    model = Unet(
        dim = constants.DIM, # 256
        dim_mults = constants.DIM_MULTS,
        channels = constants.CHANNELS
    )

    diffusion = GaussianDiffusion(
        denoise_fn = model,
        image_size = 512,
        n_mels = constants.NUM_CHANNELS,
        n_samples = constants.TARGET_SAMPLES,
        channels = constants.CHANNELS,
        timesteps = constants.TIMESTEPS,   # number of steps
        loss_type = constants.LOSS_TYPE    # L1 or L2

    )

    summary(diffusion, input_size=(constants.BATCH_SIZE, 1, 128, 1024))

    # trainer = Trainer(
    #     diffusion,
    #     constants.TRAIN_DATA,
    #     constants.SAMPLE_RATE,
    #     constants.TARGET_SAMPLES,
    #     constants.DEVICE,
    #     train_batch_size = constants.BATCH_SIZE,
    #     train_num_steps = constants.TRAIN_STEPS,
    #     gradient_accumulate_every = constants.GRADIENT_ACCUMULATION,
    #     save_and_sample_every = constants.SAVE_SAMPLES_EVERY
    # )

    # trainer.train()


from PySide2.QtCore import QObject, Slot
import chardet

class FileUtil(QObject):
    @Slot(str, result='QVariant')
    def readFile(self, file_path):
        print(file_path)
        # the_file = open(file_path, 'r', encoding ='utf-8')
        the_file = open(file_path, 'rb')
        data_byte = the_file.read()
        result = chardet.detect(data_byte)
        the_file.close()
        the_encoding = result.get('encoding')
        the_file = open(file_path, 'r', encoding=the_encoding)
        data_text = the_file.read()
        return data_text


import logging
import math
from deltacat.compute.stats.models.delta_stats import DeltaStats
from deltacat.constants import (
    PYARROW_INFLATION_MULTIPLIER,
    BYTES_PER_MEBIBYTE,
    MEMORY_TO_HASH_BUCKET_COUNT_RATIO,
)

from deltacat.storage import (
    PartitionLocator,
    Delta,
    ManifestEntry,
    interface as unimplemented_deltacat_storage,
)
from deltacat import logs
from deltacat.compute.compactor import DeltaAnnotated
from typing import Dict, List, Optional, Tuple, Union, Any
from deltacat.compute.compactor import HighWatermark
from deltacat.compute.compactor.model.compaction_session_audit_info import (
    CompactionSessionAuditInfo,
)

logger = logs.configure_deltacat_logger(logging.getLogger(__name__))

def discover_deltas(
    source_partition_locator: PartitionLocator,
    high_watermark: Union[HighWatermark, int],
    last_stream_position_to_compact: int,
    compacted_partition_locator: Optional[PartitionLocator],
    rebase_source_partition_locator: Optional[PartitionLocator],
    rebase_source_partition_high_watermark: Optional[int],
    deltacat_storage=unimplemented_deltacat_storage,
    deltacat_storage_kwargs: Optional[Dict[str, Any]] = {},
    list_deltas_kwargs: Optional[Dict[str, Any]] = {},
) -> Tuple[List[Delta], int]:
    if deltacat_storage_kwargs is None:
        deltacat_storage_kwargs = {}
    # Source One: new deltas from uncompacted table for incremental compaction or deltas from compacted table for rebase
    start_position_exclusive = (
        high_watermark.get(source_partition_locator)
        if isinstance(high_watermark, dict)
        else high_watermark
    )
    input_deltas = _discover_deltas(
        source_partition_locator,
        start_position_exclusive,
        last_stream_position_to_compact
        if not rebase_source_partition_locator
        else deltacat_storage.get_partition(
            source_partition_locator.stream_locator,
            source_partition_locator.partition_values,
            **deltacat_storage_kwargs,
        ).stream_position,
        deltacat_storage,
        deltacat_storage_kwargs,
        list_deltas_kwargs,
    )

    # Source Two: delta from compacted table for incremental compaction or new deltas from uncompacted table for rebase
    previous_last_stream_position_compacted = -1
    if not rebase_source_partition_locator:  # compacted table
        compacted_partition = deltacat_storage.get_partition(
            compacted_partition_locator.stream_locator,
            compacted_partition_locator.partition_values,
            **deltacat_storage_kwargs,
        )
        previous_last_stream_position_compacted = (
            compacted_partition.stream_position if compacted_partition else -1
        )
        input_deltas_compacted = []
        if previous_last_stream_position_compacted > 0:
            input_deltas_compacted = _discover_deltas(
                compacted_partition_locator,
                None,
                previous_last_stream_position_compacted,
                deltacat_storage,
                deltacat_storage_kwargs,
                list_deltas_kwargs,
            )
        logger.info(
            f"Length of input deltas from uncompacted table {len(input_deltas)} up to {last_stream_position_to_compact},"
            f"Length of input deltas from compacted table {len(input_deltas_compacted)} up to {high_watermark}"
        )
        input_deltas += input_deltas_compacted
    else:  # new deltas from uncompacted table between previous_last_stream_position_compacted and current last_position_to_compact
        input_deltas_new = _discover_deltas(
            rebase_source_partition_locator,
            rebase_source_partition_high_watermark,
            last_stream_position_to_compact,
            deltacat_storage,
            deltacat_storage_kwargs,
            list_deltas_kwargs,
        )
        logger.info(
            f"Length of input deltas from uncompacted table {len(input_deltas_new)} up to {last_stream_position_to_compact},"
            f"Length of input deltas from compacted table {len(input_deltas)} up to {rebase_source_partition_high_watermark}"
        )
        input_deltas += input_deltas_new

    return input_deltas, previous_last_stream_position_compacted

def limit_input_deltas(
    input_deltas: List[Delta],
    cluster_resources: Dict[str, float],
    hash_bucket_count: int,
    user_hash_bucket_chunk_size: int,
    input_deltas_stats: Dict[int, DeltaStats],
    compaction_audit: CompactionSessionAuditInfo,
    deltacat_storage=unimplemented_deltacat_storage,
    deltacat_storage_kwargs: Optional[Dict[str, Any]] = None,
    **kwargs,
) -> Tuple[List[DeltaAnnotated], int, HighWatermark, bool]:
    # TODO (pdames): when row counts are available in metadata, use them
    #  instead of bytes - memory consumption depends more on number of
    #  input delta records than bytes.

    # we assume here that we're running on a fixed-size cluster
    # this assumption could be removed, but we'd still need to know the max
    # resources we COULD get for this cluster, and the amount of memory
    # available per CPU should remain fixed across the cluster.
    if deltacat_storage_kwargs is None:
        deltacat_storage_kwargs = {}
    worker_cpus = int(cluster_resources["CPU"])
    worker_obj_store_mem = float(cluster_resources["object_store_memory"])
    logger.info(f"Total worker object store memory: {worker_obj_store_mem}")
    worker_obj_store_mem_per_task = worker_obj_store_mem / worker_cpus
    logger.info(f"Worker object store memory/task: " f"{worker_obj_store_mem_per_task}")
    worker_task_mem = cluster_resources["memory"]
    logger.info(f"Total worker memory: {worker_task_mem}")
    # TODO (pdames): ensure fixed memory per CPU in heterogenous clusters
    worker_mem_per_task = worker_task_mem / worker_cpus
    logger.info(f"Cluster worker memory/task: {worker_mem_per_task}")

    delta_bytes = 0
    delta_bytes_pyarrow = 0
    delta_manifest_entries = 0
    require_multiple_rounds = False
    # tracks the latest stream position for each partition locator
    high_watermark = HighWatermark()
    limited_input_da_list = []

    if input_deltas_stats is None:
        input_deltas_stats = {}

    input_deltas_stats = {
        int(stream_pos): DeltaStats(delta_stats)
        for stream_pos, delta_stats in input_deltas_stats.items()
    }
    for delta in input_deltas:
        manifest = deltacat_storage.get_delta_manifest(delta, **deltacat_storage_kwargs)
        delta.manifest = manifest
        position = delta.stream_position
        delta_stats = input_deltas_stats.get(delta.stream_position, DeltaStats())
        if delta_stats:
            # TODO (pdames): derive from row count instead of table bytes
            delta_bytes_pyarrow += delta_stats.stats.pyarrow_table_bytes
        else:
            # TODO (pdames): ensure pyarrow object fits in per-task obj store mem
            logger.warning(
                f"Stats are missing for delta stream position {delta.stream_position}, "
                f"materialized delta may not fit in per-task object store memory."
            )
        manifest_entries = delta.manifest.entries
        delta_manifest_entries += len(manifest_entries)
        for entry in manifest_entries:
            delta_bytes += entry.meta.content_length
            if not delta_stats:
                delta_bytes_pyarrow = delta_bytes * PYARROW_INFLATION_MULTIPLIER
        high_watermark.set(
            delta.locator.partition_locator,
            max(position, high_watermark.get(delta.locator.partition_locator)),
        )
        if delta_bytes_pyarrow > worker_obj_store_mem:
            logger.info(
                f"Input deltas limited to "
                f"{len(limited_input_da_list)} by object store mem "
                f"({delta_bytes_pyarrow} > {worker_obj_store_mem})"
            )
            require_multiple_rounds = True
            break
        delta_annotated = DeltaAnnotated.of(delta)
        limited_input_da_list.append(delta_annotated)

    logger.info(f"Input deltas to compact this round: " f"{len(limited_input_da_list)}")
    logger.info(f"Input delta bytes to compact: {delta_bytes}")
    logger.info(f"Input delta files to compact: {delta_manifest_entries}")
    logger.info(f"Latest input delta stream position: {high_watermark}")

    if not limited_input_da_list:
        raise RuntimeError("No input deltas to compact!")

    # TODO (pdames): determine min hash buckets from size of all deltas
    #  (not just deltas for this round)
    min_hash_bucket_count = int(
        max(
            math.ceil(delta_bytes_pyarrow / worker_obj_store_mem_per_task),
            min(worker_cpus, 256),
        )
    )
    logger.info(f"Minimum recommended hash buckets: {min_hash_bucket_count}")

    if hash_bucket_count is None:
        # TODO (pdames): calc default hash buckets from table growth rate... as
        #  this stands, we don't know whether we're provisioning insufficient
        #  hash buckets for the next 5 minutes of deltas or more than enough
        #  for the next 10 years
        hash_bucket_count = min_hash_bucket_count
        logger.info(f"Using default hash bucket count: {hash_bucket_count}")

    if hash_bucket_count < min_hash_bucket_count:
        logger.warning(
            f"Provided hash bucket count ({hash_bucket_count}) "
            f"is less than the min recommended ({min_hash_bucket_count}). "
            f"This compaction job run may run out of memory, or run slowly. To "
            f"resolve this problem either specify a larger number of hash "
            f"buckets when running compaction, omit a custom hash bucket "
            f"count when running compaction, or provision workers with more "
            f"task memory per CPU."
        )

    hash_bucket_chunk_size = user_hash_bucket_chunk_size
    max_hash_bucket_chunk_size = math.ceil(
        worker_obj_store_mem_per_task / PYARROW_INFLATION_MULTIPLIER
    )
    logger.info(f"Max hash bucket chunk size: {max_hash_bucket_chunk_size}")
    if hash_bucket_chunk_size > max_hash_bucket_chunk_size:
        # TODO (pdames): note type of memory to increase (task or object store)
        logger.warning(
            f"Provided hash bucket chunk size "
            f"({user_hash_bucket_chunk_size}) is greater than the max "
            f"recommended ({max_hash_bucket_chunk_size}). This compaction "
            f"job may run out of memory. To resolve this problem either "
            f"specify a smaller hash bucket chunk size when running "
            f"compaction, omit a custom hash bucket chunk size when running "
            f"compaction, or provision workers with more task and object "
            f"store memory per CPU."
        )
    elif not hash_bucket_chunk_size:
        hash_bucket_chunk_size_load_balanced = max(
            math.ceil(max(delta_bytes, delta_bytes_pyarrow) / worker_cpus),
            BYTES_PER_MEBIBYTE,
        )
        hash_bucket_chunk_size = min(
            max_hash_bucket_chunk_size,
            hash_bucket_chunk_size_load_balanced,
        )
        logger.info(f"Default hash bucket chunk size: {hash_bucket_chunk_size}")

    rebatched_da_list = DeltaAnnotated.rebatch(
        limited_input_da_list,
        hash_bucket_chunk_size,
        # TODO (pdames): Test and add value for min_file_counts
    )

    compaction_audit.set_input_size_bytes(delta_bytes)
    compaction_audit.set_input_file_count(delta_manifest_entries)
    compaction_audit.set_total_cluster_memory_bytes(worker_task_mem)
    compaction_audit.set_hash_bucket_count(hash_bucket_count)

    logger.info(f"Hash bucket chunk size: {hash_bucket_chunk_size}")
    logger.info(f"Hash bucket count: {hash_bucket_count}")
    logger.info(f"Input uniform delta count: {len(rebatched_da_list)}")

    return rebatched_da_list, hash_bucket_count, high_watermark, require_multiple_rounds

def fit_input_deltas(
    input_deltas: List[Delta],
    cluster_resources: Dict[str, float],
    compaction_audit: CompactionSessionAuditInfo,
    hash_bucket_count: Optional[int],
    deltacat_storage=unimplemented_deltacat_storage,
    deltacat_storage_kwargs: Optional[Dict[str, Any]] = None,
    **kwargs,
) -> Tuple[List[DeltaAnnotated], int, HighWatermark, bool]:
    """
    This method tries to fit all the input deltas to run into the existing cluster. Contrary to
    'limit_input_deltas', it will not fail if the current cluster cannot run the compaction reliably.
    It is the responsibility of the caller to ensure that they pass enough resources for the job to execute.

    Note: There is a possibility that individual file could be very large, which makes the deltas non uniform.
    In such scenarios, it is advisable to allocate multiple vCPUs to the tasks to ensure parallelism.

    Args:
        input_deltas: The input deltas to be normalized.
        cluster_resources: Total available resources in the cluster.
        hash_bucket_count: The hash bucket count.
        deltacat_storage: An implementation of the DeltaCAT storage interface.

    Returns:
        Tuple of list of annotated deltas, recommended hash bucket count, high watermark,
            and whether multiple rounds are required (which is always False)
    """
    if deltacat_storage_kwargs is None:
        deltacat_storage_kwargs = {}
    worker_cpus = int(cluster_resources["CPU"])
    total_memory = float(cluster_resources["memory"])
    high_watermark = HighWatermark()
    annotated_input_da_list = []
    delta_bytes = 0
    total_files = 0

    if not input_deltas:
        raise AssertionError("No input deltas found!")

    for delta in input_deltas:
        manifest_entries = delta.manifest.entries
        position = delta.stream_position

        for entry in manifest_entries:
            delta_bytes += entry.meta.content_length

        total_files += len(manifest_entries)

        high_watermark.set(
            delta.locator.partition_locator,
            max(position, high_watermark.get(delta.locator.partition_locator)),
        )
        delta_annotated = DeltaAnnotated.of(delta)
        annotated_input_da_list.append(delta_annotated)

    # We assume that the cluster is capable of distributing all tasks
    # correctly. Hence, the correct in-memory size will be in the ratio of
    # in-disk size.
    def estimate_size(manifest_entry: ManifestEntry):
        return (manifest_entry.meta.content_length * 1.0 / delta_bytes) * total_memory

    # Assuming each CPU consumes equal amount of memory
    min_delta_bytes = total_memory / worker_cpus
    rebatched_da_list = DeltaAnnotated.rebatch(
        annotated_deltas=annotated_input_da_list,
        min_delta_bytes=min_delta_bytes,
        estimation_function=estimate_size,
    )

    # Recommended hash buckets based on the experiments performed
    # using S3 input for optimal throughput.
    if hash_bucket_count is None:
        hash_bucket_count = int(
            math.ceil(total_memory / MEMORY_TO_HASH_BUCKET_COUNT_RATIO)
        )

    compaction_audit.set_input_file_count(total_files)
    compaction_audit.set_input_size_bytes(delta_bytes)
    compaction_audit.set_total_cluster_memory_bytes(total_memory)
    compaction_audit.set_hash_bucket_count(hash_bucket_count)

    logger.info(
        f"Input delta bytes: {delta_bytes}, Total files: {total_files}, The worker_cpus: {worker_cpus}, "
        f" total_memory: {total_memory}, and hash_bucket_count: {hash_bucket_count}"
    )
    return rebatched_da_list, hash_bucket_count, high_watermark, False

def _discover_deltas(
    source_partition_locator: PartitionLocator,
    start_position_exclusive: Optional[int],
    end_position_inclusive: int,
    deltacat_storage=unimplemented_deltacat_storage,
    deltacat_storage_kwargs: Optional[Dict[str, Any]] = {},
    list_deltas_kwargs: Optional[Dict[str, Any]] = {},
) -> List[Delta]:
    if deltacat_storage_kwargs is None:
        deltacat_storage_kwargs = {}

    kwargs = {**deltacat_storage_kwargs, **list_deltas_kwargs}

    deltas_list_result = deltacat_storage.list_partition_deltas(
        partition_like=source_partition_locator,
        first_stream_position=start_position_exclusive,
        last_stream_position=end_position_inclusive,
        ascending_order=True,
        include_manifest=True,
        **kwargs,
    )
    deltas = deltas_list_result.all_items()
    if not deltas:
        logger.warn(
            f"Couldn't find any deltas to "
            f"compact in delta stream position range "
            f"('{start_position_exclusive}', "
            f"'{end_position_inclusive}']. Source partition: "
            f"{source_partition_locator}"
        )
        return []

    if start_position_exclusive == deltas[0].stream_position:
        first_delta = deltas.pop(0)
        logger.info(
            f"Removed exclusive start delta w/ expected stream "
            f"position '{start_position_exclusive}' from deltas to "
            f"compact: {first_delta}"
        )
    logger.info(
        f"Count of deltas to compact in delta stream "
        f"position range ('{start_position_exclusive}', "
        f"'{end_position_inclusive}']: {len(deltas)}. Source "
        f"partition: '{source_partition_locator}'"
    )
    return deltas


import matplotlib.pyplot as plt
import pandas as pd

mc_connect_no_entity = pd.read_csv("MC/connect_no_entity/network/rtt/TCP_123-57-84-206.csv")
mc_connect_no_entity = mc_connect_no_entity.loc[:,["sample_rtt"]]

mc_connect_entity = pd.read_csv("MC/connect_many_entity/network/rtt/TCP_123-57-84-206.csv")
mc_connect_entity = mc_connect_entity.loc[:, ["sample_rtt"]]

mc_stand_no_entity = pd.read_csv("MC/stand_still_no_entity/network/rtt/TCP_123-57-84-206.csv")
mc_stand_no_entity = mc_stand_no_entity.loc[:, ["sample_rtt"]]

mc_stand_entity = pd.read_csv("MC/stand_still_many_entity/network/rtt/TCP_123-57-84-206.csv")
mc_stand_entity = mc_stand_entity.loc[:, ["sample_rtt"]]

plt.figure(figsize=(16, 9))  # 设置画布的尺寸
plt.title('Minecraft Computation Burden with RTT', fontsize=40)  # 标题，并设定字号大小
labels = 'Connection', 'General'  # 图例

# vert=False:水平箱线图；showmeans=True：显示均值
plt.boxplot([mc_connect_no_entity["sample_rtt"],
             mc_stand_no_entity["sample_rtt"]],
            labels=labels, showmeans=True,
            positions=[0.6, 2.6],
            patch_artist = True, boxprops = {'color':'orangered','facecolor':'red'})

plt.boxplot([mc_connect_entity["sample_rtt"],
             mc_stand_entity["sample_rtt"]],
            labels=labels, showmeans=True,
            positions=[1.2, 3.2],
            patch_artist = True, boxprops = {'color':'orangered','facecolor':'blue'})

plt.xticks(fontsize=30)
plt.yticks(fontsize=25)

plt.ylabel("RTT(ms)", fontsize=40)
plt.show()


#!/usr/bin/env python3

import argparse
from textwrap import wrap

parser = argparse.ArgumentParser(add_help=True)
parser.add_argument("-c", "--create", type=int, nargs="?",
help="create pattern of x length")
parser.add_argument("-o", "--offset", type=str, nargs="?",
help="find offset of x character series")
parser.add_argument("-py", help="output in prettified python format",
action="store_true")
parser.add_argument("-cpp", help="output in prettified c/c++ format",
action="store_true")

args = parser.parse_args()
size = args.create
offset = args.offset
py = args.py
cpp = args.cpp

primaries = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z']

secondaries = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c',
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9']

tertiaries = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2',
'3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z']

def pattern_create(size):

    string = ''

    for x in primaries:
        for y in secondaries:
            for z in tertiaries:
                addition = (x + y + z)
                string += addition

    return(string[:size])

def pattern_offset(offset):

    offset_len = len(offset)
    new_offset = ''
    new_offset = offset[-2:]
    counter = -2
    while len(new_offset) < offset_len:
        new_offset += offset[counter - 2:counter]
        counter = counter - 2

    offset = bytes.fromhex(new_offset).decode('utf-8')

    string = ''

    for x in primaries:
        for y in secondaries:
            for z in tertiaries:
                addition = (x + y + z)
                string += addition

    if offset in string:
        result = len(string.split(offset)[0])
        print("Exact offset found at position: {}".format(str(result)))

    else:
        print("Characters not found in string.")

def python(result):

    output = 'pattern = (\'' + result + '\')'
    list = wrap(output,78)
    for x in list:
        if x == list[0]:
            x += "\'"
            print(x)
        elif x == list[-1]:
            x = '\'' + x
            print(x)
        else:
            print('\'' + x + '\'')

def seeplusplus(result):

    header = "char pattern[] = "
    output = result + "\";"
    list = wrap(output,78)
    print(header)
    for x in list:
        if x == list[-1]:
            x = '\"' + x
            print(x)
        else:
            print('\"' + x + '\"')

if size:
    result = pattern_create(size)
    if py:
        python(result)
    elif cpp:
        seeplusplus(result)
    else:
        print(result)

elif offset:
    pattern_offset(offset)

else:
    parser.print_help()


# solution
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

# animate plots?
animate=True # True / False

# define tank model
def tank(Level,time,c,valve):
    rho = 1000.0 # water density (kg/m^3)
    A = 1.0      # tank area (m^2)
    # calculate derivative of the Level
    dLevel_dt = (c/(rho*A)) * valve
    return dLevel_dt

# time span for the simulation for 10 sec, every 0.1 sec
ts = np.linspace(0,10,101)

# valve operation
c = 50.0          # valve coefficient (kg/s / %open)
u = np.zeros(101) # u = valve % open

# level initial condition
Level0 = 0

# initial valve position
valve = 0

# for storing the results
z = np.zeros(101)
es = np.zeros(101)
sps = np.zeros(101)

# TO DO: what is the value for ubias?
ubias = 0

# TO DO: decide on a tuning value for Kc
Kc = 50.0

# TO DO: record the desired level (set point)
SP = 10

plt.figure(1,figsize=(12,5))
if animate:
    plt.ion()
    plt.show()
    make_gif = True
    try:
        import imageio  # required to make gif animation
    except:
        print('install imageio with "pip install imageio" to make gif')
        make_gif=False
    if make_gif:
        try:
            import os
            images = []
            os.mkdir('./frames')
        except:
            print('Figure directory failed')
            make_gif=False

# simulate with ODEINT
for i in range(100):
    # calculate the error
    error = SP - Level0

    # TO DO: put P-only controller here
    valve = ubias + Kc * error
    valve = max(0,valve)
    valve = min(100,valve)

    u[i+1] = valve   # store the valve position
    es[i+1] = error  # store the error
    y = odeint(tank,Level0,[0,0.1],args=(c,valve))
    Level0 = y[-1] # take the last point
    z[i+1] = Level0 # store the level for plotting
    sps[i+1] = SP

    if animate:
        # update plot
        plt.clf()
        plt.subplot(3,1,1)
        plt.plot(ts[0:i+1],z[0:i+1],'r-',linewidth=3,label='level PV')
        plt.plot(ts[0:i+1],sps[0:i+1],'k:',linewidth=3,label='level SP')
        plt.ylabel('Tank Level')
        plt.legend(loc='best')
        plt.subplot(3,1,2)
        plt.plot(ts[0:i+1],u[0:i+1],'b--',linewidth=3,label='valve')
        plt.ylabel('Valve')
        plt.legend(loc='best')
        plt.subplot(3,1,3)
        plt.plot(ts[0:i+1],es[0:i+1],'g-',linewidth=3,label='error')
        plt.ylabel('Error = SP-PV')
        plt.xlabel('Time (sec)')
        plt.legend(loc='best')
        filename='./frames/frame_'+str(1000+i)+'.png'
        plt.savefig(filename)
        if make_gif:
            images.append(imageio.imread(filename))
        plt.pause(0.1)

if not animate:
    # plot results
    plt.subplot(3,1,1)
    plt.plot(ts,z,'r-',linewidth=3,label='level PV')
    plt.plot(ts,sps,'k:',linewidth=3,label='level SP')
    plt.ylabel('Tank Level')
    plt.legend(loc='best')
    plt.subplot(3,1,2)
    plt.plot(ts,u,'b--',linewidth=3,label='valve')
    plt.ylabel('Valve')
    plt.legend(loc='best')
    plt.subplot(3,1,3)
    plt.plot(ts,es,'g-',linewidth=3,label='error')
    plt.ylabel('Error = SP-PV')
    plt.xlabel('Time (sec)')
    plt.legend(loc='best')
    plt.show()
else:
    # create animated GIF
    if make_gif:
        imageio.mimsave('animate.gif', images)


__author__ = 'mark'
# StarbaseMini Staribus/Starinet Client for the British Astronomical Association Staribus Protocol
# Copyright (C) 2015  Mark Horn
#
# This file is part of StarbaseMini.
#
# StarbaseMini is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# StarbaseMini is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with StarbaseMini.  If not, see <http://www.gnu.org/licenses/>.

import numpy as np
import logging

class DataStore(object):
    def __init__(self):

        # The raw data as returned from the controller in whatever format that might be, StaribusBlocks only at
        # present.  WARNING - the data store in RawDataBlocks will be in reverse e.g if we have block 0000 - 000A
        # index 0 of the list will be 000A whereas index 9 will be 0000

        self.RawDataBlocks = []

        # A simple condition so we know if RawDataBlocks has data True we have data False we haven't

        self.RawDataBlocksAvailable = False

        # The raw csv data

        self.RawDataCsv = []

        # Data that has been translated from whatever format the controller has provided,
        # StaribusBlocks only at present.  Make sure this ends up np.array

        self.RawData = []

        # Data that has been passed through one of the filters. Make sure this ends up np.array

        self.ProcessedData = []

        # A simple condition so we know if RawDataCsv has data True we have data False we haven't

        self.RawDataCsvAvailable = False

        # Where the data came from either CSV file or Controller

        self.DataSource = None

        # A simple condition so we know if the RawData has been saved, true is saved, false isn't saved.

        self.RawDataSaved = False

        # A simple condition so we know if the ProcessedData has been saved, true is saved, false isn't saved

        self.ProcessedDataSaved = False # False if ProcessedData hasn't been saved, or True if it has

        # Observation Note Changed.

        self.ObservationNotesChanged = False

        # Channel count of current data.

        self.channel_count = 0

        # Controller data block parser this is set from the instrument_datatranslator_class_loader

        self.block_parser = None

    def print_state(self):

        print("Data source : %s" % str(self.DataSource))
        print("\nRaw data blocks length : %s" % str(len(self.RawDataBlocks)))
        print("Raw data blocks : %s" % str(self.RawDataBlocks))
        print("Raw data blocks available : %s" % str(self.RawDataBlocksAvailable))
        print("\nRaw data csv blocks : %s" % str(self.RawDataCsv))
        print("Raw data csv available : %s" % str(self.RawDataCsvAvailable))
        print("Raw data : %s" % str(self.RawData))
        print("Raw data saved : %s" % str(self.RawDataSaved))
        print("\nProcessed data length : %s" % str(len(self.ProcessedData)))
        print("Processed data saved : %s" % str(self.ProcessedDataSaved))
        print("\nChannel count : %s" % self.channel_count)

    def data_state(self):

        logger = logging.getLogger('datastore.DataStore.data_state')

        if len(self.RawData) != 0 and self.RawDataSaved is False and self.DataSource == 'Controller' and \
           len(self.ProcessedData) != 0 and self.ProcessedDataSaved is False:

            logger.debug("We have both Raw and Processed Data but it isn't saved yet.")

            return False, "You have unsaved raw and processed data."

        elif len(self.ProcessedData) != 0 and self.ProcessedDataSaved is False:

            logger.debug("We have Processed Data but it isn't saved yet.")

            return False, "You have unsaved processed data."

        elif len(self.RawData) != 0 and self.RawDataSaved is False and self.DataSource == 'Controller':

            logger.debug("We have RawData but it isn't saved yet.")

            return False, "You have unsaved raw data."

        elif len(self.RawData) != 0 and self.RawDataSaved is False and self.ObservationNotesChanged is True:

            logger.debug("Unsaved Metadata.")

            return False, "You have unsaved metadata."

        elif self.ObservationNotesChanged is True:

            logger.debug("Unsaved Metadata.")

            return False, "You have unsaved metadata."

        else:

            logger.debug("We appear to have a clean data store.")

            return True, "Data store ready to accept data."

    def default_state(self):

        ''' Return True if all settings are reset to default other returns False'''

        if len(self.RawData) != 0 or len(self.ProcessedData) != 0 or self.DataSource is not None or \
           len(self.RawDataBlocks) != 0 or self.RawDataSaved is not False or self.ProcessedDataSaved is not False \
            or self.RawDataBlocksAvailable is not False or self.RawDataCsvAvailable \
                is not False or self.ObservationNotesChanged is not False:

            return False

        else:

            return True

    def clear_processed(self):

        # This only time this should be called is from the filters so we can process processed data more than once.

        del self.ProcessedData  # Delete instead of clear as we could have a numpy array.

        self.ProcessedData = []  # Recreate deleted variable.

    def clear(self):

        ''' Set all attributes back to default state.'''

        self.RawDataBlocks.clear()

        self.RawDataCsv.clear()

        del self.RawData  # Delete instead of clear as we could have a numpy array.

        self.RawData = [] # Recreate deleted variable.

        del self.ProcessedData # Delete instead of clear as we could have a numpy array.

        self.ProcessedData = [] # Recreate deleted variable.

        self.RawDataBlocksAvailable = False

        self.RawDataCsvAvailable = False

        self.DataSource = None

        self.RawDataSaved = False

        self.ProcessedDataSaved = False

        self.ObservationNotesChanged = False

        self.channel_count = 0

    def create_arrays(self):

        logger = logging.getLogger('datastore.DataStore.create_arrays')

        # Here we create the numpy arrays based on the shape of the data presented.

        # CSV Data ..

        if self.DataSource == 'CSV':

            self.RawData = np.array(self.RawDataCsv)

            self.RawDataCsvAvailable = True

            return True

        elif self.DataSource == 'Controller':

            # Parse Blocks

            # clear any data held in translator

            self.block_parser.clear()

            if self.block_parser.block_parser(self):

                logger.debug('We appeared to parse block')

                self.RawData = np.array(self.block_parser.parsed_data_list)

                return True

            else:

                return False


##
# Mouse Locomotion Simulation
#
# Human Brain Project SP10
#
# This project provides the user with a framework based on Blender allowing:
#  - Edition of a 3D model
#  - Edition of a physical controller model (torque-based or muscle-based)
#  - Edition of a brain controller model (oscillator-based or neural network-based)
#  - Simulation of the model
#  - Optimization of the parameters in distributed cloud simulations
#
# File created by: Gabriel Urbain <gabriel.urbain@ugent.be>. February 2016
# Modified by: Dimitri Rodarie
##

import datetime
import logging.config
import os
import sys
import time

import bge

root = os.path.dirname(os.path.dirname(bge.logic.expandPath("//")))
src = root + "/src/"
sys.path.append(src)

from body import *
from config import *

# Get BGE handles
scene = bge.logic.getCurrentScene()

if sys.argv[len(sys.argv) - 1] == "FROM_START.PY":
    # Catch command-line config when started from another script
    argv = sys.argv
    argv = eval(argv[argv.index("-") + 1])
    CONFIG_NAME = argv["config_name"]
    LOG_FILE = argv["logfile"]
    SAVE_NAME = argv["filename"]
else:
    # Default config when started directly from Blender
    CONFIG_NAME = "DogVertDefConfig()"
    LOG_FILE = os.getenv("HOME") + "/.log/qSim.log"
    dirname = root + "/save"
    filename = "sim_" + datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S") + ".qsm"
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    SAVE_NAME = dirname + "/" + filename

# Create python controller
global owner
owner = {"n_iter": 0, "t_init": time.time()}

# Create Logger and configuration
if not os.path.exists(os.path.dirname(LOG_FILE)):
    os.makedirs(os.path.dirname(LOG_FILE))
if not os.path.exists(LOG_FILE):
    f = open(LOG_FILE, 'w')
    f.close()

configuration = eval(CONFIG_NAME)
logging.config.fileConfig(root + "/etc/logging.conf",
                          defaults={'logfilename': LOG_FILE, 'simLevel': "DEBUG"})
logger = logging.getLogger(configuration.logger_name)
configuration.logger = logger
configuration.save_path = SAVE_NAME

owner["config"] = configuration
owner["cheesy"] = Body(scene, configuration)

# Advertise simulation has begun
logger.info("####################################")
logger.info("##   Mouse Locomotion Simulation   #")
logger.info("##   ---------------------------   #")
logger.info("##                                 #")
logger.info("##   Gabriel Urbain - UGent 2016   #")
logger.info("####################################\n")

# Set simulation parameters
bge.logic.setTimeScale(configuration.sim_speed)


0#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 16 16:28:13 2022

@author: astahl3
"""
import re
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
import os
import pathlib
import math

####################################################
######## ENTER DESIRED NUMERICAL VARIABLES #########
####################################################

# 0 = display name
# 1 = pattern match for log file (copy-paste entire process_0.log line up until the desired number)
var_01 =  ['loop time (sec)',               r' - Loop time  \(mpi   time\):.*?([0-9.-]+)']
var_02 =  ['fastest particle (level 0)',    r'   ->Fastest particle \(int\) in Level 0:.*?([0-9.-]+)']
var_03 =  ['fastest particle (level 1)',    r'   ->Fastest particle \(int\) in Level 1:.*?([0-9.-]+)']
var_04 =  ['fastest particle (level 2)',    r'   ->Fastest particle \(int\) in Level 2:.*?([0-9.-]+)']
var_05 =  ['fastest particle (level 3)',    r'   ->Fastest particle \(int\) in Level 3:.*?([0-9.-]+)']

variables  =    [
                var_01,
                #var_02,
                #var_03,
                #var_04,
                #var_05,
                ]
####################################################
############## ENTER PATH TO LOG FILE ##############
####################################################

#log_path = '/home/astahl3/aikef_output/Juno/Juno_n10_r0_dt10/bin/process_0.log'
#log_path = '/home/astahl3/aikef_output/Juno/Juno_n10_r1_dt10_Duling/bin/process_0.log'
#log_path = '/home/astahl3/aikef_output/Juno/Juno_n14_r3_dtplus/bin/process_0.log'
log_path = '/home/astahl3/aikef_output/Juno/Juno_n12_r1_dt10/bin/process_0.log'
#log_path = '/data1/astahl/aikef_output/G2/G2_master_r0/bin/process_0.log'
#log_path = '/home/astahl3/aikef_output/Juno/Juno_n10_r1_dt10_D2/bin/process_0.log'

####################################################
########## EXTRACT BASIC INFO ABOUT RUNS ###########
####################################################
TL_per_run = []
dt_per_run = []
TL_save = []
TL_save.append(0)
run_count = 1
TL_temp = 0
total_lines = 0
cnt = 0

###### Saving basic info about number of runs, timesteps of each run in log file, starting save position
with open(log_path) as log_file:
    pattern_match_dt = re.compile(r' dt = .*?([0-9.-]+)')
    pattern_match_time = re.compile(r' STATISTICS OF TL .*?([0-9.-]+)')
    pattern_match_save = re.compile(r' Continuing at TL .*?([0-9.-]+)')

    for line in log_file:
        total_lines = total_lines + 1 # keep count of total number of lines read
        cnt = cnt + 1 # dummy counter for print statements
        match_dt = pattern_match_dt.match(line) # extract the AIKEF timestep "dt" used in current run
        match_time = pattern_match_time.match(line) # extract timestep n
        match_save = pattern_match_save.match(line) # extract timestep for restart from state

        if match_time:
            TL_temp = match_time.group(1) # save most recent timestep in current run

        # If true, this means new run is starting
        # Save the final timestep "TL" of previous run
        # Save the AIKEF timestep "dt" used in current run
        if match_dt:
            dt_per_run.append(match_dt.group(1)) # extract dt used for current run
            TL_per_run.append(TL_temp) # save the associated (i.e., max timestep of prior run)
        if match_save:
            TL_save.append(match_save.group(1))
            run_count = run_count + 1

        if cnt == 100000:
            print(total_lines, 'lines scanned during run counter')
            cnt = 0

    TL_per_run.append(TL_temp) # store total number of timesteps achieved in each run
    TL_per_run.pop(0) # remove first entry as it doesn't corresond to any run

TL_save = np.float_(TL_save)
TL_save = np.int_(TL_save)
TL_per_run = np.float_(TL_per_run)
TL_per_run = np.int_(TL_per_run)

###### Print summary of each run included in the log file
print('\n')
for k in range(0,run_count):
    TL_per_run[k] = TL_per_run[k] - TL_save[k]
    print('run #', k+1, 'used dt =', dt_per_run[k], 'totalling ', TL_per_run[k], 'timesteps')

print('\n')
print('TOTAL LINES IN LOG FILE: ', total_lines, '\n')

####################################################
########### STRINGS TO MATCH IN LOG FILE ###########
####################################################

    ##############################################################################################
    # Extracts the number (as a float) appearing after a particular line                         #
    # The string of the line must be copied exactly, including leading spaces, from              #
    # start of line until some point before the desired numerical value                          #
    # Use backslash before parenthesis, e.g.:                                                    #
    #    if desired line is:  "   -> vel (int) in level 0 = " (wihthout the quotes)              #
    #    enter this in  re.compile():  "    -> vel \(int\) in level 0 = " (without the quotes)   #
    ##############################################################################################

pattern_start = re.compile(variables[0][1])

###### Save the desired variable or value at each timestep
save_var = []
with open(log_path) as log_file:
    k = 0
    cnt = 0
    for line in log_file:
        match_var = pattern_start.match(line)
        if match_var:
            #print('match.group(1)')
            save_var.append(match_var.group(1))
            #print(match.group(1))
            #result.append(match.group(1))
            k = k + 1
            cnt = cnt + 1
        if k == 500:
            print(cnt, ' timesteps completed during variable extraction')
            k = 0

###### PLOT ALL TIMESTEPS ######
var_plot = np.float_(save_var)
fig, ax = plt.subplots(1,1,sharex=True)
plt.subplots_adjust(hspace = 0.05)
ax.plot(np.linspace(1,cnt,cnt,endpoint=True),var_plot)
ax.set_yticks(np.round(np.linspace(1,np.max(var_plot),10,endpoint=True)))
ax.set_ylabel('maximum normalized v')
ax.set_xlabel('timestep')
ax.set_title('all timesteps')
#plt.savefig('/home/astahl3/plots/Juno_n10_r1_dt10_Duling_vel.png')

###### MAKE PLOTS FOR EACH RUN ######

for k in range(0,run_count):
    fig, ax = plt.subplots(1,1,sharex=True)
    plt.subplots_adjust(hspace = 0.05)
    temp_plot = var_plot[TL_save[k]:TL_save[k]+TL_per_run[k]:1]
    ax.plot(np.linspace(TL_save[k],TL_per_run[k],TL_per_run[k],endpoint=True),temp_plot)
    ax.set_title('run # '+str(k+1))
    ax.set_xlabel('timestep')
    ax.set_ylabel(variables[0][0])

###### PLOT TIMESTEPS LAST RUN ONLY ######
#TL_per_run = np.float_(TL_per_run)
#num_runs = len(TL_per_run)
#TL_last_end = np.sum(TL_per_run)
#TL_last_start = np.sum(TL_per_run[0:num_runs-1])
#steps = round(TL_last_end)-round(TL_last_start)
#var_last_plot = var_plot[round(TL_last_start):steps:1]

#fig, ax = plt.subplots(1,1,sharex=True)
#plt.subplots_adjust(hspace = 0.05)
#ax.plot(np.linspace(1,steps,steps,endpoint=True),var_last_plot)
#ax.set_yticks(np.round(np.linspace(1,np.max(var_plot),10,endpoint=True)))
#ax.set_ylabel(variables[0][0])
#ax.set_xlabel('timestep')


# coding:utf-8
import requests
import json

def download(src, id):
    dir = './img/wzx/' + str(id) + '.jpg'
    try:
        pic = requests.get(src, timeout=10)
        fp = open(dir, 'wb')
        fp.write(pic.content)
        fp.close()
    except requests.exceptions.ConnectionError:
        print('图片无法下载')

query = '王祖贤'

headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
    "Host": "httpbin.org",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36",
    "X-Amzn-Trace-Id": "Root=1-5e5f9a40-ce990b3ad9f29c14644f4e22"
}

for i in range(0, 20, 20):
    url = 'https://www.douban.com/j/search_photo?q=' + query + '&limit=20&start=' + str(i)
    html = requests.get(url,headers=headers)

    print(html)

    response = json.loads(html, encoding='utf-8')
    for image in response['images']:
        print(image['src'])
        download(image['src'], image['id'])


import tensorflow_datasets as tfds
import tensorflow as tf

import time
import numpy as np
import matplotlib.pyplot as plt

def load_ds():
    examples, meta = tfds.load('ted_hrlr_translate/pt_to_en', with_info=True)
    print(examples)
    print(meta)

    train_ds, val_ds = examples['train'], examples['validation']
    print(train_ds)
    print(val_ds)

    for a in train_ds.take(2):
        print(a['pt'])
        print(a['en'])

def train():
    ds = load_ds()

if __name__ == "__main__":
    train()


from django.test import TestCase
from todo.models import TableTodo
from django.db.utils import IntegrityError

class TodoTest(TestCase):

    def setUp(self):
        self.todo_task = TableTodo(
            # task_id = 1
            task_name = 'Bird Watching',
            # task_status = 'not_started',
            # task_description 'Finish youtube 2 hr bird watching video'
            # created_on = ''_
            # finished_on = ''
        )
        self.todo_task.save()

    def test_create_task(self):
        self.assertIsInstance(self.todo_task, TableTodo)
        self.assertEquals(self.todo_task.task_name, 'Bird Watching')

    def test_create_nonunique_task(self):
        with self.assertRaises(IntegrityError):
            new_task = TableTodo(task_name = 'Bird Watching')
            new_task.save()

    def test_read_task(self):
        saved_task = TableTodo.objects.all()
        self.assertEquals(saved_task[0].task_name, 'Bird Watching')

    def test_update_status_completed(self):
        self.todo_task.task_status = 'completed'
        self.todo_task.save()

        saved_task = TableTodo.objects.first()
        self.assertIsNotNone(saved_task.finished_on, f'Date finished not found, actual: {saved_task.finished_on}')

    def test_delete_task(self):
        new_task = TableTodo(task_name='Delete me please')
        new_task.save()

        tasks = TableTodo.objects.all()
        self.assertEqual(tasks.count(), 2, msg='New task for delete test not added')

        new_task.delete()

        tasks = TableTodo.objects.all()
        self.assertEqual(tasks.count(), 1, msg='Task not deleted')


import time

import pytest

from docker import Client
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

def pytest_addoption(parser):
    parser.addoption(
        '--browser',
        action='store',
        default='firefox',
        help='browser used to run the tests')

@pytest.fixture(scope='module')
def browser(request):
    return request.config.getoption("--browser")

@pytest.fixture(scope='module')
def driver(request, browser):
    """ Fixture for selenium Webdriver using docker
    """
    if browser == 'firefox':
        browser_image = 'selenium/standalone-firefox'
        browser_dc = DesiredCapabilities.FIREFOX
    elif browser == 'chrome':
        browser_image = 'selenium/standalone-chrome'
        browser_dc= DesiredCapabilities.CHROME

    cli = Client(version='auto', tls=False)

    port_conf = cli.create_host_config(
        publish_all_ports=True, port_bindings={4444: ('127.0.0.1',)})

    container = cli.create_container(
        image=browser_image, ports=[4444], detach=True, host_config=port_conf)

    cli.start(container.get('Id'))
    request.addfinalizer(lambda: cli.stop(container.get('Id')))

    port_info = cli.port(container['Id'], 4444)[0]

    selenium_host = 'http://{}:{}/wd/hub'.format(
        port_info['HostIp'], port_info['HostPort'])

    timeout = time.time() + 10
    success = 'connect to {}'.format(selenium_host)

    for output in cli.logs(container['Id'], stream=True):
        if success in output or time.time() > timeout:
            break
        else:
            time.sleep(1)

    driver = webdriver.Remote(
        command_executor=selenium_host, desired_capabilities=browser_dc)

    return driver


from django.db import models
from django.contrib.auth.models import User
from product.models import Package

class Ship(models.Model):

    product = models.ForeignKey(Package, default=None, on_delete=models.CASCADE)
    driver = models.ForeignKey(User, default=None, on_delete=models.CASCADE)
    shipping_id = models.AutoField(primary_key=True)
    shipping_datetime = models.DateTimeField(auto_now=True)
    delivered_datetime = models.DateTimeField(null=True)
    delivered = models.BooleanField(default=True)

    def __str__(self):
        return str(self.shipping_id)

class Drop(models.Model):

    drop_id = models.AutoField(primary_key=True)
    drop_code = models.CharField(max_length=8)
    region = models.CharField(max_length=200)
    city = models.CharField(max_length=200)
    drop_datetime = models.DateTimeField(auto_now=True)
    manager = models.CharField(max_length=200)

    def __str__(self):
        return str(self.drop_id)


from scipy.io import loadmat
import numpy as np
import os

from skimage.color import gray2rgb
from skimage.io import imsave
import skimage.morphology as morph
from sys import argv, exit

if len(argv) < 2:
    print('Usage: python parse.py <data_dir> [-v]')
    exit(0)

SHOW_PLOTS = False

if len(argv) == 3:
    if argv[2] != '-v':
        print('Unknown flag', argv[2])
        exit(0)
    SHOW_PLOTS = True

img_index = 0

import matplotlib.pyplot as plt

data_dir = os.path.normpath(argv[1]) + '/'
seg_filename_mask = data_dir + 'manual_seg/manual_seg_32points_pat{}.mat'
img_filename_mask = data_dir + 'mrimages/sol_yxzt_pat{}.mat'

input_dir = data_dir + 'input/'
if not os.path.exists(input_dir):
    os.mkdir(input_dir)
output_dir = data_dir + 'output/'
if not os.path.exists(output_dir):
    os.mkdir(output_dir)
bbox_dir = data_dir + 'bbox/'
if not os.path.exists(bbox_dir):
    os.mkdir(bbox_dir)

batch_filename = 'batch_{}.npy'

def save_batch(batch):
    '''
    print(i.shape)
    print(o.shape)
    print(i[0])
    print(o[0])
    '''
    np.save((input_dir + batch_filename).format(batch_count), batch[0])
    np.save((output_dir + batch_filename).format(batch_count), batch[1])
    np.save((bbox_dir + batch_filename).format(batch_count), batch[2])

def fill_poly(poly_y, poly_x, shape):
    bbox = np.zeros((4), dtype=np.int32)
    bbox[0] = np.min(poly_y)
    bbox[1] = np.min(poly_x)
    bbox[2] = np.max(poly_y)
    bbox[3] = np.max(poly_x)

    mask = np.zeros(shape, dtype = np.bool_)
    mask[poly_y.astype(np.int), poly_x.astype(np.int)] = True
    mask = morph.convex_hull_image(mask).astype(np.int8)
    return mask, bbox

max_bbox = [0, 0]

count = 0
batch_count = 0
i = 1
while True:
    cur_batch = [None, None, None]
    seg_filename = seg_filename_mask.format(i)
    img_filename = img_filename_mask.format(i)
    i += 1

    print(seg_filename)
    if not os.path.isfile(seg_filename):
        break

    slices = loadmat(img_filename)['sol_yxzt']
    segmentations = loadmat(seg_filename)['manual_seg_32points']

    for z in range(slices.shape[2]):
        for t in range(slices.shape[3]):

            slice = slices[:, :, z, t]
            segmentation = segmentations[z, t]

            if segmentation.shape[0] > 1:
                segm = np.zeros((2, 33, 2))
                segm[0] = segmentation[:33, :]
                segm[0, 32] = segm[0, 0]

                segm[1] = segmentation[32:, :]
                segm[1, 0] = segm[1, -1]

                mask1, _ = fill_poly(segm[0, :, 1], segm[0, :, 0], slice.shape[:2])
                mask2, bbox = fill_poly(segm[1, :, 1], segm[1, :, 0], slice.shape[:2])
                mask = mask1 + mask2
                count += 1

                max_bbox = [max(max_bbox[0], bbox[2] - bbox[0]),
                            max(max_bbox[1], bbox[3] - bbox[1])]

                if cur_batch[0] is None:
                    cur_batch[0] = np.array([slice], dtype=np.float16)
                    cur_batch[1] = np.array([mask], dtype=np.int8)
                    cur_batch[2] = np.array([bbox], dtype=np.int32)
                else:
                    cur_batch[0] = np.append(cur_batch[0], [slice], axis=0)
                    cur_batch[1] = np.append(cur_batch[1], [mask], axis=0)
                    cur_batch[2] = np.append(cur_batch[2], [bbox], axis=0)
                '''
                if count % batch_size == 0:
                    save_batch(cur_batch[1], cur_batch[0])
                    cur_batch = [None, None]
                    batch_count += 1
                '''

                plt.imsave('{}images/img_{}.png'.format(data_dir, img_index), slice, cmap='gray')

                np.save('{}mask1/img_{}'.format(data_dir, img_index), mask1)
                np.save('{}mask2/img_{}'.format(data_dir, img_index), mask2)
                #plt.imsave('{}mask1/img_{}.png'.format(data_dir, img_index), mask1, cmap='gray')
                #plt.imsave('{}mask2/img_{}.png'.format(data_dir, img_index), mask2, cmap='gray')
                img_index += 1
                if SHOW_PLOTS:
                    plt.subplot(121)
                    plt.plot(segm[0, :, 0], segm[0, :, 1])
                    plt.plot(segm[1, :, 0], segm[1, :, 1])
                    plt.imshow(slice, cmap='gray')

                    plt.subplot(122)
                    plt.imshow(mask, cmap='gray', interpolation='None')

                    mngr = plt.get_current_fig_manager()
                    # to put it into the upper left corner for example:
                    mngr.window.setGeometry(100,100, 1000, 800)

                    plt.show()

    save_batch(cur_batch)
    batch_count += 1

    print('count:', count)
print('max_bbox:', max_bbox)
'''
if cur_batch[0] is not None:
    np.save(batch_filename.format(batch_count), cur_batch)
    cur_batch = None
'''


from dash import dcc, html
import dash_bootstrap_components as dbc

from app import app

cameron_yap = dbc.Card([
    dbc.CardImg(src='assets/cameron_yap_cropped.jpg'),
    dbc.CardBody([
        html.H4(
            html.A('Cameron Yap', href='https://www.linkedin.com/in/cameron-yap-9b8234119'),
            style={'text-align': 'center'})
    ])
])

emily_christiansen = dbc.Card([
    dbc.CardImg(src='assets/emily_christiansen.jpeg'),
    dbc.CardBody([
        html.H4(
            html.A('Emily Christiansen', href='https://www.linkedin.com/in/emily-christiansen-65a644117/'),
            style={'text-align': 'center'})
    ])
])

madeleine_thompson = dbc.Card([
    dbc.CardImg(src='assets/madeleine_thompson.png'),
    dbc.CardBody([
        html.H4(html.A('Madeleine Thompson', href='https://www.linkedin.com/in/madeleine-jane-thompson/'),
        style={'text-align': 'center'})
    ])
])

stefan_lin = dbc.Card([
    dbc.CardImg(src='assets/stefan_lin_cropped.jpeg'),
    dbc.CardBody([
        html.H4(
            html.A('Stefan Lin', href='https://www.linkedin.com/in/stefanemoses'),
            style={'text-align': 'center'})
    ])
])

layout = html.Div([
    # Header container
    dbc.Container([
        dbc.Row([
            dbc.Col(html.H1('Predicting House Prices'))
        ]),
        dbc.Row([
            dbc.Col(html.Div(
                'Cameron Yap, Emily Christiansen, Madeleine Thompson, and Stefan Lin'))
        ]),
        html.Br()
    ]),

    # Content container
    dbc.Container([
        dbc.Row([
            dbc.Col(html.H3('About our project'))
        ]),
        dbc.Row([
            dbc.Col(html.Div("""
            We have developed predictive models to accurately forecast rent prices and analyze local rent level trends in relation to environmental quality metrics in Los Angeles County. This tool will serve as a predictive reference and could prove to be advantageous for a company to gain an informational edge over all other housing agencies. Our tool could also encourage state or local governments to pay attention to environmental trends in order to support the housing market in Los Angeles County.
            """))
        ]),

        html.Br(),
        dbc.Row([
            dbc.Col(html.H3("What's in this tool?"))
        ]),
        dbc.Row([
            dbc.Col(html.Div("""
            For all your rental needs, we have incorporated socio-environmental factors into our prediction model. What are your main concerns when renting a home? Do you worry about the surrounding socio-environmental issues such as safety, education, oil wells, air quality, and water pollution that can impact you and your partner, children and beloved furry friends? Our tool takes in your specific needs and concerns and helps you find the best locations and predicted price for your next rented home!
            """))
        ]),

        html.Br(),
        dbc.Row([
            dbc.Col(html.H3('About us'))
        ]),
        dbc.Row([
            dbc.Col(html.Div("""
            Index Zero is a data analytics consulting team at the University of Southern California. Our members are Cameron Yap, Emily Christiansen, Madeleine Thompson, and Stefan Lin. We are experts in technical data analytics and want to help you, residential renters, to find your next home in Los Angeles County.
            """))
        ]),
        html.Br(),
        dbc.Row([
            dbc.Col(cameron_yap),
            dbc.Col(emily_christiansen),
            dbc.Col(madeleine_thompson),
            dbc.Col(stefan_lin),
        ])
    ])
])


from copy import deepcopy
import import_ipynb
import numpy as np
import tsaug
import h5py
import EMGImg
import math
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt

colors=list(mcolors.TABLEAU_COLORS.keys()) #颜色变化
# 按动作次数将手势分割（数据，手势类别，通道数）

def segment(data, dataclass, channel):
    data = np.array(data)
    Datalist = []
    for j in range(1, dataclass + 1):
        num = 0
        while (num < 6):
            for i in range(data.shape[0]):
                if data[i, channel] == j:  # alldata最后一维存储标签
                    start = i
                    break
            for k in range(start, data.shape[0]):
                if data[k, channel] == 0:
                    end = k
                    break
                if k == len(data) - 1:
                    end = data.shape[0]
                    break

            print(start, end)
            num = num + 1
            emg = (deepcopy(data[start:end, 0:channel]))
            label = (deepcopy(data[start:end, channel]))
            myemg = EMGImg.EMG(emg, label)
            Datalist.append(myemg)  # 用append将每次动作分割为单独的元素,保存了emg数据和标签
            for m in range(start, end):
                data[m, channel] = 100
    return Datalist  # 返回数据为list类型，保存不同长度的数据

'''该函数在输入二维数组时会将各个通道随机打乱，破坏了不同电极采样的同步性，故设定随机种子，单个通道依次打乱'''
def OneenhanceEMG(data):
    newdata =tsaug.TimeWarp(n_speed_change=1, max_speed_ratio=2, seed=123).augment(data)
#     enhancedata =np.hstack((data, newdata))
    return data, newdata

for j in range(1, 2):
    h5 = h5py.File('F:/DB2/filter/DB2_s' + str(j) + 'filter.h5', 'r')
    alldata = h5['alldata']
    seglist = segment(alldata, 2, 12)
    timedata=seglist[0].data
    data=[]
    newdata=[]
    for k in range(12):
            datasample, newdatasample =OneenhanceEMG(timedata[:,k])
            data.append(datasample)
            newdata.append(newdatasample)
    data=np.array(data)
    newdata=np.array(data)
    plt.figure(figsize=(20,8))
    for i in range(12):
        plt.subplot(12,1,i+1)
        plt.plot(data[:,i],color=mcolors.TABLEAU_COLORS[colors[int(math.fabs(i-2))]])
    #         plt.xticks([])  #去掉x轴
        plt.axis('off')
    plt.show()

# for j in range(1, 2):
#     h5 = h5py.File('F:/DB2/filter/DB2_s' + str(j) + 'filter.h5', 'r')
#     alldata = h5['alldata']
#     seglist = segment(alldata,2, 12)
#     timedata=seglist[0].data
#
#     data, newdata =enhanceEMG((timedata))
#
#
#     plt.figure(figsize=(20,8))
#     for i in range(12):
#         plt.subplot(12,1,i+1)
#         plt.plot(data[:,i],color=mcolors.TABLEAU_COLORS[colors[int(math.fabs(i-2))]])
#         #         plt.xticks([])  #去掉x轴
#         plt.axis('off')
#     plt.show()


import pandas as pd
import numpy as np

from collections import defaultdict
from sklearn import model_selection
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.ensemble import ExtraTreesClassifier, RandomForestRegressor, AdaBoostRegressor
from sklearn.model_selection import KFold

def Venue_Changes(teamA,teamB,venue):   #venue is changed to 1 for teamA, -1 for teamB and 0 for no team.
	d = defaultdict(list)
	country=''
	with open ('stadium/stadiums', 'r') as f:
	    lines = f.read().splitlines()
	    length=len(lines)
	    i=0
	    while i<length:
	        line = lines[i]
	        if line == '$':
	        	country=lines[i+1]
	        	i+=1
	        else:
	        	d[country].append(line)
	        	i+=1

	if venue in (d[teamA]):
		return 1
	if venue in (d[teamB]):
		return -1
	return 0

def Toss_Changes(teamA,teamB,Toss):
	if Toss == teamA:
		return 1
	return 0

def Toss_Decision_Changes(Toss,Toss_Decision):
	if ( ((Toss == 1) & (Toss_Decision == 'bat')) | ((Toss == 0) & (Toss_Decision == 'field')) ):
		return 1
	return 0

def Win_Prob_Of_TeamA(df,teamA,teamB):

	playOffAandB=df[((df['TeamA']==teamA)&(df['TeamB']==teamB) | (df['TeamA']==teamB)&(df['TeamB']==teamA))]
	playOffAandB = playOffAandB.sort_values(by = 'Date', ascending=[0])
	# playOffAandB = playOffAandB.head(10)

	Awin=playOffAandB[(playOffAandB['Winner']==1)]
	a=len(Awin)
	p=len(playOffAandB)

	if p==0:
		return 0
	return a/p

def Win_prob_on_venue(df1,venue,Toss_Decision):

	prevMatches = df1[(df1['Venue']==venue)]
	prevMatches = prevMatches.sort_values(by = 'Date', ascending=[0])
	# prevMatches = prevMatches.head(10)

	#print prevMatches

	if Toss_Decision == 1:
		Awin = prevMatches[(prevMatches['Toss_Decision'] == prevMatches['Winner'])]
	else:
		Awin = prevMatches[(prevMatches['Toss_Decision'] != prevMatches['Winner'])]

	#print Awin
	a=len(Awin)
	p=len(prevMatches)

	if p==0:
		return 0
	return a/p

def strength_based_on_batBowl_avg(df,TeamA,TeamB):
	playOffAandB=df[((df['TeamA']==TeamA)&(df['TeamB']==TeamB) | (df['TeamA']==TeamB)&(df['TeamB']==TeamA))]
	playOffAandB = playOffAandB.sort_values(by = 'Date', ascending=[0])
	playOffAandB = playOffAandB["Strength"].iloc[0]

	return playOffAandB

def pastPerformance(df1,teamA,teamB,bat_avg):
	prevA = df1[((df1['TeamA']==teamA) | (df1['TeamB']==teamA))]
	prevA = prevA.sort_values(by = 'Date', ascending=[0])
	prevA = prevA.head(10)

	form_A = 0
	cntA = 0
	for index, row in prevA.iterrows():
		name = str(row['MatchID'])+'.csv'#"657643" #657645
		df=pd.read_csv("Dataset/PlayerInfo/"+name)
		df['Bat_Avg'] = df['Bat_Avg'].replace('-', bat_avg)
		total_A = 0
		cntA = cntA + 1
		team_list=df[(df['Country']==teamA)]
		for index1, row1 in team_list.iterrows():
			total_A=total_A+float(row1['Bat_Avg'])
		form_A = form_A + total_A/11

	prevB = df1[((df1['TeamA']==teamB) | (df1['TeamB']==teamB))]
	prevB = prevB.sort_values(by = 'Date', ascending=[0])
	prevB = prevB.head(10)

	form_B = 0
	cntB = 0
	for index, row in prevB.iterrows():
		name = str(row['MatchID'])+'.csv'#"657643" #657645
		df=pd.read_csv("Dataset/PlayerInfo/"+name)
		df['Bat_Avg'] = df['Bat_Avg'].replace('-', bat_avg)
		total_B = 0
		cntB = cntB + 1
		team_list=df[(df['Country']==teamB)]
		for index1, row1 in team_list.iterrows():
			total_B=total_B+float(row1['Bat_Avg'])
		form_B = form_B + total_B/11

	# print form_A, form_B, df1.loc[i, 'MatchID']
	if cntA == 0:
		cntA = 1
		formA = bat_avg
	if cntB == 0:
		cntB = 1
		formB = bat_avg

	return form_A/cntA - form_B/cntB

def testPredicit(df1,testData,TeamA,TeamB):
	df1 = df1[((df1['TeamA']==TeamA)&(df1['TeamB']==TeamB) | (df1['TeamA']==TeamB)&(df1['TeamB']==TeamA))]
	predictors = ['Toss', 'Toss_Decision','Venue', 'HTH', 'WinningPerDes','Strength','latest_form']
	alg = LogisticRegression(random_state=1)
	df = df1[['Toss', 'Toss_Decision', 'Venue', 'HTH', 'WinningPerDes', 'Strength','latest_form','Winner']]
	train_predictors = (df[predictors])
	train_target = df["Winner"]
	alg.fit(train_predictors, train_target)
	test_predictions = alg.predict(testData)
	return test_predictions[0]

# main Function

def startPrediction(teamA_input,teamB_input,venue_input,toss_input,tossDecision_input):

	df = pd.read_csv('OutputOfAll.csv')

	if teamB_input < teamA_input:
		teamB_input,teamA_input = teamA_input,teamB_input

	TeamA = teamA_input
	TeamB = teamB_input
	Toss = toss_input
	Toss_Decision = tossDecision_input
	Venue = venue_input

	Venue = Venue_Changes(TeamA,TeamB,Venue)
	Toss = Toss_Changes(TeamA,TeamB,Toss)
	Toss_Decision = Toss_Decision_Changes(Toss,Toss_Decision)

	HTH = Win_Prob_Of_TeamA(df,TeamA,TeamB)

	WinningPerDes = Win_prob_on_venue(df,Venue,Toss_Decision)

	bat_avg = 22.6046511628
	bowl_avg = 29.7670682731

	Strength = strength_based_on_batBowl_avg(df,TeamA,TeamB)

	latest_form = pastPerformance(df,TeamA,TeamB,bat_avg)

	print ("teamA : " +  TeamA)
	print (" ")
	print ("teamB :"  + TeamB)
	print(" ")
	print ( "winning probability of TeamA based on previous matches : " + str(HTH))
	print (" ")
	print ( "winning probability of Team batting first : " + str(WinningPerDes))
	print(" ")
	print ("Team A Performance - Team B Performance : " + str(latest_form))
	print(" ")
	print ("Performance is calculated based on team's batting average")
	print(" ")
	dict = {'Toss':Toss, 'Toss_Decision':Toss_Decision, 'Venue':Venue, 'HTH':HTH, 'WinningPerDes':WinningPerDes, 'Strength':Strength , 'latest_form':latest_form}

	testData = pd.DataFrame(dict,index = ["result"])

	if testPredicit(df,testData,TeamA,TeamB) == 1:
		return teamA_input
	return teamB_input


from typing import List

class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        ans, res = [], []

        def backtracking(nums, k):
            nonlocal ans, res
            if k == 0:
                ans.append(res)
                print(res, ans)
                return
            for i in range(len(nums) - k + 1):
                data = nums[i]
                res.append(data)
                nums.pop(i)
                backtracking(nums[i:], k - 1)
                # 回溯
                res.pop()
                nums.insert(i, data)

        nums = [x for x in range(1, n + 1)]
        backtracking(nums, k)
        return ans

if __name__ == "__main__":
    s = Solution()
    ans = s.combine(4, 2)
    print(ans)


"""
@author: Rosita Hormann

Python 3.7.4
"""
# Imports:
import requests
from random import randrange
import time
import numpy as np

host="localhost"
port="8080" #8080 and 8081 are KairosDB nodes, 80 is Nginx node.
api_base = "http://"+ host +":" + port +"/api/v1"

#current_date = np.round(time.time()*1000,0) # time.time() is in seconds
current_date = 1577836800000 # 01-jan-2020 00:00:00 (timestamp in milliseconds)
metric_name = "example_2"
tags = {
        "host": "host_example"
        }

list_datapoints = []

n_datapoints = 10000 #80000*60*15 # number of datapoints to insert for each time series

for i in range(n_datapoints):
    value = randrange(1000)
    data_point = [current_date, value]
    list_datapoints.append(data_point)

    current_date += 1000

data_to_post = {
                  "name": metric_name,
                  "datapoints": list_datapoints,
                  "tags": tags
                }

start = time.time()
req = requests.post(api_base+"/datapoints", json = data_to_post)
end = time.time()

print("req.status_code", req.status_code)
print("response: ", req)
print("------------------------------------------------------")
print("Insertion time:", end - start, "seconds")


# coding:utf-8
import os
import codecs
from lxml import etree
import re
from connectMysql import Mymysql
import datetime
import uuid
from util import get_pic_md5

def main_page_jiexi(product_id, content, update_time,item_dict,thread_name):
    try:
        x = Mymysql()
        x._GetConnect()

        # 属性插入:
        tree = etree.HTML(content)
        attributes = tree.xpath(".//ul[@class='attributes-list']/li/text()")
        attributes = [item.split(":") for item in attributes]
        for item in attributes:
            if item:
                detail_id = uuid.uuid1()
                index_name = item[0].strip()
                index_value = item[1].strip().replace("'", "")
                sql = """
                          insert into product_detail_information(detail_id,product_id,index_name,index_value,update_time)
                          values('%s','%s','%s','%s','%s')
                      """ % (detail_id, product_id, index_name, index_value, update_time)
                try:
                    x.ExecNonQuery(sql)
                except Exception as ex:
                    print("attributes jiexi error", ex)
                    print(sql)
                    print("=============================")
                    return False
        # 左上角图片插入
        imgs_left = tree.xpath(".//ul[@id='J_UlThumb']/li//img")
        count = 1
        for item in imgs_left:
            src = item.get("src")
            if not src:
                src = item.get("data-src")
            src = "http:" + src
            src = src.replace("50x50", "400x400")
            if not src.endswith("jpg"):
                continue
            from_who = position = 1
            sequence = count
            pic_md5 = get_pic_md5(src, product_id)
            sql = """
                              insert into image(product_id,img_src,position,sequence,update_time,from_who,pic_md5)
                                          values('%s','%s','%s','%s','%s','%s','%s')
                              """ % (product_id, src, position, sequence, update_time, from_who,pic_md5)
            try:
                x.ExecNonQuery(sql)
                count += 1
            except Exception as ex:
                print("left_img jiexi error",ex)
                print(sql)
                print("=============================")
                continue

        # size 插入
        code = tree.xpath(".//ul[@class='J_TSaleProp tb-clearfix']//span/text()")
        for item in code:
            Size_ID = uuid.uuid1()
            Size = item.replace("'", "")
            Size = item.replace("\\", "")
            sql = """
                             insert into  size(product_id,size_id, size,update_time) values('%s','%s','%s','%s')
                  """ % (product_id, Size_ID, Size, update_time)
            try:
                x.ExecNonQuery(sql)
            except Exception as ex:
                    print('size jiexi error',ex)
                    print(sql)
                    print("=============================")
                    return False

        # color 插入
        color = tree.xpath(".//ul[@class='J_TSaleProp tb-img tb-clearfix']//a")
        count = 1
        for item in color:
            src = item.get("style")
            img_id = ""
            if src:  # 有图片
                src = "http:" + re.findall("\((.*?)\)", src)[0]
                src = src.replace("30x30", "400x400")
                if not src.endswith("jpg"):
                    continue
                from_who = 1
                position = 3
                sequence = count
                pic_md5 = get_pic_md5(src, product_id)
                sql = """
                          insert into image(pic_md5,product_id,img_src,position,sequence,update_time,from_who)
                                      values('%s','%s','%s','%s','%s','%s','%s')
                          """ % (pic_md5, product_id, src, position, sequence, update_time, from_who)
                try:
                    x.ExecNonQuery(sql)
                    count += 1
                except Exception as ex:
                    print("color imagejiexi error",ex)
                    print(sql)
                    print("=============================")
                    continue

            color_id = uuid.uuid1()
            color = item.xpath(".//span/text()")[0]
            sql = """
                          insert into color(product_id,img_id,color_id,color,update_time)
                                      values('%s','%s','%s','%s','%s')
                          """ % (product_id, img_id, color_id, color, update_time)
            try:
                x.ExecNonQuery(sql)
            except Exception as ex:
                print("colortext jiexi error",ex)
                print(sql)
                print("=============")
                continue
        # 3 产品名称
        product_name = tree.xpath(".//h3[@class='tb-main-title']")
        if not product_name:
            product_name = ""
        else:
            product_name = product_name[0].get("data-title")
        product_name = product_name.replace("'", "")
        item_dict['product_name'] = product_name

        # 产品简述
        product_profile = tree.xpath(".//*[@id='J_Title']/p/text()")
        product_profile = product_profile[0] if product_profile else ""
        product_profile = product_profile.replace("'", "")
        item_dict['product_profile'] = product_profile

        return "ok"

    except Exception as ex:
        print(product_id, "main_page_jiexi", "error:", ex)
        file_save = os.path.join("jiexi", product_id + "_" + "1" + ".html")
        f = codecs.open(file_save, "a+", encoding="utf-8")
        f.write(content)
        f.close()
        return False


import os

import logging

_logger = logging.getLogger(__name__)

def _get_alembic_config(db_url):
    from alembic.config import Config
    current_dir = os.path.dirname(os.path.abspath(__file__))
    package_dir = os.path.normpath(os.path.join(current_dir, os.pardir, os.pardir))
    directory = os.path.join(package_dir, 'alembic')
    config = Config(os.path.join(package_dir, 'alembic.ini'))
    config.set_main_option('script_location', directory)
    config.set_main_option('sqlalchemy.url', db_url)
    return config

def _upgrade_db(url):
    """
    Upgrade the schema of an MLflow tracking database to the latest supported version.
    version. Note that schema migrations can be slow and are not guaranteed to be transactional -
    we recommend taking a backup of your database before running migrations.

    :param url Database URL, like sqlite:///<absolute-path-to-local-db-file>. See
    https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls for a full list of valid
    database URLs.
    """
    # alembic adds significant import time, so we import it lazily
    from alembic import command

    _logger.info("Updating database tables at %s", url)
    config = _get_alembic_config(url)
    command.upgrade(config, 'heads')


import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from _ast import IsNot
from selenium.webdriver.remote import webelement

def decision():
    import tkinter as tk
    root= tk.Tk()
    canvas1 = tk.Canvas(root, width = 400, height = 300)
    canvas1.pack()
    entry1 = tk.Entry (root)
    canvas1.create_window(200, 140, window=entry1)
    def getSquareRoot ():
        global x1
        x1 = entry1.get()
        label1 = tk.Label(root, text= x1)
        canvas1.create_window(200, 230, window=label1)
    button1 = tk.Button(text='Get the Square Root', command=getSquareRoot)
    canvas1.create_window(200, 180, window=button1)
    root.mainloop()

driver = webdriver.Chrome('C://Users//vthapan//Downloads//chromedriver_win32//chromedriver')  # Optional argument, if not specified will search path.
time.sleep(3)
driver.maximize_window()
driver.get('https://www.google.co.in/');
time.sleep(5)
decision()
print(x1)
if x1 == 1:
    driver.close()


from dotenv import load_dotenv, find_dotenv
from langchain import OpenAI, Wikipedia
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.agents.react.base import DocstoreExplorer

_ = load_dotenv(find_dotenv())  # read local .env file

# ---------------------------
# First Agent
# ---------------------------
docstore = DocstoreExplorer(Wikipedia())
tools = [
    Tool(
        name="Search",
        func=docstore.search,
        description="useful for when you need to ask with search",
    ),
    Tool(
        name="Lookup",
        func=docstore.lookup,
        description="useful for when you need to ask with lookup",
    ),
]

llm = OpenAI(temperature=0, model_name="text-davinci-002")

# ZERO_SHOT_REACT_DESCRIPTION
# STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION
# CONVERSATIONAL_REACT_DESCRIPTION
# CHAT_CONVERSATIONAL_REACT_DESCRIPTION
# REACT_DOCSTORE <---------------------
# SELF_ASK_WITH_SEARCH
react = initialize_agent(tools, llm, agent=AgentType.REACT_DOCSTORE, verbose=True)

question = "Author David Chanoff has collaborated with a U.S. Navy admiral who served as the ambassador to the United Kingdom under which President?"
result = react.run(question)
print(result)