"""Tests configurations are set properly"""
import os
import unittest
from pathlib import Path

from numcodecs import Blosc

from aind_data_transfer.config_loader.imaging_configuration_loader import (
    ImagingJobConfigurationLoader,
)

TEST_DIR = Path(os.path.dirname(os.path.realpath(__file__)))
CONFIGS_DIR = TEST_DIR / "resources" / "test_configs"

class TestImagingJobConfigs(unittest.TestCase):
    code_repo_url = "https://github.com/AllenNeuralDynamics/aind-data-transfer"

    """Basic config loads test"""

    def test_conf_loads(self):
        """Basic config loads test"""

        raw_data_dir = (
            "tests/resources/imaging/exaSPIM_125L_2022-08-05_17-25-36"
        )

        expected_configs = {
            "jobs": {
                "upload_aux_files": False,
                "transcode": True,
            },
            "endpoints": {
                "raw_data_dir": raw_data_dir,
                "dest_data_dir": (
                    "s3://aind-transfer-test/"
                    "exaSPIM_125L_2022-08-05_17-25-36"
                ),
                "code_repo_location": self.code_repo_url,
            },
            "data": {"name": "imaging"},
            "transcode_job": {
                "compressor": {
                    "compressor_name": "blosc",
                    "kwargs": {
                        "cname": "zstd",
                        "clevel": 1,
                        "shuffle": Blosc.SHUFFLE,
                    },
                },
                "chunk_size": 64,
                "resume": False,
                "n_levels": 8,
                "submit_args": {
                    "conda_activate": (
                        "/allen/programs/aind/workgroups/msma/"
                        "cameron.arshadi/miniconda3/bin/"
                        "activate"
                    ),
                    "conda_env": "aind-data-transfer",
                    "cpus_per_task": 1,
                    "mail_user": "cameron.arshadi@alleninstitute.org",
                    "mem_per_cpu": 3000,
                    "nodes": 8,
                    "ntasks_per_node": 8,
                    "run_parent_dir": (
                        "/home/cameron.arshadi/"
                        "exaSPIM-transcode-jobs/"
                        "exaSPIM_125L_2022-08-05_17-25-36"
                    ),
                    "tmp_space": "8GB",
                    "walltime": "72:00:00",
                    "queue": "aind",
                },
            },
        }
        conf_file_path = (
            CONFIGS_DIR / "imaging" / "transcode_job_test_config.yml"
        )

        args = ["-c", str(conf_file_path)]

        loaded_configs = ImagingJobConfigurationLoader().load_configs(args)
        self.assertEqual(loaded_configs, expected_configs)

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


#!/usr/bin/env python3
import sys

sys.path.append("src")

import logging
import os

from telegram import Update
from telegram.ext import CallbackContext

from constants import USERSTATE
from bot import Bot
from user import UserManager
from utils import utils
from utils.log import Log

LOG_FILE = os.path.join("logs", f"example:get_user_input.log")

CONFIG = utils.load_yaml_file(os.path.join("config.yaml"))
assert CONFIG, "Failed to load config.yaml. Fatal error, please remedy."\
    "\n\nLikely an invalid format."

LIVE_MODE = CONFIG["RUNTIME"]["LIVE_MODE"]
BOT_TOKEN = CONFIG["BOT_TOKENS"]["LIVE"] if LIVE_MODE else CONFIG["BOT_TOKENS"]["TEST"]

def main():
    logger = Log(
        name=__name__,
        stream_handle=sys.stdout,
        file_handle=LOG_FILE,
        log_level=logging.DEBUG
    )

    user_manager = UserManager()
    user_manager.init(logger=logger)

    bot = Bot()
    bot.init(token=BOT_TOKEN,
             logger=logger,
             config=CONFIG)

    STAGE_FAV_FRUIT = "input_favorite_fruit"
    STAGE_END = "end"

    # ------------------------ Stage: input:favorite_fruit ----------------------- #
    def print_user_choice(choice: str, update: Update, context: CallbackContext) -> USERSTATE:
        print(f"User has selected: {choice}")

        bot.edit_or_reply_message(
            update, context,
            f"You have selected: {choice}",
            reply_message=True
        )

        return bot.proceed_next_stage(
            current_stage_id=STAGE_FAV_FRUIT,
            next_stage_id=STAGE_FAV_FRUIT,
            update=update, context=context
        )

    bot.get_user_input(
        stage_id=STAGE_FAV_FRUIT,
        input_text="Enter your favorite fruit!\n\n"
        "Enter /cancel to exit.",
        input_handler=print_user_choice,
        exitable=True
    )
    bot.set_first_stage(stage_id=STAGE_FAV_FRUIT)
    # ---------------------------------------------------------------------------- #

    # -------------------------------- Stage: end -------------------------------- #
    bot.make_end_stage(
        stage_id=STAGE_END,
        goodbye_message="You have exited the conversation. \n\nUse /start to begin a new one."
    )
    # ---------------------------------------------------------------------------- #

    # Start Bot
    logger.info(False, "")
    logger.info(False, "Initializing...")
    logger.info(False, "")

    bot.start(live_mode=LIVE_MODE)

if __name__ == "__main__":
    main()


import sys
from collections import deque
T=int(sys.stdin.readline()) # 테스트 케이스
dx=[-2,-1,1,2,2,1,-1,-2]
dy=[-1,-2,-2,-1,1,2,2,1]
def sol(start,target):
    queue=deque()
    v=[[False]*l for _ in range(l)]
    queue.append(start)
    tx,ty=target
    while queue:
        x,y=queue.popleft()
        if tx==x and ty==y:
            print(board[tx][ty])
            break
        v[x][y]=True
        for i in range(8):
            nx=x+dx[i]
            ny=y+dy[i]
            if 0<=nx<l and 0<=ny<l and not v[nx][ny]:
                board[nx][ny]=board[x][y]+1
                v[nx][ny]=True
                queue.append((nx,ny))

for _ in range(T):
    l=int(sys.stdin.readline()) # l: 체스판 한변의 길이 (l x l)
    board=[[0]*l for _ in range(l)]
    a1,b1=map(int,sys.stdin.readline().split()) # (a1,b1) 현재 나이트
    a2,b2=map(int,sys.stdin.readline().split()) # (a2,b2) 나이트가 이동하려고 하는 칸
    # 나이트가 최소 몇 번만에 이동할 수 있는지 출력
    sol((a1,b1),(a2,b2))


"""
Good morning! Here's your coding interview problem for today.

This problem was asked by Uber.

Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.

For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].

Follow-up: what if you can't use division?
"""
from functools import reduce

def func(data):
	m = reduce(lambda x, y: x*y, data)

	return [m//i for i in data]

data = [1, 2, 3, 4, 5]

print(func(data))

# Note: this might not be the optimal solution.


import os
import random
import string
from flask import Flask, render_template
from flask_socketio import SocketIO, emit, join_room, leave_room
from aiortc import RTCPeerConnection, RTCSessionDescription
from aiortc.contrib.media import MediaStreamTrack
from av import VideoFrame

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
socketio = SocketIO(app, async_mode='gevent')

# Dictionary to store active rooms and their participants
active_rooms = {}

# Generate a random room ID
def generate_room_id():
    return ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))

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

@socketio.on('join_room')
def join_video_room(data):
    username = data['username']
    room_id = data.get('room_id', generate_room_id())
    join_room(room_id)

    if room_id not in active_rooms:
        active_rooms[room_id] = []

    active_rooms[room_id].append(username)

    emit('user_joined', {'username': username}, room=room_id)
    emit('active_users', {'users': active_rooms[room_id]}, room=room_id, include_self=False)

@socketio.on('leave_room')
def leave_video_room(data):
    username = data['username']
    room_id = data['room_id']
    leave_room(room_id)

    if room_id in active_rooms:
        active_rooms[room_id].remove(username)

    emit('user_left', {'username': username}, room=room_id)
    emit('active_users', {'users': active_rooms[room_id]}, room=room_id, include_self=False)

@socketio.on('sdp')
async def sdp(data):
    pc = get_peer_connection(data)
    if data['type'] == 'offer':
        await pc.setRemoteDescription(RTCSessionDescription(sdp=data['sdp'], type='offer'))
        answer = await pc.createAnswer()
        await pc.setLocalDescription(answer)
        await socketio.emit('sdp', {
            'type': 'answer',
            'sdp': pc.localDescription.sdp,
            'pc_id': data['pc_id'],
            'room_id': data['room_id'],
            'username': data['username']
        })
    elif data['type'] == 'answer':
        await pc.setRemoteDescription(RTCSessionDescription(sdp=data['sdp'], type='answer'))

def get_peer_connection(data):
    pc = RTCPeerConnection()
    @pc.on('datachannel')
    def on_datachannel(channel):
        @channel.on('message')
        def on_message(message):
            emit('data_channel_message', {'message': message})
    pc_id = data['pc_id']
    pcs[pc_id] = pc
    pc.pc_id = pc_id
    pc.username = data['username']
    return pc

if __name__ == '__main__':
    socketio.run(app, debug=True)


# coding=utf-8
import os
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from center_loss import get_center_loss

slim = tf.contrib.slim
# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
# GPU 参数配置
def GPU_config(rate=0.5):

    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"      # 按照PCI_BUS_ID顺序从0开始排列GPU设备
    os.environ["CUDA_VISIBLE_DEVICES"] = "1"    # 设置当前使用的GPU设备仅为0号设备
    gpuConfig = tf.ConfigProto()
    gpuConfig.allow_soft_placement = False      #设置为True，当GPU不存在或者程序中出现GPU不能运行的代码时，自动切换到CPU运行
    gpuConfig.gpu_options.allow_growth = True       #设置为True，程序运行时，会根据程序所需GPU显存情况，分配最小的资源
    gpuConfig.gpu_options.per_process_gpu_memory_fraction = rate    #程序运行的时，所需的GPU显存资源最大不允许超过rate的设定值

    return gpuConfig

LAMBDA = 0.5
CENTER_LOSS_ALPHA = 0.5
NUM_CLASSES = 10

with tf.name_scope('input'):
    input_images = tf.placeholder(tf.float32, shape=(None,28,28,1), name='input_images')
    labels = tf.placeholder(tf.int64, shape=(None), name='labels')

global_step = tf.Variable(0, trainable=False, name='global_step')

def inference(input_images):
    with slim.arg_scope([slim.conv2d], weights_initializer=slim.variance_scaling_initializer(),
        activation_fn=tf.nn.relu, normalizer_fn= slim.batch_norm, kernel_size=3, padding='SAME'):
        with slim.arg_scope([slim.max_pool2d], kernel_size=2):

            x = slim.conv2d(input_images, num_outputs=32, scope='conv1_1')
            x = slim.conv2d(x, num_outputs=32, scope='conv1_2')
            x = slim.max_pool2d(x, scope='pool1')

            x = slim.conv2d(x, num_outputs=64, scope='conv2_1')
            x = slim.conv2d(x, num_outputs=64, scope='conv2_2')
            x = slim.max_pool2d(x, scope='pool2')

            x = slim.conv2d(x, num_outputs=128, scope='conv3_1')
            x = slim.conv2d(x, num_outputs=128, scope='conv3_2')
            x = slim.max_pool2d(x, scope='pool3')

            x = slim.flatten(x, scope='flatten')

            feature = slim.fully_connected(x, num_outputs=2, activation_fn=None, scope='fc1')

            x = tf.nn.relu(feature)

            x = slim.fully_connected(x, num_outputs=10, activation_fn=None, scope='fc2')

    return x, feature

def build_network(input_images, labels, ratio=0.5):
    logits, features = inference(input_images)

    with tf.name_scope('loss'):
        with tf.name_scope('center_loss'):
            center_loss, centers, centers_update_op = get_center_loss(features, labels, CENTER_LOSS_ALPHA, NUM_CLASSES)
        with tf.name_scope('softmax_loss'):
            softmax_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits))
        with tf.name_scope('total_loss'):
            total_loss = softmax_loss + ratio * center_loss

    with tf.name_scope('acc'):
        accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.arg_max(logits, 1), labels), tf.float32))

    with tf.name_scope('loss/'):
        tf.summary.scalar('CenterLoss', center_loss)
        tf.summary.scalar('SoftmaxLoss', softmax_loss)
        tf.summary.scalar('TotalLoss', total_loss)

    return logits, features, total_loss, accuracy, centers_update_op

logits, features, total_loss, accuracy, centers_update_op = build_network(input_images, labels, ratio=LAMBDA)

mnist = input_data.read_data_sets('tmp/mnist', reshape=False)

optimizer = tf.train.AdamOptimizer(0.001)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
update_ops.append(centers_update_op)
with tf.control_dependencies(update_ops):
    train_op = optimizer.minimize(total_loss, global_step=global_step)

summary_op = tf.summary.merge_all()
sess = tf.Session(config=GPU_config())
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter('tmp/mnist_log', sess.graph)

mean_data = np.mean(mnist.train.images, axis=0)

step = sess.run(global_step)
while step <= 80000:
    batch_images, batch_labels = mnist.train.next_batch(128)
    _, summary_str, train_acc, train_loss = sess.run(
        [train_op, summary_op, accuracy, total_loss],
        feed_dict={
            input_images: batch_images - mean_data,
            labels: batch_labels,
        })
    print ('train_acc:{:.4f}'.format(train_loss))
    step += 1

    writer.add_summary(summary_str, global_step=step)

    if step % 200 == 0:
        vali_image = mnist.validation.images - mean_data
        vali_acc, val_loss = sess.run(
            [accuracy,total_loss],
            feed_dict={
                input_images: vali_image,
                labels: mnist.validation.labels
            })
        print ('val_acc:{:.4f}'.format(val_loss))
        print(("step: {}, train_acc:{:.4f}, vali_acc:{:.4f}".
              format(step, train_acc, vali_acc)))

# 训练集
feat = sess.run(features, feed_dict={input_images:mnist.train.images[:10000]-mean_data})

# %matplotlib inline
import matplotlib.pyplot as plt

labels = mnist.train.labels[:10000]

f = plt.figure(figsize=(16,9))
c = ['#ff0000', '#ffff00', '#00ff00', '#00ffff', '#0000ff',
     '#ff00ff', '#990000', '#999900', '#009900', '#009999']
for i in range(10):
    plt.plot(feat[labels==i,0].flatten(), feat[labels==i,1].flatten(), '.', c=c[i])
plt.legend(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
plt.grid()
plt.show()

# 测试集
feat = sess.run(features, feed_dict={input_images:mnist.test.images[:10000]-mean_data})

# %matplotlib inline
import matplotlib.pyplot as plt

labels = mnist.test.labels[:10000]

f = plt.figure(figsize=(16,9))
c = ['#ff0000', '#ffff00', '#00ff00', '#00ffff', '#0000ff',
     '#ff00ff', '#990000', '#999900', '#009900', '#009999']
for i in range(10):
    plt.plot(feat[labels==i,0].flatten(), feat[labels==i,1].flatten(), '.', c=c[i])
plt.legend(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
plt.grid()
plt.show()

sess.close()


import logging
import time
from concurrent import futures
from typing import Dict

import grpc

from feast.core import FeatureSet_pb2 as FeatureSetProto
from feast.core.CoreService_pb2 import ListFeatureSetsResponse
from feast.core.CoreService_pb2_grpc import CoreServiceStub
from feast.serving import ServingService_pb2_grpc as Serving
from feast.serving.ServingService_pb2 import GetFeastServingInfoResponse

_ONE_DAY_IN_SECONDS = 60 * 60 * 24

class ServingServicer(Serving.ServingServiceServicer):
    def __init__(self, core_url: str = None):
        if core_url:
            self.__core_channel = None
            self.__connect_core(core_url)
            self._feature_sets = (
                dict()
            )  # type: Dict[str, FeatureSetProto.FeatureSetSpec]

    def __connect_core(self, core_url: str):
        if not core_url:
            raise ValueError("Please set Feast Core URL.")

        if self.__core_channel is None:
            self.__core_channel = grpc.insecure_channel(core_url)

        try:
            grpc.channel_ready_future(self.__core_channel).result(timeout=5)
        except grpc.FutureTimeoutError:
            raise ConnectionError(
                "connection timed out while attempting to connect to Feast Core gRPC server "
                + core_url
            )
        else:
            self._core_service_stub = CoreServiceStub(self.__core_channel)

    def __get_feature_sets_from_core(self):
        # Get updated list of feature sets
        feature_sets = (
            self._core_service_stub.ListFeatureSets
        )  # type: ListFeatureSetsResponse

        # Store each feature set locally
        for feature_set in list(feature_sets.feature_sets):
            self._feature_sets[feature_set.name] = feature_set

    def GetFeastServingVersion(self, request, context):
        return GetFeastServingInfoResponse(version="0.3.2")

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    Serving.add_ServingServiceServicer_to_server(ServingServicer(), server)
    server.add_insecure_port("[::]:50052")
    server.start()
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)

if __name__ == "__main__":
    logging.basicConfig()
    serve()


if __name__ == '__main__':
    import csv
    import os
    import time

    from binance.client import Client

    from analyzer import *
    from config import *
    from crypto import *

    tic = time.perf_counter()
    config = Config()
    crypto = Crypto(config)

    # 交易用的貨幣，等同於買股票用的現金
    cash_asset = "USDT"

    # 要排除的貨幣
    exclude_assets = []

    watching_symbols = crypto.get_tradable_symbols(cash_asset, exclude_assets)

    fromdate = "01-01-2021"
    todate = "05-01-2021"  # API有問題，目前不管怎樣都會抓到今天為止
    KLINE_INTERVAL = Client.KLINE_INTERVAL_15MINUTE
    BASE_DIR = 'history_klines'

    os.makedirs(BASE_DIR, mode=0o755, exist_ok=True)

    for symbol_info in watching_symbols:
        symbol = symbol_info.symbol
        base_asset = symbol_info.base_asset

        csv_path = os.path.join(BASE_DIR, f'{symbol}.csv')

        # if os.path.exists(csv_path):
        #     print(f'[{symbol}] skip...')
        #     continue

        csvfile = open(csv_path, 'w', newline='')
        candlestick_writer = csv.writer(csvfile, delimiter=',')
        print(f'[{symbol}] start downloading...')
        candlesticks = crypto.get_historical_klines(
            symbol, KLINE_INTERVAL, fromdate, todate)
        for candlestick in candlesticks:
            candlestick[0] = candlestick[0] / 1000  # 為了轉成float型態
            candlestick_writer.writerow(candlestick)

        print(f'[{symbol}] downloaded to {csv_path}')
        csvfile.close()


'''
자연수 N - 숫자 재배열, N보다 큰 N의 배수 만들 수 있는지

'''
import sys
sys.stdin = open("input.txt", "r")

from itertools import permutations

T = int(input())
for i in range(1, T+1):
    data = list(map(int, input()))
    original = int(''.join(map(str, data)))

    result = 'impossible'
    for value in permutations(data, len(data)):
        if int(''.join(map(str, value))) > original:
            if int(''.join(map(str, value))) % original:
                result = 'possible'
                break
    print('#%d %s' %(i, result))


import time

import asyncio
import pytest

from openlimit import ChatRateLimiterWithRedis

@pytest.fixture(scope="module")
def chat_params():
    return {
        "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Who won the world series in 2020?"}
        ],
        "max_tokens": 1,
        "n": 1
    }

@pytest.mark.asyncio
async def test_rate_limiter_with_redis(chat_params):
    rate_limiter_async = ChatRateLimiterWithRedis(
        request_limit=200,
        token_limit=4000,
        redis_url="redis://localhost:6379"  # Update this to your Redis URL
    )

    async def rate_limited_function_async(**chat_params):
        async with rate_limiter_async.limit(**chat_params):
            return "success"

    start_time = asyncio.get_event_loop().time()
    duration = 5  # seconds
    successful_calls = 0

    while (asyncio.get_event_loop().time() - start_time) < duration:
        result = await rate_limited_function_async(**chat_params)
        if result == "success":
            successful_calls += 1

    # Check if the number of successful calls is within the rate limits
    assert 0 < successful_calls <= rate_limiter_async.request_limit * (duration / 60)


from flask_restful import Resource
from NewsApp.models import News
from flask import request

class NewsResource(Resource):
    def get(self):
        offset = request.args.get('offset')
        count = request.args.get('count')

        if offset is None:
            offset = 0
        else:
            try:
                offset = int(offset)
            except:
                return {'error': 'Offset could be an integer'}

        if count is None:
            count = 1
        else:
            try:
                count = int(count)
            except:
                return {'error': 'Count could be an integer'}

        count = max(1, min(20, count))
        offset = max(0, offset)

        news_query = News.query.offset(offset).limit(count).all()
        return {'news': [n.vk_json for n in news_query]}, 200


##########################################################################################################
# group tests by year of creasion and version
##########################################################################################################
import csv
import json

with open('../../computation/version/results/result.json', 'r') as f:
    versions = json.load(f)

with open('../../computation/official/results/testChangeDates.json', 'r') as f:
    history = json.load(f)
del history['hashLastCommit']

history = {test: history[test][0].split('-')[0] for test in history}
versions = {test: int(versions[test]) if isinstance(versions[test], int) or len(versions[test]) < 2 else -1 for test in versions}

tests = versions.keys()

resultsDict = {}

for test in tests:
    standard = resultsDict.get(versions[test], {})
    standard[history[test]] = standard.get(history[test],0) +1
    resultsDict[versions[test]] = standard

years = list(set(history.values()))
years.sort()

standards = list(set(versions.values()))
standards.sort()
print(standards)
Header = ['standard'] + years
Body = []

for stand in standards:
    Body.append([stand])
    for year in years:
        numTests = resultsDict[stand].get(year, 0)
        Body[-1].append(numTests)

with open('results/createdOnPerVersion.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerow(Header)
    writer.writerows(Body)


import open3d as o3d
import numpy as np
from typing import List
from .Rect import Rect3d

class Octree:
    def __init__(self, parent=None, center=None, size=None, point_cloud: o3d.geometry.PointCloud = None) -> None:
        if parent != None:
            self.parent = parent
            self.root = self.parent.root
            self.level = self.parent.level + 1
            self.children = [None] * 8
            self.size = size
            self.center = center
        if point_cloud:
            self.cloud = point_cloud
            self.extension: Rect3d = self.calc_extension()
            self.center = self.extension.get_center()
            self.size = self.extension.get_max_size()
            self.indices = np.arange(len(self.cloud.points))
            self.root = self
            self.parent = self
            self.level = 0
            self.leaf_table: List["None | Octree"] = [None] * len(point_cloud.points)
        self.leaf = True
        # get_box_points for corners

    def calc_extension(self) -> Rect3d:
        min_ = np.full(3, 1)*np.inf
        max_ = -min_
        for point in self.cloud.points:
            for dim in range(3):
                min_[dim] = min(min_[dim], point[dim])
                max_[dim] = max(max_[dim], point[dim])
        return Rect3d(min_, max_)

    def get_level(self)->int:
        if self == self.root:
            return 0
        return 1 + self.parent.get_level()

    def partition(self, levels, min_points=1, min_size=0.0) -> None:
        if not self.leaf:
            for child in self.children:
                if child != None:
                    child.partition(levels-1, min_points, min_size)
        else:
            if levels <= 0 or len(self.indices) <= 1 <= min_points or self.size < min_size:
                return
            new_size = self.size*0.5
            new_centers = self.calc_new_centers(new_size)
            self.leaf = False

            # create children
            self.children: List["None| Octree"] = [None]*8

            # split points
            for i in self.indices:

                child_index: int = self.calc_child_index(
                    self.root.cloud.points[i])
                if self.children[child_index] == None:
                    self.children[child_index] = Octree(parent=self, center=new_centers[child_index], size=new_size)
                    self.children[child_index].indices = []
                self.children[child_index].indices.append(i)
                # update current leaf where point is stored
                self.root.leaf_table[i] = self.children[child_index]

            self.indices = []

            for child in self.children:
                if child != None:
                    child.partition(levels-1, min_points, min_size)

    def calc_new_centers(self, new_size):
        new_centers = [0]*8
        for child in range(8):
            d0 = new_size * (((child & 4) >> (2) << 1) - 1)
            d1 = new_size * (((child & 2) >> (1) << 1) - 1)
            d2 = new_size * (((child & 1) << 1) - 1)
            new_centers[child] = self.center + [d0,d1,d2]
        return new_centers

    def calc_child_index(self, point) -> int:
        index = 0
        if point[0] > self.center[0]:
            index += 4
        if point[1] > self.center[1]:
            index += 2
        if point[2] > self.center[2]:
            index +=  1
        return index

    def num_points(self):
        if self.leaf:
            return len(self.indices)
        num_points = 0
        for child in self.children:
            if child != None:
                num_points += child.num_points()
        return num_points

    def get_points(self):
        if self.leaf:
            return self.indices
        points = []
        for child in self.children:
            if child != None:
                points += child.get_points()
        return points

    def get_containing_leaf(self, point):
        return self.root.leaf_table[point]

    def is_root(self) -> bool:
        return self.parent == self


import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

from util.dataset import read_dataset
from util.dataset import get_dataset_for_deepcluster
from util.dataset import save_clustered_results

from util.cluster import construct_hierarchy
from util.cluster import query
from util.cluster import update_label_confidence
from util.cluster import update_partition
from util.cluster import get_leaves
from util.cluster import transductive_propagation

from util.cnn import get_alexnet
from util.cnn import remove_last_layer

from scipy import cluster

dir_name = input('forneça o diretório: ')
dir_results = input('forneça o diretório onde irá salvar os resultados: ')
num_class = int(input('forneça o número de classes: '))

max_deepcluster_it = 100

print('Carregando imagens...')
dataset = read_dataset(dir_name)
x = get_dataset_for_deepcluster(dataset)

print('Construindo modelo de Rede Neural Convolutiva...')
alexnet = get_alexnet(x, num_class)

print("Realizadno agrupamento profundo...")
for i in range(max_deepcluster_it):

    alexnet_wll = remove_last_layer(alexnet)
    x_wards = alexnet_wll.predict(x)

    wards_dendrogram = cluster.hierarchy.ward(x_wards)
    clusters = cluster.hierarchy.cut_tree(wards_dendrogram, n_clusters=num_class)

    alexnet.fit(x, clusters, epochs=1)
    acc_local = alexnet.history.history['accuracy'][0]

    if (acc_local >= 0.9):
        break

node = construct_hierarchy(wards_dendrogram, num_class)
partition = [node]
queried_objs = []
to_query = 1

while (to_query):

    q_object = query(partition)

    img_show = dataset[q_object.id]["img_rgb"].show()
    q_label = int(input('Forneça o rótulo de classe da imagem: '))

    queried_objs.append(dataset[q_object.id])
    update_label_confidence(q_object, q_label, num_class)
    partition = update_partition(partition)

    objects = []
    get_leaves(node, objects)
    objects_labeled = transductive_propagation(objects)

    save_clustered_results(dataset, objects_labeled, num_class, dir_results)

    to_query = int(input('Deseja continuar o processo de rotulação (1) SIM - (0) NÃO: '))


import os, sys

import django

os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.testproject.settings'

test_dir = os.path.dirname(__file__)
sys.path.insert(0, test_dir)

django.setup()

from django.test.utils import get_runner
from django.conf import settings

def runtests():
    app_label = ['testapp']
    if django.VERSION < (1, 8):
        from django.test.simple import DjangoTestSuiteRunner
        def run_tests(app_label, verbosity, interactive):
            runner = DjangoTestSuiteRunner(verbosity=verbosity, interactive=interactive, failfast=False)
            return runner.run_tests(app_label)
    else:
        from django.test.runner import DiscoverRunner
        def run_tests(app_label, verbosity, interactive):
            runner = DiscoverRunner(verbosity=verbosity, interactive=interactive, failfast=False)
            return runner.run_tests(app_label)
    failures = run_tests(app_label, verbosity=1, interactive=True)
    sys.exit(failures)

if __name__ == '__main__':
    runtests()


import glob
import os
import pickle
from datetime import datetime

from brightline.addressParser import AddressParser
from brightline.dataParser import DataParser
from brightline.exporter import Exporter
from brightline.geoContainer import GeoContainer

def get_latest_dump():
    path = os.getcwd() + '/resources/.data/*.container'
    dumps = glob.glob(path)
    if not dumps:
        return None
    else:
        newest = max(dumps, key=os.path.getctime)
        return newest

def main():
    start_time = datetime.now()
    dump = get_latest_dump()
    if dump:
        filehandler = open(dump, 'rb')
        container = pickle.load(filehandler)

    else:
        data_parser = DataParser(os.getcwd() + '/resources/CleanData_UCBerkeley.csv')
        container = data_parser.read()
        address_parser = AddressParser(os.getcwd() + '/resources/InternalLocData.csv')
        addr_dict = address_parser.read()
        container.merge_dict(addr_dict)
        container.save()

    export = Exporter(container)
    export.generate_geoJSON()
    end_time = datetime.now()
    print('completed in {} seconds'.format(end_time-start_time))

if __name__ == '__main__':
    main()


from flask import Flask
from flask_cors import CORS
import html

# paths
from auth.tba import auth_tba
from devrant.rants import devrant_rants
from status import status
from frc.team import frc_teams
from databuffer.monitor import databuffer_monitor
from databuffer.buffer import cleaning_thread
from auth.keygen import auth_keygen
from frc.events import frc_events
from auth.devices.register import dev_reg

app = Flask(__name__)
CORS(app)

# Blueprint registration
blueprints = []

def register(blueprint):
    app.register_blueprint(blueprint)
    blueprints.append(blueprint)

def getUrls(blueprint):
    temp_app = Flask(__name__)
    temp_app.register_blueprint(blueprint)
    return [str(p) for p in temp_app.url_map.iter_rules()]

# Set up blueprints
print("Registering endpoints")
register(auth_tba)
register(devrant_rants)
register(status)
register(frc_teams)
register(databuffer_monitor)
register(auth_keygen)
register(frc_events)
register(dev_reg)

@app.route('/')
def index():
    return """
    Welcome to the RetryLife api v2.

    This is a dark and mysterious place.
    """

@app.route("/endpoints")
def endpoints():
    # Return a list of all endpoints
    output = "Avalible API endpoints:"
    for blueprint in blueprints:
        for url in getUrls(blueprint):
            if url[:7] == "/static":
                continue
            output += f"
\t{html.escape(url)}"
    return output

# Start task threads
print("Starting tasks")
cleaning_thread.start()

if __name__ == '__main__':
    # Start webserver
    app.run(host='0.0.0.0')


# libraries
import time

# local modules
import datetime as dt
# for seding request to binance and getting data
from binance.client import Client
from binance.enums import *

# data manipulation
import pandas as pd

# for POST and GET request
import requests

# json
import json
# {
#     "dict":123,
#     "hello":23
# }

# for technical indicators
from finta import TA

# for telegram messages
import telepot

# connecting telegram bot
# telegram bot is made by botfather on telegram
# search botfather on telegram
# type /newbot
# finally we get information like 1913570782:AAHIrTJDK7-toxsxqOv27Dh9wYU2d5HYfgk
bot = telepot.Bot('1913570782:AAHIrTJDK7-toxsxqOv27Dh9wYU2d5HYfgk')
bot.getMe()

#reading 'parameters.csv'
# df3 -> dataframe (datatype)
df3 = pd.read_csv("parameters.csv")
df3.set_index("parameters",inplace=True)
# .T is used for transposing a dataframe
df1=df3.T

#logging in binance
client = Client((df1['binance_api_key'][0]), str(df1['binance_api_secret_key'][0]))

#historical data
def candle(symbol, interval):

    # root_url -> url where we are retrieving data from
    root_url = 'https://api.binance.com/api/v1/klines'
    url = root_url + '?symbol=' + symbol + '&interval=' + interval
    data = json.loads(requests.get(url).text)

    # data retrieve and converted to dataframe and saved in df variable
    df = pd.DataFrame(data)
    df.columns = ['Datetime',
                'Open', 'High', 'Low', 'Close', 'volume',
                'close_time', 'qav', 'num_trades',
                'taker_base_vol', 'taker_quote_vol', 'ignore']

    # datetime (timestamp -> datetime)
    df.index = [dt.datetime.fromtimestamp(x / 1000.0) for x in df.close_time]

    # droping the columns not needed
    df.drop(['close_time','qav','num_trades','taker_base_vol', 'taker_quote_vol', 'ignore'],axis=1,inplace=True)

    # strings to -> float value
    df['Open']=pd.to_numeric(df["Open"], downcast="float")
    df["High"]=pd.to_numeric(df["High"], downcast="float")
    df["Low"]=pd.to_numeric(df["Low"], downcast="float")
    df["Close"]=pd.to_numeric(df["Close"], downcast="float")
    df["volume"]=pd.to_numeric(df["volume"], downcast="float")

    # ATR indicator

    # made a new column named 'FIXED_ATR'
    df['FIXED_ATR']=TA.ATR(df,int(df1['atr_fixed'][0]))
    # Special data for special requirements of strategy.
    # calculating maximum and minimum values of last X BARS
    df['X_BARS_HIGH']=df['High'][-int(df1['X_bars_high'][0]):-1].max()
    df['X_BARS_LOW']=df['Low'][-int(df1['X_bars_low'][0]):-1].min()

    #atr value calulationg for 'TRAILING_ATR'
    df['TRAILING_ATR']=TA.ATR(df,int(df1['atr_trailing'][0]))

    # Datetime Open High Low Close Volume FIXED_ATR X_BARS_HIGH X_BARS_LOW TRAILING_ATR
    return df

#getting latest price of instrument
def ltp_price(instrument):
    prices = client.get_all_tickers()
    for i in range(len(prices)):
        if prices[i]['symbol']==str(instrument):
            return float(prices[i]['price'])

#order function for buy
def market_order():

    # NOOB Code -> should not put all the variables global, instead should put it in arguments
    global df,df1,distance_long,distance_short,buy_price,sell_price,quantity_buy,quantity_sell,fixed_buy_atr,fixed_sell_atr

    # present ATR VALUE In python -> -1 -> latest value
    fixed_buy_atr=float(df['FIXED_ATR'][-1])

    # balance of clients account
    p_l=float(client.futures_account_balance()[0]['balance'])

    # it is done so that we can be sure of what maximum loss we can take
    stoploss=float(df1['stop_loss_precentage'][0])/100 * p_l
    quantity_buy=int(stoploss/fixed_buy_atr)

    # uncomment for actually sending order on binance account

    # order = client.futures_create_order(
    #     symbol=str(df2['symbol_binance'][0]),
    #     side=Client.SIDE_BUY,
    #     type=Client.ORDER_TYPE_MARKET,

    #     quantity=float(df1['quantity'][0]))

    # noob method of doing
    buy_price=ltp_price(df1['symbol_binance'][0])

    l=1

#order function for sell
def market_order1():
    global df,df1,distance_long,distance_short,buy_price,sell_price,quantity_buy,quantity_sell,fixed_buy_atr,fixed_sell_atr

    fixed_sell_atr=float(df['FIXED_ATR'][-1])

    p_l=float(client.futures_account_balance()[0]['balance'])
    stoploss=float(df1['stop_loss_precentage'][0])/100 * p_l
    quantity_sell=int(stoploss/fixed_sell_atr)

    # order = client.futures_create_order(
    #     symbol=str(df1['symbol_binance'][0]),
    #     side=Client.SIDE_SELL,
    #     type=Client.ORDER_TYPE_MARKET,
    #     quantity=float(df1['quantity'][0]))

    sell_price=ltp_price(df1['symbol_binance'][0])

    l=1

#getting signal for buy/sell/squareoff's
def trade_signal(instrument,l_s):

    global df,df1,distance_long,distance_short,buy_price,sell_price,quantity_buy,quantity_sell,fixed_buy_atr,fixed_sell_atr,times1

    # current price of the currency
    ltp=ltp_price(df1['symbol_binance'][0])

    # initially signal is saved as ""
    signal=""

    if l_s=="":

        # if 5 minutes has already passed after starting the bot then times1 is updated
        if time.time()>times1+300:
            times1=time.time()

        # if current price -> is greater then X_BARS_HIGH current
        if ltp>float(df['X_BARS_HIGH'][-1]):
            signal="buy"

        # if current price -> is less then X_BARS_LOW current
        if ltp<float(df['X_BARS_LOW'][-1]):
            signal="sell"

    elif l_s=="long":

        # if distance between current value and current stoploss is 10% -> 10% = distance1_long
        distance1_long = ltp-df['TRAILING_ATR'][-1]*float(df1['atr_trailing_multiplier'][0])

        # trailing stoploss shifts
        # shifts when price goes up
        if distance1_long>distance_long:
            distance_long=ltp-df['TRAILING_ATR'][-1]*float(df1['atr_trailing_multiplier'][0])

        # squares off if fixed stoploss is hit /or/ it gets below trailing stoploss (distance_long)
        if ltp<=buy_price-fixed_buy_atr*float(df1['atr_fixed_multiplier'][0]) or ltp<=distance_long:
            distance_long=0

            signal="squareoffsell"

        if time.time()>times1+300:
            times1=time.time()

    elif l_s=="short":

        distance1_short=ltp+df['TRAILING_ATR'][-1]*float(df1['atr_trailing_multiplier'][0])

        if distance1_short<distance_short:
            distance_short=ltp+df['TRAILING_ATR'][-1]*float(df1['atr_trailing_multiplier'][0])
        if ltp>=sell_price+fixed_sell_atr*float(df1['atr_fixed_multiplier'][0]) or ltp>=distance_short:
            distance_short=100000000000

            signal="squareoffbuy"

        if time.time()>times1+300:

            times1=time.time()

    return signal

# %%

#main function
def main():
    global df,df1,distance_long,distance_short,buy_price,sell_price,quantity_buy,quantity_sell,fixed_buy_atr,fixed_sell_atr,position

    # reading parameters.csv file
    df3 = pd.read_csv("parameters.csv")
    df3.set_index("parameters",inplace=True)
    df1=df3.T

    # getting latest data about the symbol
    ltp=ltp_price(df1['symbol_binance'][0])

    #getting historical data
    df=candle(df1['symbol_binance'][0],str(df1['time_frame'][0]))

    # getting if there is a signal buy | sell | squuareoffbuy | squareoffsell
    signal=trade_signal(df1['symbol_binance'][0],position)

    #buying/selling/ squarring  off
    if signal=='buy':
        market_order()

        # variable which says that presently there is a 'long' position
        # this variable is used so that we dont have multiple buy /sell
        position='long'

        #sending update on telegram
        bot.sendMessage(1039725953,f"New long position initiated at {buy_price}")

    if signal=='sell':
        market_order1()
        position='short'
        bot.sendMessage(1039725953,f"New short position initiated at {sell_price}")

    if signal=="squareoffsell":
        market_order1()

        position=''
        bot.sendMessage(1039725953,f"long position squared of at {sell_price}")
        bot.sendMessage(1039725953,f" profit of {((sell_price-buy_price)/buy_price)*100*int(df1['binance_X'][0])}")

    if signal=="squareoffbuy":
        market_order()

        position=''
        bot.sendMessage(1039725953,f"short position squared of at {buy_price}")
        bot.sendMessage(1039725953,f" profit of {((sell_price-buy_price)/buy_price)*100*int(df1['binance_X'][0])}")

l=0
distance_short=100000000000
distance_long=0
times1=time.time()
position=''

#infinite loop
while True:
    try:
        # calling main function
        main()

    except Exception as e:
        bot.sendMessage(1039725953,str(e))

# CHATGPT -> tool ||


#import
import matplotlib
import numpy as np
from matplotlib import pyplot
import itertools

#変数
class CMath ( object ):

    ###########################################
    # 機能 ：コンストラクタ
    ###########################################
    def __init__ ( self, ):
        self.value = "Class2Init"

    ###########################################
    # 機能 ：グラフを描画してみる
    ###########################################
    def TestDrawGraph ( self, ):

        # データの定義(サンプルなのでテキトー)
        x = list( range( 10 ) )
        y = list( range( 10 ) )

        # グラフの描画
        pyplot.plot( x, y )
        pyplot.show( )

    ###########################################
    # 機能 ：配列を作る
    ###########################################
    def Conv2Arr ( self,obj ):
        return np.array( obj )

    ###########################################
    # 機能 ：グラフを描画する
    ###########################################
    def DrawGraph ( self,x ):

        ## グラフの描画
        #pyplot.plot(x, y)
        #pyplot.show()

        # グラフ化
        matplotlib.pyplot.plot( x[::10] )
        matplotlib.pyplot.grid( )
        matplotlib.pyplot.show( )

    ###########################################
    # 機能 ：配列の要素が一定個数以上0が連続している区間を返す
    # 例 ：arr = np.array( [0,1,2,0,0,0,6,7,0,0,10,11,0,0,0,0] )
    # 例 ：val = 0
    # 例 ：width = 3
    # 例 ：iSeqRanges = [[3,6],[12,15]]
    # 例 ：x = [3,6,12,15]
    # 例 ：y = [0,0,0,0]
    ###########################################
    def SearchIntervalWithSameVal ( self,arr,val,width ):
        iSeqRanges = [ ]
        iTmpL :int = 0
        iTmpR :int = 0
        bInPrgrs = False
        iCnt = 0
        for idx in range( len( arr ) ):

            #値が一致⇒カウント増加
            if( arr[idx] == val ):
                iCnt = iCnt + 1
            else:
                iCnt = 0

            #カウントに応じて判断
            if( ( 0 >= iCnt ) or ( len( arr ) == idx ) ):
                if( bInPrgrs == True ):
                    iTmpR = idx - 1
                    bInPrgrs = False
                    iSeqRanges.append( [iTmpL,iTmpR] )
            elif ( 1 >= iCnt ):
                iTmpL = idx
            elif( width >= iCnt ):
                pass
            else:
                bInPrgrs = True

        ## 結果の確認
        #iNdarrTmp = np.array( iSeqRanges )
        #x = list(itertools.chain.from_iterable(iNdarrTmp))
        #y = np.zeros( len( x ) )
        #pyplot.plot( arr[:],c="blue",zorder=1 )
        #pyplot.scatter( x[:],y[:],c="red", zorder=2 )
        #pyplot.show( )

        return iSeqRanges

    ###########################################
    # 機能 ：グラフを描画する
    ###########################################
    def search_sequence_numpy ( self,arr,seq ):
    #""" Find sequence in an array using NumPy only.

    #Parameters
    #----------
    #arr : input 1D array
    #seq : input 1D array

    #Output
    #------
    #Output : 1D Array of indices in the input array that satisfy the
    #matching of input sequence in the input array.
    #In case of no match, an empty list is returned.
    #"""Q

        # Store sizes of input array and sequence
        Na, Nseq = arr.size, seq.size

        # Range of sequence
        r_seq = np.arange( Nseq )

        # Create a 2D array of sliding indices across the entire length of
        # input array.
        # Match up with the input sequence & get the matching starting indices.
        M = ( arr[np.arange( Na - Nseq + 1 )[:,None] + r_seq] == seq ).all( 1 )

        # Get the range of those indices as final output
        if M.any( ) > 0:
            return np.where( np.convolve( M,np.ones( ( Nseq ),dtype=int ) ) > 0 )[0]
        else:
            return [ ]         # No match found

###########################################
#引用1
###########################################


import json, time
from django.http import JsonResponse
from django.views.generic import TemplateView
from core.views.base import CustomView
from core.utils import utils_dates
from facturation.forms.edition_prestations import Formulaire

def Generer_pdf(request):
    # Récupération des options
    form = Formulaire(request.POST, request=request)
    if not form.is_valid():
        return JsonResponse({"erreur": "Veuillez compléter les paramètres"}, status=401)
    options = form.cleaned_data

    # Récupération des paramètres
    options["activites"] = json.loads(options["activites"])
    if not options["activites"]["ids"]:
        return JsonResponse({"erreur": "Veuillez cocher au moins une activité"}, status=401)

    # Récupération de la période
    date_debut = utils_dates.ConvertDateENGtoDate(options["periode"].split(";")[0])
    date_fin = utils_dates.ConvertDateENGtoDate(options["periode"].split(";")[1])
    options["periode"] = (date_debut, date_fin)

    # Création du PDF
    from facturation.utils import utils_impression_prestations
    impression = utils_impression_prestations.Impression(titre="Edition des prestations", dict_donnees=options)
    if impression.erreurs:
        return JsonResponse({"erreur": impression.erreurs[0]}, status=401)
    nom_fichier = impression.Get_nom_fichier()
    time.sleep(1)
    return JsonResponse({"nom_fichier": nom_fichier})

class View(CustomView, TemplateView):
    menu_code = "edition_prestations"
    template_name = "facturation/edition_prestations.html"

    def get_context_data(self, **kwargs):
        context = super(View, self).get_context_data(**kwargs)
        context['page_titre'] = "Edition des prestations"
        context['box_titre'] = "Edition des prestations"
        context['box_introduction'] = "Renseignez les paramètres et cliquez sur le bouton Générer le PDF. La génération du document peut nécessiter quelques instants d'attente."
        if "form" not in kwargs:
            context['form'] = Formulaire(request=self.request)
        return context


"""employee_management URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from employees.views import (employee_registration,
                             employee_login,
                             get_all_employees,
                             edit_employee,
                             delete_employee,
                             logout_view,
                             export_employee,
                             employee_search,
                             bulk_employee_registation,
                             get_email_via_gmail,
                             reply_email,
                             callback)

urlpatterns = [

    path('admin/', admin.site.urls),
    path('employee-registration/', employee_registration, name="employee-registration"),
    path('login/', employee_login, name="login"),
    path('logout/', logout_view, name="logout"),
    path('get-all-employees/', get_all_employees, name="get-all-employees"),
    path('edit-employee/<str:employee_id>/', edit_employee, name="edit-employee"),
    path('delete-employee/<str:employee_id>/', delete_employee, name="delete-employee"),
    path('export-employees/', export_employee, name="export-employees"),
    path('search-employees/', employee_search, name="search-employees"),
    path('bulk-employee-registration/', bulk_employee_registation, name="bulk-employee-registration"),
    path('get-emails/<str:employee_id>/',get_email_via_gmail,name="get-emails"),
    path('reply-email/<str:email_id>/<str:employee_id>/',reply_email,name="reply-email"),
    path('callback',callback,name="callback")

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


"""Snapshot the current depot composition and add it to the depot composition history table."""
import datetime
import os

HEADER_ROW = 100
DATE_COLUMN = 9
PORTFOLIO_FIRST_ROW = 32
PORTFOLIO_IDENTIFIER_COLUMN = 3
PORTFOLIO_SHARE_COUNT_COLUMN = 8

# can't import a utility function from another module because LibreOffice disallows it
# pylint: disable=duplicate-code
try:
    # pylint: disable=undefined-variable
    desktop = XSCRIPTCONTEXT.getDesktop()  # type: ignore
    model = desktop.getCurrentComponent()
    sheet = model.getSheets().getByName("Sparen")
    EXPORT_DIRECTORY = sheet.getCellByPosition(8, 11).getString()
except NameError:  # running tests
    EXPORT_DIRECTORY = "~/"

def snapshot_depot_composition(*args):  # pylint: disable=unused-argument
    """Snapshot the current depot composition and add it to the depot composition history table."""
    positions = _get_positions()
    new_row = _get_new_row()
    _write_depot_composition(positions, new_row)
    _write_current_date(new_row)

def _date_value(date: datetime.date) -> int:
    delta = date - datetime.date(year=1900, month=1, day=1)
    return delta.days + 2

def _write_current_date(row: int):
    sheet.getCellByPosition(DATE_COLUMN, row).setValue(_date_value(datetime.date.today()))

def _write_depot_composition(positions, new_row: int):
    for i in range(len(positions)):  # pylint: disable=consider-using-enumerate
        sheet.getCellByPosition(DATE_COLUMN + i + 1, new_row).setValue(
            _get_share_count(positions[i]))

def _get_new_row():
    row = HEADER_ROW + 1
    date = sheet.getCellByPosition(DATE_COLUMN, row).getString()
    while date:
        row += 1
        date = sheet.getCellByPosition(DATE_COLUMN, row).getString()
    return row

def write_csv(*args):  # pylint: disable=unused-argument
    """Write the depot composition history to a csv file."""
    with open(os.path.join(EXPORT_DIRECTORY, "depot_composition_history.csv"), "w",
              encoding="utf-8") as file:
        positions = _get_positions()
        file.write("Datum")
        for position in positions:
            file.write(";" + position)
        file.write("\n")
        row = HEADER_ROW + 1
        date = sheet.getCellByPosition(DATE_COLUMN, row).getString()
        while date:
            file.write(date)
            for i in range(len(positions)):
                file.write(";" + str(sheet.getCellByPosition(DATE_COLUMN + i + 1, row).getValue()))
            file.write("\n")
            row += 1
            date = sheet.getCellByPosition(DATE_COLUMN, row).getString()

def _get_share_count(position: str) -> float:
    i = 0
    value = sheet.getCellByPosition(PORTFOLIO_IDENTIFIER_COLUMN,
                                    PORTFOLIO_FIRST_ROW + i).getString()
    while value:
        if value == position:
            return sheet.getCellByPosition(PORTFOLIO_SHARE_COUNT_COLUMN,
                                           PORTFOLIO_FIRST_ROW + i).getValue()
        i += 1
        value = sheet.getCellByPosition(PORTFOLIO_IDENTIFIER_COLUMN,
                                        PORTFOLIO_FIRST_ROW + i).getString()
    return -1

def _get_positions():
    positions = []
    i = 1
    value = sheet.getCellByPosition(DATE_COLUMN + i, HEADER_ROW).getString()
    while value:
        positions.append(value)
        i += 1
        value = sheet.getCellByPosition(DATE_COLUMN + i, HEADER_ROW).getString()
    return positions


from django.contrib.auth.hashers import make_password
from django.db import migrations
import json

def create_default_artist(apps, schema_editor):
	Artist = apps.get_registered_model('core', 'Artist')
	artist = Artist()
	artist._id = 0
	artist.name = 'Unknown',
	artist.track_ids = json.dumps([])
	artist.num_tracks = 0
	artist.album_ids = json.dumps([])
	artist.num_albums = 0
	artist.save()

class Migration(migrations.Migration):
	dependencies = [
		('core', '0001_initial')
	]

	operations = [
		migrations.RunPython(create_default_artist),
	]


import tensorflow as tf
import numpy as np
import argparse
import functools
import gzip
import json
import pickle
from collections import defaultdict
from operator import mul

from tqdm import tqdm
from squad.utils import get_phrase, get_best_span

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('paths', nargs='+')
    #parser.add_argument('-o', '--out', default='ensemble.json')
    parser.add_argument("--data_path", default="inter_ensemble/data_single.json")
    parser.add_argument("--shared_path", default="inter_ensemble/shared_single.json")
    parser.add_argument('--train_data', default="/home/vera/data/squad/dev-v1.1.json")
    args = parser.parse_args()
    return args

def get_scores(context, wordss, y1_list, y2_list):
    scores = []
    phrases = []
    d = defaultdict(float)
    for y1, y2 in zip(y1_list, y2_list):
        span, score = get_best_span(y1, y2)
        phrase = get_phrase(context, wordss, span)
        scores += [score]
        phrases += [phrase]
    return phrases,scores

def prepare_data(args):
    e_list = []
    for path in tqdm(args.paths):
        with gzip.open(path, 'r') as fh:
            e = pickle.load(fh)
            e_list.append(e)

    with open(args.data_path, 'r') as fh:
        data = json.load(fh)

    with open(args.shared_path, 'r') as fh:
        shared = json.load(fh)

    predictions = {}
    for idx, (id_, rx) in tqdm(enumerate(zip(data['ids'], data['*x'])), total=len(e['yp'])):
        if idx >= len(e['yp']):
            break
        context = shared['p'][rx[0]][rx[1]]
        wordss = shared['x'][rx[0]][rx[1]]
        yp_list = [e['yp'][idx] for e in e_list]
        yp2_list = [e['yp2'][idx] for e in e_list]
        phrases, scores = get_scores(context, wordss, yp_list, yp2_list)
        predictions[id_] = (phrases, scores)

    with open(args.train_data) as dataset_file:
        dataset_json = json.load(dataset_file)
    dataset = dataset_json['data']
    train_x = []
    train_y = []
    for article in dataset:
        for paragraph in article['paragraphs']:
            for qa in paragraph['qas']:
                #total += 1
                if qa['id'] not in predictions:
                    continue
                ground_truths = list(map(lambda x: x['text'], qa['answers']))
                phrases, scores = predictions[qa['id']]
                flag = False
                for truth in ground_truths:
                    if truth in phrases:
                        labels = [1 if ph==truth else 0 for ph in phrases]
                        flag = True
                        break
                if flag:
                    train_x += [scores]
                    train_y += [labels]
    return train_x, train_y

def train_weights(train_x,train_y,epoch=10000,rate = 0.001):
    train_x = np.array(train_x)
    train_y = np.array(train_y)
    n = train_x.shape[1]
    x = tf.placeholder("float")
    y = tf.placeholder("float")
    w = tf.Variable(tf.constant([1.0/n for i in range(n)]))

    pred = x*w
    pred = pred/tf.reduce_sum(pred)
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(pred,y)
    loss = tf.reduce_sum(cross_entropy)
    optimizer = tf.train.GradientDescentOptimizer(rate).minimize(loss)

    init = tf.initialize_all_variables()

    sess = tf.Session()
    sess.run(init)
    for index in range(epoch):
        sess.run(optimizer,{x:train_x,y:train_y})
    loss = sess.run(loss,{x:train_x,y:train_y})
    w =  sess.run(w)
    print("loss:",loss)
    return w

def predictionTest(test_x,test_y,w,b):
    W = tf.placeholder(tf.float32)
    B = tf.placeholder(tf.float32)
    X = tf.placeholder(tf.float32)
    Y = tf.placeholder(tf.float32)
    n = test_x.shape[0]
    pred = tf.add(tf.mul(X,W),B)
    loss = tf.reduce_mean(tf.pow(pred-Y,2))
    sess = tf.Session()
    loss = sess.run(loss,{X:test_x,Y:test_y,W:w,B:b})
    return loss

def softmax(x):
    return np.exp(x)/np.sum(np.exp(x),axis=0)

if __name__ == "__main__":
    args = get_args()
    train_x,train_y = prepare_data(args)
    w = train_weights(train_x,train_y)
    print ('weights:',w)
    print ('softmax(weights):',softmax(w))


from typing import List

class Solution:
    def minSwapsCouples(self, row: List[int]) -> int:

        count_swap = 0
        for i in range(0, len(row), 2):
            p1 = row[i]
            if p1 % 2 == 0:
                p2 = p1 + 1
            else:
                p2 = p1 - 1
            if row[i + 1] != p2:
                p3_index = row.index(p2)
                row[i + 1], row[p3_index] = row[p3_index], row[i + 1]
                count_swap += 1
        return count_swap


import nltk
from nltk.stem import SnowballStemmer
import spacy
nlp = spacy.load("pt_core_news_sm")
stemmer = SnowballStemmer("portuguese")

examples = [
   "A empresa buscava novas estratégias",
  "A empresa buscou novas estratégias"
]

def stem_and_lemm(text):
    # Tokenização e stemming
    words = nltk.word_tokenize(text)
    stemmed_words = [stemmer.stem(word) for word in words]

    # Lematização
    doc = nlp(text)
    lemmatized_words = [token.lemma_ for token in doc]

    return stemmed_words, lemmatized_words

print("--------------")
for text in examples:
    stemmed_result, lemmatized_result = stem_and_lemm(text)
    print("Texto original:", text)
    print("Stemming:", stemmed_result)
    print("Lemmatização:", lemmatized_result)
    print("="*50)


#!/usr/bin/env python

import argparse
import rospy
import baxter_interface
import recorder
import moveit_commander
from baxter_interface import CHECK_VERSION

def main():
    """Joint and EE Pose Recorder
    Record timestamped joint and gripper positions to a file
    """
    epilog = """
Related examples:
  joint_position_file_playback.py; joint_trajectory_file_playback.py.
    """
    arg_fmt = argparse.RawDescriptionHelpFormatter
    parser = argparse.ArgumentParser(formatter_class=arg_fmt,
                                     description=main.__doc__,
                                     epilog=epilog)
    required = parser.add_argument_group('required arguments')
    parser.add_argument('-f', dest='filename', type = str, required=True, help = 'Output Joint Angle Filename')
    parser.add_argument('-r', dest='record_rate', type=int, default=100, metavar='RECORDRATE', help='rate at which to record (default: 100)')
    parser.add_argument('-m', dest='mode', type = int, default=1, help = 'JointRecorder mode: 0=Only JA, 1=All data (default: 1)')

    args = parser.parse_args(rospy.myargv()[1:])

    print("Initializing node... ")
    rospy.init_node("rsdk_joint_recorder")
    print("Getting robot state... ")
    rs = baxter_interface.RobotEnable(CHECK_VERSION)
    print("Enabling robot... ")
    rs.enable()

    #recordingRate = 100
    #mode = 1
    baxter_recorder = recorder.JointRecorder(args.filename, args.record_rate, args.mode)
    rospy.on_shutdown(baxter_recorder.stop)

    print("Recording. Press Ctrl-C to stop.")
    baxter_recorder.record()

    print("\nDone.")

if __name__ == '__main__':
    main()


import humps

class StorageConnector:
    HOPSFS = "HOPSFS"
    S3 = "S3"
    JDBC = "JDBC"
    HOPSFS_DTO = "featurestoreHopsfsConnectorDTO"
    S3_DTO = ""

    def __init__(
        self,
        id,
        name,
        description,
        featurestore_id,
        storage_connector_type,
        # members specific to type of connector
        hopsfs_path=None,
        dataset_name=None,
        access_key=None,
        secret_key=None,
        bucket=None,
        connection_string=None,
        arguments=None,
    ):
        self._id = id
        self._name = name
        self._description = description
        self._feature_store_id = featurestore_id
        self._storage_connector_type = storage_connector_type
        self._hopsfs_path = hopsfs_path
        self._dataset_name = dataset_name
        self._access_key = access_key
        self._secret_key = secret_key
        self._bucket = bucket
        self._connection_string = connection_string
        self._arguments = arguments

    @classmethod
    def from_response_json(cls, json_dict):
        json_decamelized = humps.decamelize(json_dict)
        _ = json_decamelized.pop("type")
        return cls(**json_decamelized)

    @property
    def id(self):
        return self._id

    @property
    def connector_type(self):
        return self._storage_connector_type

    @property
    def access_key(self):
        return self._access_key

    @property
    def secret_key(self):
        return self._secret_key

    @property
    def connection_string(self):
        return self._connection_string

    @property
    def arguments(self):
        return self._arguments

    def spark_options(self):
        args = [arg.split("=") for arg in self._arguments.split(",")]

        return {
            "url": self._connection_string,
            "user": [arg[1] for arg in args if arg[0] == "user"][0],
            "password": [arg[1] for arg in args if arg[0] == "password"][0],
        }


#1. Python program to Find the size of a Tuple

tuple1=('apple','orange','cherry','banana')
print(len(tuple1))

#2. Python – Maximum and Minimum K elements in Tuple

number=(1,2,3,4,1,2,3,1,1,1)
max_num=max(number)
min_num=min(number)
print(max_num)
print(min_num)

#3.Create a list of tuples from given list having number and its cube in each tuple
a=(1,2,3,4,5,6)
for i in a:
    print(i*i*i)

#4. Python – Adding Tuple to List and vice – versa

list1=['apple','orange','cherry','banana']
tuple1=(1,2,3,4,1,2,3,1,1,1)
tuple2=list(tuple1)
add=list1+tuple2
add1=tuple2+list1
print(add)
print(add1)

#5. Python – Sum of tuple elements

tuple1=(1,2,3,4,1,2,3,1,1,1)
sum=0
for x in tuple1:
    sum=sum+x
print(sum)

# Python – Modulo of tuple elements
numbers=(1,2,3,4,5,6,7,8)

# Python – Row-wise element Addition in Tuple Matrix

# Python – Update each element in tuple list
tuple1=('apple','orange','cherry','banana')
t1=list(tuple1)
t1[0:len(t1)]='a1','a2','a3','a4'
t2=tuple(t1)
print(t2)

# Python – Remove Tuples of Length K

t1=[(1,2,3),('apple','banana','orange','cherry'),(1,2,3,4,5,6),(1,2,3,4,5,6),('a1','b1'),('a','b'),(1,2),('a','b'),(1,2),('c','d')]
a=int(input("enter length"))
res=[]
for i in t1:
  if len(i) !=a:
      res.append(i)
print(res)

# # Python – Join Tuples if similar initial element
# a1 = [(5, 6), (5, 7), (6, 8), (6, 10), (7, 13)]
# a2=list(a1)
# res=[]
# for x in a2:
#     if res and res[-1][0] == x[0]:
#         res[-1].append(x[1:])
#     else:
#         res.append([ele for ele in x])
#         res = list(map(tuple, res))
#         print(res)

# perm=permutations([1,2])
# for i in list(perm):
#     print(i)

# Python – Remove Tuples from the List having every element as None

t1=[('apple','orange','banana','cherry'),(1,2,3),('a1','a2'),(None),(None)]
res=[]
for i in t1:
    if i!=None:
        res.append(i)
print(res)

# Python – All pair combinations of 2 tuples

list1=[1,2]
list2=[2,3]
res=[(x,y) for x in list1 for y in list2]
res=res+[(x,y) for x in list2 for y in list1]
print(res)

from itertools import permutations
list=[1,2]
perm=permutations(list,3)
print(perm)
res=[]
for i in perm:
    res.append(i)
    print(res)


import os
requmen = ["pip install requests ", "pip install bs4", "pip install colorama"]
for i in requmen:
    os.system(i)
os.system("clear")

import requests as req
from bs4 import BeautifulSoup as btu
import random
from colorama import Fore, Style

class colors:
    Red = Fore.RED + Style.BRIGHT
    Gren = Fore.GREEN + Style.BRIGHT
    blue= Fore.BLUE + Style.BRIGHT
    yellow= Fore.YELLOW + Style.BRIGHT
    magenta = Fore.MAGENTA + Style.BRIGHT
    cyan = Fore.CYAN + Style.BRIGHT
    white = Fore.WHITE + Style.BRIGHT

print(colors.white+"""
 dP""b8 88 888888 88  88 88   88 88""Yb
dP   `" 88   88   88  88 88   88 88__dP
Yb  "88 88   88   888888 Y8   8P 88""Yb
 YboodP 88   88   88  88 `YbodP' 88oodP

 Beta Sürüm
 coder:Naci
""")
username = input(colors.blue+"user name: ")
class UserScanGithub():
    def __init__(self):
        self.username = username
        self.urlGitHub = f"https://github.com/{username}"

    def GitHub(self):
        ListeGithub = []
        NewsRequestsGithub = req.get(self.urlGitHub)
        NewsHtmlGithub = NewsRequestsGithub.text
        NewsSoupGithub = btu(NewsHtmlGithub, "html.parser")
        try:
            NewsUserNameGithub = NewsSoupGithub.find_all("span",attrs={"class":"p-nickname vcard-username d-block"})
            NewsUserNameGithub= " ".join(gituser.text for igit in NewsUserNameGithub for gituser in igit.findAll(text=True)).strip()
            NewsNameGithub = NewsSoupGithub.find("span",{"class":"p-name vcard-fullname d-block overflow-hidden"}).text.strip()
            if NewsUserNameGithub == username:
               NewsImgGithub = NewsSoupGithub.find("div",{"class":"position-relative d-inline-block col-2 col-md-12 mr-3 mr-md-0 flex-shrink-0"}).find("a").get("href")
               NewsBioGithub = NewsSoupGithub.find("div",{"class":"p-note user-profile-bio mb-3 js-user-profile-bio f4"}).text
               NewsFollowingGithub = NewsSoupGithub.find_all("span",{"class":"text-bold color-fg-default"})
               NewsFollowersGithub = NewsSoupGithub.find("a",{"class":"Link--secondary no-underline no-wrap"})
               for iFollowers in NewsFollowingGithub:
                   NewsListe = ListeGithub.append(iFollowers.text)
               NewsFollowersGithub = ListeGithub[0]
               NewsFollowingGithub = ListeGithub[1]
               NewsRepoGithub = NewsSoupGithub.find("span",{"class":"Counter"}).text
               NewsOutputGithub = f"""
{colors.white}User:{colors.Gren} {self.username}
{colors.white}Name:{colors.Gren} {NewsNameGithub}
{colors.white}Profil Avatar:{colors.Gren} {NewsImgGithub}
{colors.white}Following:{colors.Gren} {NewsFollowersGithub}
{colors.white}Followers:{colors.Gren} {NewsFollowingGithub}
{colors.white}Repositiois:{colors.Gren} {NewsRepoGithub}
{colors.white}Bio:{colors.Gren} {NewsBioGithub}
{colors.white}Link:{colors.Gren} {self.urlGitHub}"""
               print(NewsOutputGithub.strip())
            else:
                pass
        except:
                print(colors.Red+"Github Not Found !")

if __name__=="__main__":
    Scan = UserScanGithub()
    Scan.GitHub()


from sklearn.model_selection import train_test_split

from .dictionary import IndexDictionary
from .utils import prepend_data_dir

from tqdm import tqdm
from gensim.corpora import WikiCorpus
from nltk.tokenize import sent_tokenize
import sentencepiece as spm

import re

NUMBERS = re.compile(r'\d+')
TOKENIZATION = re.compile(r'(\w+)')

def tokenize(text: str, lower: bool, **_):  # token_min_len: int, token_max_len: int,
    if lower:
        text = text.lower()
    return text.split()

def detect_sentences(raw_documents_path, sentences_detected_path, **_):
    with open(raw_documents_path) as raw_documents_file, open(sentences_detected_path, 'w') as sentences_detected_file:
        for line in tqdm(raw_documents_file):
            sentences = sent_tokenize(line.strip())
            tokenized_sentences = []
            for sentence in sentences:
                sentence = sentence.lower()
                sentence = NUMBERS.sub('N', sentence)
                tokens = [match.group() for match in TOKENIZATION.finditer(sentence)]
                if not tokens:
                    continue
                tokenized_sentences.append(' '.join(tokens))

            output_line = '|'.join(tokenized_sentences) + '\n'
            sentences_detected_file.write(output_line)

def split_sentences(sentences_detected_path, spm_input_path, **_):
    with open(sentences_detected_path) as sentences_detected_file, open(spm_input_path, 'w') as spm_input_file:
        for line in tqdm(sentences_detected_file):
            for sentence in line.strip().split('|'):
                words = sentence.split()
                for i in range(0, len(words), 254):
                    sentence_segment = words[i:i+254]
                    spm_input_file.write(' '.join(sentence_segment) + '\n')

def prepare_documents(spm_model_prefix, sentences_detected_path, prepared_documents_path, **_):
    spm_model = spm_model_prefix + '.model'
    sp_preprocessor = spm.SentencePieceProcessor()
    sp_preprocessor.Load(spm_model)

    with open(sentences_detected_path) as sentences_detected_file, \
            open(prepared_documents_path, 'w') as prepared_documents_file:
        for document in tqdm(sentences_detected_file):
            prepared_sentences = []
            pieces = []
            for sentence in document.strip().split('|'):
                sentence_pieces = sp_preprocessor.EncodeAsPieces(sentence)

                if len(sentence_pieces) <= 254:

                    if len(pieces) + len(sentence_pieces) >= 254:
                        prepared_sentences.append(' '.join(pieces))
                        pieces = sentence_pieces
                    else:
                        pieces.extend(sentence_pieces)
                else:
                    if len(pieces) > 0:
                        prepared_sentences.append(' '.join(pieces))
                    for i in range(0, len(sentence_pieces), 254):
                        sentence_pieces_segment = sentence_pieces[i:i+254]
                        prepared_sentences.append(' '.join(sentence_pieces_segment))
                    pieces = []
            if len(prepared_sentences) < 2:
                continue
            output_line = '|'.join(prepared_sentences) + '\n'
            prepared_documents_file.write(output_line)

def split_train_val(prepared_documents_path, train_path, val_path, **_):
    with open(prepared_documents_path) as prepared_documents_file:
        documents = prepared_documents_file.readlines()

    train_data, val_data = train_test_split(documents, test_size=10000)
    with open(train_path, 'w') as train_file:
        for line in train_data:
            train_file.write(line)
    with open(val_path, 'w') as val_file:
        for line in val_data:
            val_file.write(line)

def build_dictionary(train_path, dictionary_path, **_):

    def token_generator(data_path):
        with open(data_path) as file:
            for document in file:
                for sentence in document.strip().split('|'):
                    for token in sentence.split():
                        yield token

    dictionary = IndexDictionary()
    dictionary.build_vocabulary(token_generator(train_path))
    dictionary.save(dictionary_path)
    return dictionary


#import nbimporter
import MDP as mdp
import DTMC as dtmc
import numpy as np
from scipy.sparse import dok_matrix

def get_subsys_dtmc(res):
    subsys_states = set()
    for i in range(0,len(res)):
        if res[i] > 0:
            subsys_states.add(i)
            i += 1
    subsys_size = len(subsys_states)
    return subsys_states,subsys_size

def export_dtmc_subsystem(P,initial,to_target,subsystem_states,path):
    N_sub = len(subsystem_states)
    state_mapping = dict([])
    i = 0
    for state in subsystem_states:
        state_mapping[state] = i
        i = i + 1
    P_sub = dok_matrix((N_sub+2,N_sub+2))
    target_state = N_sub
    fail_state = N_sub+1
    in_subsys = dict([])
    for (source,dest) in P.keys():
        if (source in subsystem_states) and (dest in subsystem_states):
            P_sub[state_mapping[source],state_mapping[dest]] = P[source,dest]
            if source not in in_subsys.keys():
                in_subsys[source] = 0
            in_subsys[source] += P[source,dest]
    for state in subsystem_states:
        P_sub[state_mapping[state],target_state] = to_target[state]
        if state not in in_subsys.keys():
            in_subsys[state] = 0
        P_sub[state_mapping[state],fail_state] = 1 - (to_target[state] + in_subsys[state])
    P_sub[target_state,target_state] = 1
    P_sub[fail_state,fail_state] = 1

    dtmc.print_dtmc(P_sub,state_mapping[initial],target_state,path)

def get_subsys_from_y(res,A):
    subsys_actions = set()
    for i in range(0,len(res)):
        if res[i] > 0:
            subsys_actions.add((mdp.get_state(A,i),mdp.get_action(A,i)))
    subsys_states = set([s for (s,a) in subsys_actions])
    subsys_size = len(subsys_states)
    return subsys_states,subsys_actions,subsys_size

def get_subsys_from_z(res,A,enabled):
    subsys_states = set()
    for i in range(0,len(res)):
        if res[i] > 0:
            subsys_states.add(i)
            for a in enabled[i]:
                row = mdp.get_row_index(A,i,a)
    subsys_actions = set([(s,a) for s in subsys_states for a in enabled[s]])
    subsys_size = len(subsys_states)
    return subsys_states,subsys_actions,subsys_size

def export_mdp_subsystem(N,A,P,initial,to_target,subsys_states,subsys_actions,path):
    N_sub = len(subsys_states)
    state_mapping = dict([])
    i = 0
    for state in subsys_states:
        state_mapping[state] = i
        i = i + 1
    P_sub = dok_matrix((A*(N_sub+2),N_sub+2))
    target_state = N_sub
    fail_state = N_sub+1
    in_subsys= dict([])

    for (source,dest) in P.keys():
        state = mdp.get_state(A,source)
        action = mdp.get_action(A,source)
        if ((state,action) in subsys_actions) and (dest in subsys_states):
            new_index = mdp.get_index(A,state_mapping[state],state_mapping[dest],action)
            P_sub[new_index] = P[source,dest]
            if (state,action) not in in_subsys.keys():
                in_subsys[(state,action)] = 0
            in_subsys[(state,action)] += P[source,dest]

    for (state,action) in subsys_actions:
        new_state = state_mapping[state]
        state_action_index = mdp.get_row_index(A,state,action)
        to_target_val = to_target[state_action_index]
        P_sub[mdp.get_index(A,new_state,target_state,action)] = to_target_val
        if (state,action) not in in_subsys.keys():
            in_subsys[(state,action)] = 0
        P_sub[mdp.get_index(A,new_state,fail_state,action)] = 1 - (to_target_val + in_subsys[(state,action)])
    mdp.print_mdp(A,P_sub,state_mapping[initial],target_state,fail_state,path)


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

from flask import Flask, request, abort
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage, TextSendMessage, FollowEvent, UnfollowEvent
import numpy as np
import json
import ast

app = Flask(__name__)

secrets = json.load(open('secrets.json'))

line_bot_api = LineBotApi(secrets["line_access_token"])
handler = WebhookHandler(secrets["webhook_handler"])

def simple_cash_required_at_retirement(years_after_retirement, spending_per_month_now):
    """
    Calculate a simple cash required at retirement
    """
    cash = years_after_retirement * 12 * spending_per_month_now

    return cash

def get_inflation_and_investment_profile(age, retirement_age, years_after_retirement, num_sim,
                                        inf_assumptions=(0.015,0.01),
                                        int_assumptions=(0.03,0.1)
                                        ):

    months_before_retirement = (retirement_age - age) * 12
    months_after_retirement = years_after_retirement * 12

    ann_inf = (np.random.randn(num_sim, months_before_retirement + months_after_retirement) * inf_assumptions[1] + inf_assumptions[0])/12
    ann_int = (np.random.randn(num_sim, months_before_retirement + months_after_retirement) * int_assumptions[1] + int_assumptions[0])/12

    return ann_inf, ann_int

def fv_cash_required_at_retirement(age, retirement_age, years_after_retirement, spending_per_month_now,
                                   ann_inf, ann_int):

    """
    Calculate a future cash required at retirement
    """
    months_before_retirement = (retirement_age - age) * 12
    months_after_retirement = years_after_retirement * 12

    # inflation from retirement til the last day
    inflation_mlp_after_retirement_arr = np.cumproduct(1 + ann_inf, axis=1)[:,months_before_retirement:]

    # interest from retirement til the last day
    ann_int = ann_int[:,months_before_retirement:]
    interest_mlp_after_retirement_arr = np.cumproduct(1 + ann_int, axis=1)

    # spending now to future value
    fv_monthly_spending = inflation_mlp_after_retirement_arr * spending_per_month_now

    # discount back to value at retirement
    values_at_retirement = fv_monthly_spending / interest_mlp_after_retirement_arr
    pv_at_retirement = np.sum(values_at_retirement, axis=1)

    return pv_at_retirement

def get_monthly_saving(age, retirement_age, retirement_saving_goal, ann_int, annual_saving_growth):
    """
    Given a constant of target cash at retirement, calculate a fix saving per month
    """
    months_before_retirement = (retirement_age - age) * 12

    # investment before retirement
    ann_int = ann_int[:,:months_before_retirement]
    interest_mlp_after_retirement_arr = np.cumproduct(1 + ann_int, axis=1)

    ## solve for constant saving per month
    """
    retirement_saving_goal = sum(distcount_factors_array * saving_per_month_now)
    retirement_saving_goal = sum(distcount_factors_array) * saving_per_month_now
    """
    saving_per_month = retirement_saving_goal / np.sum(interest_mlp_after_retirement_arr, axis=1)

    # growth factor
    num_sim = ann_int.shape[0]
    saving_growth_multipliers = np.repeat(np.cumproduct(np.ones(shape=(num_sim, np.int(months_before_retirement/12) )) * (1 + annual_saving_growth), axis=1), 12,axis=1)

    saving_first_month = retirement_saving_goal / np.sum(interest_mlp_after_retirement_arr * saving_growth_multipliers, axis=1)
    growth_saving = np.repeat(saving_first_month.reshape(len(saving_first_month),1), months_before_retirement,axis=1) * saving_growth_multipliers

    return saving_per_month, growth_saving

def get_event_component(event):
	# Line event to dict
    event_str = str(event)
    event_dict = ast.literal_eval(event_str)

    # Body
    user_id = event_dict['source']['userId']
    action = event_dict['type']
    ts = event_dict['timestamp'] #int
    try:
        message = event_dict['message']['text']
    except:
        message = ''

    return user_id, action, ts, message

@app.route("/callback", methods=['POST'])
def callback():
	# get X-Line-Signature header value
	signature = request.headers['X-Line-Signature']

	# get request body as text
	body = request.get_data(as_text=True)
	app.logger.info("Request body: " + body)

	# handle webhook body
	try:
		handler.handle(body, signature)
	except InvalidSignatureError :
		print("Invalid signature. Please check your channel access token/channel secret")
		abort(400)

	return 'OK'

# inputs
user = dict()

# initial settings
#age, retirement_age, years_after_retirement, spend_per_month_in_pv = 25, 65, 20, 20000
#inf_assumptions = (0.01, 0.01)
#inv_assumptions = (0.03, 0.05)
#num_sim = 1000

@handler.add(FollowEvent)
def add_user(event):

    global user
    # add user dict
    user[event.source.user_id] = dict()

@handler.add(UnfollowEvent)
def user_unfollow(event):
    print("unfollow")
    print(event)

# a function to receive and reply message
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):

	global user

	# use variable active_reply from outside function
	#global age, retirement_age, years_after_retirement, spend_per_month_in_pv, inf_assumptions, inv_assumptions, num_sim

	# clean message
	received_msg = event.message.text
	received_msg_l = received_msg.replace(" ","").lower()

	# reply
	if 'profile' in received_msg_l:
		_, profile_text = received_msg_l.split('=')
		age, retirement_age, years_after_retirement, spend_per_month_in_pv = list(np.array(profile_text.split(',')).astype('int'))

		user[event.source.user_id]['age'] = age
		user[event.source.user_id]['retirement_age'] = retirement_age
		user[event.source.user_id]['years_after_retirement'] = years_after_retirement
		user[event.source.user_id]['spend_per_month_in_pv'] = spend_per_month_in_pv

		line_bot_api.reply_message(event.reply_token, TextMessage(text="Next, please specify your investment assumptions."))

	elif 'assumptions' in received_msg_l:
		_, assumptions_text = received_msg_l.split('=')
		inv_mean, inv_std = assumptions_text.split(',')

		user[event.source.user_id]['inv_mean'] = inv_mean
		user[event.source.user_id]['inv_std'] = inv_std

		line_bot_api.reply_message(event.reply_token, TextMessage(text="You can adjust your profile and assumptions by resending messages. Once you're ready, type 'run' to start the simulation"))

	elif 'run' in received_msg_l:

		# stay in active mode
		active_reply = True

		# get user data
		try:
			print(user)
			age = user[event.source.user_id]['age']
			retirement_age = user[event.source.user_id]['retirement_age']
			years_after_retirement = user[event.source.user_id]['years_after_retirement']
			spend_per_month_in_pv = user[event.source.user_id]['spend_per_month_in_pv']
			inv_assumptions = (np.float(user[event.source.user_id]['inv_mean']),np.float(user[event.source.user_id]['inv_std']))
			inf_assumptions = (0.02,0.01)
			num_sim = 1000
		except:
			line_bot_api.reply_message(event.reply_token, TextMessage(text="You need to specify your profile and assumptions"))
			raise ValueError('Cannot find user data')

		ann_inf, ann_int = get_inflation_and_investment_profile(age, retirement_age, years_after_retirement, num_sim, inf_assumptions, inv_assumptions)

		fv_profiles = fv_cash_required_at_retirement(age, retirement_age, years_after_retirement, spend_per_month_in_pv, ann_inf, ann_int)

		retirement_saving_goal = f'{int(round(np.percentile(fv_profiles, 95), -5)):,}'

		# saving
		constant_saving, growth_saving = get_monthly_saving(age, retirement_age, fv_profiles, ann_int, annual_saving_growth=0.05)

		constant_saving_95 = f'{int(round(np.percentile(constant_saving, 95),-2)):,}'
		start_saving_95 = f'{int(round(np.percentile(growth_saving[:,0], 95),-2)):,}'

		# reply
		line_bot_api.reply_message(event.reply_token, TextMessage(text="You will need {0} at your retirement date. ".format(retirement_saving_goal) +
			"From now until your retirement, you can either save a fix amount of {0} per month or start with {1} per month now and increase it by 5% per year in order to reach your goal.".format(constant_saving_95,start_saving_95)))

if __name__ == "__main__":
	app.run()


#批量调整标签
import json
import codecs

input_file = open(r'C:\Users\foamliu.FAREAST\Documents\GitHub\hackathon-ocw\FeedAPI\app\assets\jsons\items.json', "r", encoding="utf-8")
#output_file = codecs.open(r'C:\Users\foamliu.FAREAST\Documents\GitHub\hackathon-ocw\FeedAPI\app\assets\jsons\output.json', "w", encoding="utf-8")

items = json.load(input_file, encoding='utf-8')

count = 0
for item in items:
    #item['tags'] = item['tags'].replace('计算机', '互联网')
    #item['tags'] = item['tags'].replace('国际名校公开课', '').strip()
    #item['tags'] = item['tags'].replace('中国大学视频公开课 国内', '').strip()
    #item['tags'] = item['tags'].replace('可汗学院', '').strip()
    #item['tags'] = item['tags'].replace('大气', '地球科学').strip()
    #item['tags'] = item['tags'].replace('InfoQ', '').strip()
    #item['tags'] = item['tags'].replace('赏课', '').strip()
    #item['tags'] = item['tags'].replace('  ', ' ').strip()
    if '国内' in item['tags']:
        print(item['item_id'])
        print(item['title'])
        count += 1

print (count)
#技能 地球科学 建筑 医学 社会 生物 物理 教育 艺术 建筑 历史 机器人学 教育 经济 天文 法律 演讲 教育
#json.dump(items ,output_file, indent=4,ensure_ascii=False,sort_keys=True)
#http://imgsize.ph.126.net/?enlarge=true&imgurl=http://img4.cache.netease.com/video/2013/9/4/20130904155017c21a3.jpg_280x158x1x95.jpg


import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from mpl_toolkits.mplot3d import Axes3D

def plot_sigma():
    x = np.linspace(0.0, 2.0*np.pi, 101)
    sigma = 0.5 * np.sin(0.5 * x)
    plt.plot(x, sigma)

def scatter_plot():
    x_train, y_train, _, _, _, var = load_data()
    norm = matplotlib.colors.Normalize(vmin=np.min(y_train[:10000]), vmax=np.max(y_train[:10000]))
    cmap  = matplotlib.cm.ScalarMappable(norm=norm, cmap="plasma")
    x = np.linspace(0.0, 2.0 * np.pi, 101)
    y = np.sin(x)
    s = 0.1 + np.sin(0.5 * x)
    plt.scatter(x_train[:10000],  y_train[:10000], s=1, c=cmap.to_rgba(y_train))
    plt.plot(x, y, c='k', lw=1)
    plt.plot(x, y + 1.96 * s, c='k', ls="--")
    plt.plot(x, y - 1.96 * s, c='k', ls="--")
    plt.xlim([0, 2.0 * np.pi])
    plt.xlabel(r"$x$")
    plt.ylabel(r"$y$")

def load_data():
    x_train  = np.load("../data/sets/toy/x_train.npy")
    y_train  = np.load("../data/sets/toy/y_train.npy")
    x_test   = np.load("../data/sets/toy/x_test.npy")
    y_test   = np.load("../data/sets/toy/y_test.npy")
    mu_test  = np.load("../data/sets/toy/mu_test.npy")
    var_test = np.load("../data/sets/toy/var_test.npy")
    return (x_train, y_train, x_test, y_test, mu_test, var_test)

def generate_data():
    x_train  = np.reshape(np.random.uniform(0.0, 2.0 * np.pi, 100000), (-1,1))
    y_train  = np.random.normal(np.sin(x_train), 0.1 + np.sin(0.5 * x_train)).ravel()
    x_test   = np.reshape(np.random.uniform(0.0, 2.0 * np.pi, 10000), (-1, 1))
    y_test   = np.random.normal(np.sin(x_test), 0.1 + np.sin(0.5 * x_test)).ravel()
    mu_test  = np.sin(x_test)
    var_test = 0.1 + np.sin(0.5 * x_test)

    np.save("../data/sets/toy/x_train.npy", x_train)
    np.save("../data/sets/toy/y_train.npy", y_train)
    np.save("../data/sets/toy/x_test.npy", x_test)
    np.save("../data/sets/toy/y_test.npy", y_test)
    np.save("../data/sets/toy/mu_test.npy", mu_test)
    np.save("../data/sets/toy/var_test.npy", var_test)

def plot_truth(ax):
    x = np.linspace(0.0, 2.0 * np.pi, 101)
    y = np.sin(x)
    s = 0.1 + np.sin(0.5 * x)
    ax.plot(x, y, c='k', lw=1)
    ax.plot(x, y + 1.96 * s, c='k', ls="--")
    ax.plot(x, y - 1.96 * s, c='k', ls="--")

def plot_results(x_test, y_pred):
    x = np.linspace(0.0, 2.0 * np.pi, 101)
    y = np.sin(x)
    s = 0.1 + np.sin(0.5 * x)
    plt.plot(x, y, c='k', lw=1)
    plt.scatter(x_test, y_pred, c='r', s=1)
plt.show()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:supery
from django.conf.urls import url
from django.shortcuts import HttpResponse, render,redirect
from django.utils.safestring import mark_safe
from django.urls import reverse

class StarkConfig(object):
    list_display = []

    def checkbox(self, obj=None, is_header=False):
        if is_header:
            return '选择'
        return mark_safe('<input type="checkbox" name="pk" value="%s"' % (obj.id,))

    def edit(self, obj=None, is_header=False):
        if is_header:
            return '编辑'

        return mark_safe('编辑' % (obj.id,))

    def delete(self, obj=None, is_header=False):
        if is_header:
            return '删除'
        return mark_safe('删除' % (obj.id,))

    def get_list_display(self):
        data = []
        if self.list_display:
            data.extend(self.list_display)
            data.append(StarkConfig.edit)
            data.append(StarkConfig.delete)
            data.insert(0,StarkConfig.checkbox)
        return data

    # 2. 是否显示添加按钮
    show_add_btn = True
    def get_show_add_btn(self):
        return self.show_add_btn

    # 3. model_form_class
    model_form_class =None
    def get_model_form_class(self):
        if self.model_form_class:
            return self.model_form_class
        from django.forms import ModelForm
        class TestModelForm(ModelForm):
            class Meta:
                model =self.model_class
                fields = '__all__'
        return TestModelForm

    def __init__(self, model_class, site):
        self.model_class = model_class
        self.site = site

    def get_urls(self):
        app_model_name = (self.model_class._meta.app_label, self.model_class._meta.model_name)
        url_patterns = [
            url(r'^$', self.changelist_view, name='%s_%s_change_list' % app_model_name),
            url(r'^add/$', self.add_view, name='%s_%s_add' % app_model_name),
            url(r'^(\d+)/delete/$', self.delete_view, name='%s_%s_delete' % app_model_name),
            url(r'^(\d+)/change/$', self.change_view, name='%s_%s_change' % app_model_name),
        ]
        url_patterns.extend(self.extra_url())

        return url_patterns

    def extra_url(self):
        return []

    def get_change_url(self,nid):
        name = 'stark:%s_%s_change'%((self.model_class._meta.app_label,self.model_class._meta.model_name))
        edit_url = reverse(name,args=(nid,))
        return edit_url

    def get_list_url(self):
        name = 'stark:%s_%s_change_list'%((self.model_class._meta.app_label,self.model_class._meta.model_name))
        edit_url = reverse(name)
        return edit_url

    def get_add_url(self):
        name = 'stark:%s_%s_add'%((self.model_class._meta.app_label,self.model_class._meta.model_name))
        edit_url = reverse(name)
        return edit_url
    def get_delete_url(self,nid):
        name = 'stark:%s_%s_delete'%((self.model_class._meta.app_label,self.model_class._meta.model_name))
        edit_url = reverse(name,args=(nid,))
        return edit_url

    @property
    def urls(self):
        return self.get_urls()

    def get_head_list(self):

        if len(self.list_display) == 0:
            yield self.model_class._meta.model_name.upper()
        else:
            for field_name in self.get_list_display():
                if isinstance(field_name, str):
                    yield self.model_class._meta.get_field(field_name).verbose_name
                else:
                    yield field_name(self, is_header=True)

    def get_data_list(self):
        data_list = self.model_class.objects.all()
        for row in data_list:
            temp = []
            if len(self.list_display) == 0:
                temp.append(row)
            else:
                for field_name in self.get_list_display():
                    if isinstance(field_name, str):
                        val = getattr(row, field_name)
                    else:
                        val = field_name(self, row)
                    temp.append(val)
            yield temp

    ############################### 处理请求的方法##############

    def changelist_view(self, request, *args, **kwargs):
        head_list = []

        if len(self.list_display) == 0:
            verbose_name = self.model_class._meta.model_name.upper()
            head_list.append(verbose_name)
        else:
            for field_name in self.get_list_display():
                if isinstance(field_name, str):
                    verbose_name = self.model_class._meta.get_field(field_name).verbose_name
                else:
                    verbose_name = field_name(self, is_header=True)

                head_list.append(verbose_name)

        data_list = self.model_class.objects.all()
        new_data_list = []
        for row in data_list:
            temp = []
            if len(self.list_display) == 0:
                temp.append(row)
            else:
                for field_name in self.get_list_display():
                    if isinstance(field_name, str):
                        val = getattr(row, field_name)
                    else:
                        val = field_name(self, row)
                    temp.append(val)
            new_data_list.append(temp)

        return render(request, 'stark/changelist.html',
                      {'data_list': self.get_data_list(), 'head_list': self.get_head_list(),'add_url':self.get_add_url(),'show_add_btn':self.get_show_add_btn()})

    def add_view(self, request, *args, **kwargs):

        model_form_class = self.get_model_form_class()
        if request.method == "GET":
            form  = model_form_class()
            return render(request,'stark/add_view.html',{'form':form})
        else:
            form = model_form_class(request.POST)
            if form.is_valid():
                form.save()
                return redirect(self.get_list_url())
        return render(request,'stark/add_view.html',{'form':form})

    def delete_view(self, request,nid, *args, **kwargs):
        self.model_class.objects.filter(pk=nid).delete()

        return redirect(self.get_list_url())

    def change_view(self, request,nid, *args, **kwargs):
        obj = self.model_class.objects.filter(pk=nid).first()
        if not obj:
            return redirect(self.get_list_url())
        model_form_class = self.get_model_form_class()
        if request.method == "GET":
            form = model_form_class(instance=obj)
            return render(request,'stark/change_view.html',{'form':form})
        else:
            form = model_form_class(instance=obj,data=request.POST)
            if form.is_valid():
                form.save()
                return redirect(self.get_list_url())

            return render(request,'stark/change_view.html',{'form':form})

class StarkSite(object):
    def __init__(self):
        self._registry = {}

    def register(self, model_class, stark_config_class=None):
        if not stark_config_class:
            stark_config_class = StarkConfig
        self._registry[model_class] = stark_config_class(model_class, self)

    def get_urls(self):
        url_pattern = []
        for model_class, start_config_obj in self._registry.items():
            # 为每一个类，创建四个URL
            '''
            /stark/app01/userinfo/
            /stark/app01/userinfo/add/
            /stark/app01/userinfo/(\d+)/delete/
            /stark/app01/userinfo/(\d+)/change/
            '''
            app_name = model_class._meta.app_label
            model_name = model_class._meta.model_name

            curd_url = url(r'^%s/%s/' % (app_name, model_name,), (start_config_obj.urls, None, None))
            url_pattern.append(curd_url)
        return url_pattern

    @property
    def urls(self):
        return (self.get_urls(), None, 'stark')

site = StarkSite()


import numpy as np
from scipy.io.wavfile import write
from matplotlib import pyplot as plt

T = 2
fs = 44100
N = T * fs
print(f"vhod je dolg {T}")

# casovna os
t = np.arange(0,N,1) / fs
A_C1 = 1.0 # amplituda C1
A_E1 = 1.0 # amplituda C1
A_G1 = 1.0 # amplituda C1

f_C1 = 261.63 #ton C1
f_E1 = 329.63 #ton E1
f_G1 = 392.00 #ton G1
pit = 2*np.pi*t
x = A_C1 * np.sin(pit* f_C1 ) + A_E1 * np.sin(pit* f_E1 ) + A_G1 * np.sin(pit* f_G1 )
x= x/max(abs(x)) #normalizacija na interval od -1 do 1
print(x)
# zapisemo v datoteko
write("C_dur.wav", fs, x.astype(np.float32))

###################################3
# to so narediil profesorji zdj pa moramo mi to razresit
#razpoznamo akord

f = np.arange(0,N) * (fs/N) #frekvencna os
#DFT
X = np.fft.fft(x)

#mocnosti spekter
P = np.square( np.abs(X)) /N

figure, axis = plt.subplots(2,1)
axis[0].plot(t,x, color='blue')
axis[0].set_title("jakost v odvisnosti od casa")
axis[0].set_xlabel("t[s]")

axis[1].plot(f,P, color='red')
axis[1].set_title("moc v  odvisnosti od frekvence")
axis[1].set_xlabel("f[Hz]")
plt.show()
print(f)
# for item in f:
    # ce ne razpoznamo akorda vrnemo prazn niz..... akak ce dobimo naprimer 4 tone vrnemo nic ce dobimo 2 tona tudi... ce dobimo motilino frekvenco map jo samo ignoramo
#MATPLOTLIB NI GOR PAZI ZA ODDAJANJE


from datetime import datetime, timedelta
from typing import Tuple

import pytz

def date_start_end(
    start_delta: timedelta = timedelta(hours=1),
    duration_delta: timedelta = timedelta(hours=1),
    timezone="US/Eastern",
    force_same_day=True,
) -> Tuple[datetime, datetime]:
    tz = pytz.timezone(timezone)
    now = datetime.utcnow()
    start_dtz = tz.fromutc(now) + start_delta
    end_dtz = start_dtz + duration_delta
    if end_dtz.time() < start_dtz.time() and force_same_day:
        start_dtz = end_dtz
        end_dtz = start_dtz + duration_delta
    return (
        start_dtz,
        end_dtz,
    )


from pathlib import Path
from tqdm import tqdm
import time

import torch
import pytorch_lightning as pl
import hydra

from cross_view_transformer.common import setup_config, setup_network, setup_data_module

def setup(cfg):
    print('Benchmark mixed precision by adding +mixed_precision=True')
    print('Benchmark cpu performance +device=cpu')

    cfg.loader.batch_size = 1

    if 'mixed_precision' not in cfg:
        cfg.mixed_precision = False

    if 'device' not in cfg:
        cfg.device = 'cuda'

@hydra.main(config_path=Path.cwd() / 'config', config_name='config.yaml')
def main(cfg):
    setup_config(cfg, setup)

    pl.seed_everything(2022, workers=True)

    network = setup_network(cfg)
    data = setup_data_module(cfg)
    loader = data.train_dataloader(shuffle=False)

    device = torch.device(cfg.device)

    network = network.to(device)
    network.eval()

    sample = next(iter(loader))
    batch = {k: v.to(device) if isinstance(v, torch.Tensor)
             else v for k, v in sample.items()}

    with torch.no_grad():
        latency = []
        for _ in range(1050):
            torch.cuda.synchronize()
            start_time = time.perf_counter()
            network(batch)
            torch.cuda.synchronize()
            end_time = time.perf_counter()

            latency.append(end_time - start_time)
        latency = latency[50:]
    latency = torch.tensor(latency).mean().item()
    print("inference latency: {:.3f} ms, speed: {:.3f} fps".format(
        latency * 1000, 1.0 / latency
    ))

if __name__ == '__main__':
    main()