"""
AOC day 13 2018
"""
from collections import defaultdict
from lib.filehelper import file_to_str_array
# pylint: disable=missing-module-docstring

class Board():
    def __init__(self,inputfile):
        self.inputfile=inputfile
        self.board=defaultdict(lambda: '.')
        self.processinputfile()

    def processinputfile(self):
        self.input=file_to_str_array(self.inputfile)
        self.dotlocs=[]
        self.folds=[]
        self.maxx=0
        self.maxy=0
        for line in self.input:
            if ',' in line:
                coords = line.split(',')
                x=int(coords[0])
                y=int(coords[1])
                self.maxx=max(self.maxx,x)
                self.maxy=max(self.maxy,y)
                self.board[(x,y)]='#'

            elif len(line) == 0:
                pass
            elif  line.startswith('fold along'):
                self.folds.append(line.replace('fold along ',''))
        return None

    def fold(self,foldinst):
        direction,value = foldinst.split('=')
        value=int(value)
        if direction == 'y':
            for x in range(self.maxx+1):
                self.board[(x,value)]='-'
            for y in range(0,value):
                for x in range(0,self.maxx+1):
                    if self.board[(x,y)] == '.':
                        self.board[(x,y)]= self.board[(x,2*value-y)]
            self.maxy=value-1

        elif direction == 'x':
            for y in range(self.maxy+1):
                self.board[(value,y)]='|'
            for x in range(0,value):
                for y in range(0,self.maxy+1):
                    if self.board[(x,y)] == '.':
                        self.board[(x,y)]= self.board[(2*value-x,y)]
            self.maxx=value-1

    def part2(self):
        for fold in self.folds:
            self.fold(fold)

    def part1(self):
        self.fold(self.folds[0])
        count=0
        for x in range(self.maxx+1):
            for y in range(self.maxy+1):
                if self.board[(x,y)]=='#':
                    count+=1
        return count

    def __repr__(self):
        output=''
        for y in range(self.maxy+1):
            thisy=''
            for x in range(self.maxx+1):
                thisy+=self.board[(x,y)]
            output+=thisy+'\n'
        return output

def day13_01():
    """Run part 1 of Day 13's code"""
    inputfile='input/day13.txt'
    testboard=Board(inputfile)
    result=testboard.part1()
    print(f'1301: {result} Hashes after the first fold')

def day13_02():
    """Run part 2 of Day 13's code"""
    inputfile='input/day13.txt'
    testboard=Board(inputfile)
    testboard.part2()
    result='CAFJHZCK'
    print(f'1302: {result} Is the activation key.')

if __name__ == "__main__":
    day13_01()
    day13_02()


# -*- coding: utf-8 -*-
"""
"""
from setuptools import setup, find_packages

with open('requirements.txt') as fd:
    requirements = fd.readlines()

with open('test-requirements.txt') as fd:
    test_requirements = fd.readlines()

setup(
    name='remarkable-friend',
    packages=find_packages(),
    setup_requires=[],
    install_requires=requirements,
    entry_points={
        'console_scripts': 'rmfriend=rmfriend.tools.manage:main'
    },
)


import json
import logging
from collections import defaultdict
from dataclasses import dataclass
from typing import Dict, List

import pandas as pd
import typer

logger = logging.getLogger(__name__)

@dataclass
class ResfinderResult:
    sample_name: str
    genes: List[str]
    amr_resistant: List[str]

def process_args_table(
    sample_results: ResfinderResult,
    table: Dict[str, List[int]],
    n_processed_samples: int,
):
    args_to_add = set(["_".join(arg.split("_")[:1]) for arg in sample_results.genes])
    logger.info(
        f"Adding {len(args_to_add)} ARGs corresponding to sample {sample_results.sample_name}"
    )
    # Add the sample values to the table
    for arg in table:
        if arg in args_to_add:
            table[arg].append(1)
            args_to_add.remove(arg)
        else:
            table[arg].append(0)
    # Add new ARG if it was not present in the previous samples
    for arg in args_to_add:
        table[arg] = [0] * (n_processed_samples - 1) + [1]

def main(
    results_files: List[str] = typer.Argument(
        ..., help="List of path to the parsed results from ResFinder"
    ),
    output: str = typer.Option(..., help="Filename of the final CSV file"),
):
    logger.info(f"Reading {len(results_files)} results files...")
    # Read and process all the results files
    samples_list = []
    args_table = defaultdict(list)
    for res_filepath in results_files:
        with open(res_filepath, "r") as json_file:
            sample_results = ResfinderResult(**json.load(json_file))
        samples_list.append(sample_results.sample_name)
        process_args_table(sample_results, args_table, len(samples_list))
    # Create dataframe
    logger.info(f"Creating dataframe with total of {len(args_table)} ARGs...")
    args_table = pd.DataFrame.from_records(args_table, index=samples_list)
    args_table.to_csv(output, index_label="sample_name")
    logger.info(f"Results saved to {output} successfully.")

if __name__ == "__main__":
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s [%(levelname)s][%(name)s] -- %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
    )
    typer.run(main)


import math
import torch
import torch.nn.functional as F
import torch_scatter
from torch_geometric.nn import GCNConv
from torch_geometric.nn import MessagePassing
#from torch_geometric.nn import GatedGraphConv

class GCNResidualBlock(torch.nn.Module):
    def __init__(self, hidden_size):
        super().__init__()
        self.gcn_conv = GCNConv(hidden_size, hidden_size) # (hidden, num_out_features_per_node)
        self.linear = torch.nn.Linear(hidden_size, hidden_size)

    def forward(self, x, edge_index):
        x_block = self.gcn_conv(x, edge_index)
        x_block = F.relu(x_block)
        x_block = self.linear(x_block)
        x_block = F.relu(x_block)
        return x_block+x

# Residual connection
class GCN(torch.nn.Module):
    def __init__(self):
        super().__init__()
        hidden_size = 1280
        p = 0.4
        self.stem = torch.nn.Sequential(
            torch.nn.Linear(10, hidden_size),
            torch.nn.ReLU(),
        )

        self.block1 = GCNResidualBlock(hidden_size)
        self.bn1 = torch.nn.BatchNorm1d(hidden_size)
        self.do1 = torch.nn.Dropout(p)

        self.block2 = GCNResidualBlock(hidden_size)
        self.bn2 = torch.nn.BatchNorm1d(hidden_size)
        self.do2 = torch.nn.Dropout(p)

        self.block3 = GCNResidualBlock(hidden_size)
        self.bn3 = torch.nn.BatchNorm1d(hidden_size)
        self.do3 = torch.nn.Dropout(p)

        self.block4 = GCNResidualBlock(hidden_size)
        self.bn4 = torch.nn.BatchNorm1d(hidden_size)
        self.do4 = torch.nn.Dropout(p)

        self.block5 = GCNResidualBlock(hidden_size)
        self.bn5 = torch.nn.BatchNorm1d(hidden_size)
        self.do5 = torch.nn.Dropout(p)

        self.out = torch.nn.Sequential(
            torch.nn.Linear(hidden_size, 7)
        )

    def forward(self, data):
        x, edge_index = data.x, data.edge_index
        x = self.stem(x)
        x = self.block1(x, edge_index)
        x = self.bn1(x)
        x = self.do1(x)

        x = self.block2(x, edge_index)
        x = self.bn2(x)
        x = self.do2(x)

        x = self.block3(x, edge_index)
        x = self.bn3(x)
        x = self.do3(x)

        x = self.block4(x, edge_index)
        x = self.bn4(x)
        x = self.do4(x)

        x = self.block5(x, edge_index)
        x = self.bn5(x)
        x = self.do5(x)

        x = self.out(x)
        return x

class MLP(torch.nn.Module):
    """Multi-Layer perceptron"""
    def __init__(self, input_size, hidden_size, output_size, layers, layernorm=True):
        super().__init__()
        self.layers = torch.nn.ModuleList()
        for i in range(layers):
            self.layers.append(torch.nn.Linear(
                input_size if i == 0 else hidden_size,
                output_size if i == layers - 1 else hidden_size,
            ))
            if i != layers - 1:
                self.layers.append(torch.nn.ReLU())
        if layernorm:
            self.layers.append(torch.nn.LayerNorm(output_size))
        self.reset_parameters()

    def reset_parameters(self):
        for layer in self.layers:
            if isinstance(layer, torch.nn.Linear):
                layer.weight.data.normal_(0, 1 / math.sqrt(layer.in_features))
                layer.bias.data.fill_(0)

    def forward(self, x):
        for layer in self.layers:
            x = layer(x)
        return x

class InteractionNetwork(MessagePassing):
    """Interaction Network as proposed in this paper:
    https://proceedings.neurips.cc/paper/2016/hash/3147da8ab4a0437c15ef51a5cc7f2dc4-Abstract.html"""
    def __init__(self, hidden_size, layers):
        super().__init__()
        self.lin_edge = MLP(hidden_size * 3, hidden_size, hidden_size, layers)
        self.lin_node = MLP(hidden_size * 2, hidden_size, hidden_size, layers)

    def forward(self, x, edge_index, edge_feature):
        edge_out, aggr = self.propagate(edge_index, x=(x, x), edge_feature=edge_feature)
        node_out = self.lin_node(torch.cat((x, aggr), dim=-1))
        edge_out = edge_feature + edge_out
        node_out = x + node_out
        return node_out, edge_out

    def message(self, x_i, x_j, edge_feature):
        x = torch.cat((x_i, x_j, edge_feature), dim=-1)
        x = self.lin_edge(x)
        return x

    def aggregate(self, inputs, index, dim_size=None):
        out = torch_scatter.scatter(inputs, index, dim=self.node_dim, dim_size=dim_size, reduce="sum")
        return (inputs, out)

class LearnedSimulator(torch.nn.Module):
    """Graph Network-based Simulators(GNS)"""
    def __init__(
        self,
        hidden_size=128,
        n_mp_layers=10, # number of GNN layers
        #num_particle_types=9,
        #particle_type_dim=16, # embedding dimension of particle types
        dim=3, # dimension of the world, typical 2D or 3D
        #window_size=5, # the model looks into W frames before the frame to be predicted
    ):
        super().__init__()
        #self.window_size = window_size
        #self.embed_type = torch.nn.Embedding(num_particle_types, particle_type_dim)
        self.node_in = MLP(dim * 2, hidden_size, hidden_size, 3)
        self.edge_in = MLP(dim + 1, hidden_size, hidden_size, 3)
        self.node_out = MLP(hidden_size, hidden_size, dim, 3, layernorm=False)
        self.n_mp_layers = n_mp_layers
        self.layers = torch.nn.ModuleList([InteractionNetwork(
            hidden_size, 3
        ) for _ in range(n_mp_layers)])

        #self.reset_parameters()

    #def reset_parameters(self):
    #    torch.nn.init.xavier_uniform_(self.embed_type.weight)

    def forward(self, data):
        # pre-processing
        # node feature: combine categorial feature data.x and contiguous feature data.pos.
        #node_feature = torch.cat((self.embed_type(data.x), data.pos), dim=-1)
        node_feature = self.node_in(data.x)
        edge_feature = self.edge_in(data.edge_attr)
        # stack of GNN layers
        for i in range(self.n_mp_layers):
            node_feature, edge_feature = self.layers[i](node_feature, data.edge_index, edge_feature=edge_feature)
        # post-processing
        out = self.node_out(node_feature)
        return out

if __name__=='__main__':
    simulator = LearnedSimulator()
    print(simulator)


import time
import json
from azure.mgmt.resource import ResourceManagementClient
from devtools_testutils import AzureMgmtTestCase
from azure.mgmt.netapp.models import CapacityPool, CapacityPoolPatch
from test_account import create_account, delete_account
from setup import *
import azure.mgmt.netapp.models

pools = [TEST_POOL_1, TEST_POOL_2]

def create_pool(client, rg=TEST_RG, acc_name=TEST_ACC_1, pool_name=TEST_POOL_1, location=LOCATION, pool_only=False):
    if not pool_only:
        create_account(client, rg, acc_name)

    pool_body = CapacityPool(service_level=SERVICE_LEVEL, size=DEFAULT_SIZE, location=location)
    pool = client.pools.create_or_update(
        pool_body,
        rg,
        acc_name,
        pool_name,
        {'location': location}
    ).result()

    return pool

def wait_for_no_pool(client, rg, acc_name, pool_name, live=False):
    # a workaround for the async nature of certain ARM processes
    co=0
    while co<5:
        co += 1
        if live:
            time.sleep(10)
        try:
            pool = client.pools.get(rg, acc_name, pool_name)
        except:
            # not found is an exception case (status code 200 expected)
            # and is actually what we are waiting for
            break

def delete_pool(client, rg, acc_name, pool_name, live=False):
    # nest resources seem to hang around for a little while even
    # when apparently deleted, therefore give it a chance
    co=0
    while co<5:
        co += 1
        if live:
            time.sleep(10)
        try:
            client.pools.delete(rg, acc_name, pool_name).wait()
        except:
            # Want to catch specifically "Can not delete resource before nested resources are deleted."
            # but should be safe to generalise
            break
    wait_for_no_pool(client, rg, acc_name, pool_name, live)

class NetAppAccountTestCase(AzureMgmtTestCase):
    def setUp(self):
        super(NetAppAccountTestCase, self).setUp()
        self.client = self.create_mgmt_client(azure.mgmt.netapp.AzureNetAppFilesManagementClient)

    def test_create_delete_pool(self):
        pool = create_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, LOCATION)
        self.assertEqual(pool.size, DEFAULT_SIZE)
        self.assertEqual(pool.name, TEST_ACC_1 + '/' + TEST_POOL_1)

        pool_list = self.client.pools.list(TEST_RG, TEST_ACC_1)
        self.assertEqual(len(list(pool_list)), 1)

        self.client.pools.delete(TEST_RG, TEST_ACC_1, TEST_POOL_1).wait()
        pool_list = self.client.pools.list(TEST_RG, TEST_ACC_1)
        self.assertEqual(len(list(pool_list)), 0)

        wait_for_no_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, live=self.is_live)
        delete_account(self.client, TEST_RG, TEST_ACC_1, live=self.is_live)

    def test_list_pools(self):
        pool = create_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, LOCATION)
        pool = create_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_2, LOCATION, pool_only=True)

        pool_list = self.client.pools.list(TEST_RG, TEST_ACC_1)
        self.assertEqual(len(list(pool_list)), 2)
        idx = 0
        for pool in pool_list:
            self.assertEqual(pool.name, pools[idx])
            idx += 1

        self.client.pools.delete(TEST_RG, TEST_ACC_1, TEST_POOL_1).wait()
        self.client.pools.delete(TEST_RG, TEST_ACC_1, TEST_POOL_2).wait()
        for pool in pools:
            wait_for_no_pool(self.client, TEST_RG, TEST_ACC_1, pools[idx], live=self.is_live)
        delete_account(self.client, TEST_RG, TEST_ACC_1, live=self.is_live)

    def test_get_pool_by_name(self):
        pool = create_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, LOCATION)

        pool = self.client.pools.get(TEST_RG, TEST_ACC_1, TEST_POOL_1)
        self.assertEqual(pool.name, TEST_ACC_1 + '/' + TEST_POOL_1)

        self.client.pools.delete(TEST_RG, TEST_ACC_1, TEST_POOL_1).wait()
        wait_for_no_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, live=self.is_live)
        delete_account(self.client, TEST_RG, TEST_ACC_1, live=self.is_live)

    def test_update_pool(self):
        pool = create_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1)
        self.assertEqual(pool.service_level, "Premium")

        pool_body = CapacityPool(service_level="Standard", size=DEFAULT_SIZE, location=LOCATION)
        pool = self.client.pools.create_or_update(
            pool_body,
            TEST_RG,
            TEST_ACC_1,
            TEST_POOL_1,
            {'location': LOCATION}
        ).result()
        self.assertEqual(pool.service_level, "Standard")

        self.client.pools.delete(TEST_RG, TEST_ACC_1, TEST_POOL_1).wait()
        wait_for_no_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, live=self.is_live)
        delete_account(self.client, TEST_RG, TEST_ACC_1, live=self.is_live)

    def test_patch_pool(self):
        create_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1)

        tag = {'Tag2': 'Value1'}
        capacity_pool_patch = CapacityPoolPatch(service_level="Standard", tags=tag)

        pool = self.client.pools.update(capacity_pool_patch, TEST_RG, TEST_ACC_1, TEST_POOL_1)
        self.assertEqual(pool.service_level, "Standard")
        self.assertTrue(pool.tags['Tag2'] == 'Value1')

        self.client.pools.delete(TEST_RG, TEST_ACC_1, TEST_POOL_1).wait()
        wait_for_no_pool(self.client, TEST_RG, TEST_ACC_1, TEST_POOL_1, live=self.is_live)
        delete_account(self.client, TEST_RG, TEST_ACC_1, live=self.is_live)


import streamlit as st
import pickle
import spacy
nlp = spacy.load("en_core_web_lg")

data = {
    "Household": 0,
    "Books": 1,
    "Electronics": 2,
    "Clothing & Accessories": 3
}

def preprocess(text):
    doc = nlp(text)
    filtered_tokens = []
    for token in doc:
        if token.is_stop or token.is_punct:
            continue
        filtered_tokens.append(token.lemma_)
    return [" ".join(filtered_tokens)]

st.title('Ecommerce Product Classifier')

# Create a text element and let the reader know the data is loading.
data_load_state = st.text_input("Enter Product Details")
with open('model/ecommerce.pkl', 'rb') as f:
    model = pickle.load(f)

    if (data_load_state != ""):
        text = preprocess(data_load_state)
        pred = model.predict(text)

        matching_keys = [key for key,
                         value in data.items() if value == pred[0]]
        st.text("Output: " + matching_keys[0])


import os
import sys

import transaction
from pyramid.paster import get_appsettings, setup_logging
from pyramid.scripts.common import parse_vars
from sqlalchemy import engine_from_config

from ..models import (
    DBSession,
    Company,
    Member,
    Order,
    Event,
    Base,
    )

def usage(argv):
    cmd = os.path.basename(argv[0])
    print('usage: %s <config_uri> [var=value]\n'
          '(example: "%s development.ini")' % (cmd, cmd))
    sys.exit(1)

def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)

    Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)
    with transaction.manager:
        event1 = Event(
            name = 'Иннопром 2016',
            date = 'с 11 по 14 июля',
            #address = 'г. Екатеринбург, ЭКСПО-бульвар, дом 2',
            description = 'Главная тема ИННОПРОМ-2016 - «Промышленные сети». Страна-партнер ИННОПРОМ-2016 – Индия. Темы, которые действительно волнуют промышленников. Более 500 спикеров и экспертов на уровне СЕО. 150 мероприятий, открытых для посещения. Более 40 партнеров по организации мероприятий, среди которых: крупнейшие игроки российского и международного промышленного сектора, финансовые институты, институты развития, исследовательские учреждения. Индивидуальные/закрытые встречи лиц, принимающих решения. Прямое общение компаний с заказчиками, партнерами и всеми уровнями власти.',
        	img = 'innoprom.jpg')
        event2 = Event(
            name = 'Gadget Show 2016',
            date = 'c 26 по 28 августа',
            description = 'Концепция выставки и кибер-фестиваля заключается в максимуме интерактива, применению роботов и виртуальной реальности, созданию развлекательного и рабочего пространства для участников и посетителей. Возможности для спонсоров и партнеров в категориях генеральный спонсор, спонсор и партнер включают широкие возможности продвижения как до начала, так во время и после выставки.',
            img = 'gadgetshow.jpg')
        event3 = Event(
            name = 'Международная туристическая выставка Expotravel',
            date = 'c 7 по 8 октября',
            description = '«EXPOTRAVEL» - яркое событие в индустрии туризма, направленное на развитие внутреннего, въездного и выездного туризма, а также на укрепление взаимоотношений между региональными и зарубежными представителями туристического бизнеса за счет создания уникальной платформы для их взаимодействия. Участие в выставке позволит Вам рассказать о своих уникальных услугах, установить контакты с потенциальными клиентами, как в Уральском регионе, так и за его пределами, и обменяться опытом с вашими коллегами.',
            img = 'expotravel.jpg')
        DBSession.add(event1)
        DBSession.add(event2)
        DBSession.add(event3)

        company1 = Company(
            name = 'Госкорпорация "Росатом"',
            site = 'http://www.rosatom.ru/')
        company2 = Company(
            name = 'КУКА РОБОТИКС РУС, ООО',
            site = 'http://www.kuka-robotics.com/russia/ru/')
        DBSession.add(company1)
        DBSession.add(company2)

        member1 = Member(
            name = 'Сергей Кириенко',
            phone = '89022134935',
            company = company1)
        member2 = Member(
            name = 'Эйил Грир',
            phone = '89002107823',
            company = company2)
        member3 = Member(
            name = 'Мэтью Уэбб',
            phone = '89002107824',
            company = company2)
        DBSession.add(member1)
        DBSession.add(member2)
        DBSession.add(member3)

        order1 = Order(
            member = member1,
            payment_status = True,
            obtain_status = False)
        order2 = Order(
            member = member2,
            payment_status = True,
            obtain_status = False)
        order3 = Order(
            member = member3,
            payment_status = True,
            obtain_status = False)
        DBSession.add(order1)
        DBSession.add(order2)
        DBSession.add(order3)


import sys
import socket
import threading
from random import random
from time import sleep

from flask import Flask, render_template, Response, url_for, copy_current_request_context
from flask_socketio import SocketIO, emit
from camera import Camera
from data_stream import DataStream

app = Flask(__name__)

socketio = SocketIO(app, async_mode=None, logger=False, engineio_logger=False)

HOST = ''
PORT = 12801

data_stream = DataStream(PORT)

c = Camera(data_stream)

thread = threading.Thread()
thread_stop_event = threading.Event()

def randomNumberGenerator():
    """2
    Generate a random number every 1 second and emit to a socketio instance (broadcast)
    Ideally to be run in a separate thread?
    """
    #infinite loop of magical random numbers
    print("Making random numbers")
    gen = data_stream.read()
    while not thread_stop_event.isSet():
        if data_stream.connected():
            data = next(gen)
            socketio.emit('newdata', {'data': data}, namespace='/test')
        socketio.sleep(0.01)
@app.route('/video_feed')
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(c.gen_frames(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')

@socketio.on('connect', namespace='/test')
def test_connect():
    # need visibility of the global thread object
    global thread
    print('Client connected')

    #Start the random number generator thread only if the thread has not been started before.
    if not thread.is_alive():
        print("Starting Thread")
        thread = socketio.start_background_task(randomNumberGenerator)

@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected')

socketio.run(app)


from math import floor
import numpy as np

class Helper:
    PROMOTER_FILE: str = "data/CNNPromoterData/human_non_tata.fa"
    NONPROMOTER_FILE: str = "data/CNNPromoterData/human_nonprom_big.fa"
    _dna_dict: dict
    _lbl_dict: dict
    _variable_dict: dict

    def __init__(self):
        self._dna_dict = {'A': 0, 'C': 1, 'G': 2, 'T': 3}
        self._lbl_dict = {'Non-Promoter': 0, 'Promoter': 1}
        self._variable_dict = {}

    def get_DNA_dict(self):
        return self._dna_dict

    def get_LABEL_dict(self):
        return self._lbl_dict

    def add_variable(self, name, value):
        try:
            _ = self._variable_dict[name]
            return False
        except KeyError:
            self._variable_dict[name] = value
            return True

    def get_variable(self, name):
        try:
            return self._variable_dict[name]
        except KeyError:
            raise Exception("Variable doesn't exist.")

    def update_variable(self, name, value):
        try:
            _ = self._variable_dict[name]
            self._variable_dict[name] = value
            return True
        except KeyError:
            return False

    @staticmethod
    def normalize(x):
        return (x-np.min(x))/(np.max(x)-np.min(x))

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

    @staticmethod
    def relu(x):
        return x * (x > 0)

    @staticmethod
    def neg_relu(x, value):
        return min(x, value)

    @staticmethod
    def reciprocal(x):
        temp = 1/x
        return (temp / np.sum(temp, axis=0))

    @staticmethod
    def sum_one(x):
        dif = 1 - np.sum(x, axis=1)
        approx = x + np.expand_dims((dif / 4), axis=1)
        check = np.sum(approx, axis=1)
        if not np.all(check == 1):
            dif = 1 - check
            bad_rows = np.nonzero(check != 1)
            for row in bad_rows[0]:
                if dif[row] > 0:
                    approx[row, np.argmax(approx, axis=1)[row]] += dif[row]
                else:
                    approx[row, np.argmin(approx, axis=1)[row]] += dif[row]
        return approx

    @staticmethod
    def aggregate_weights(filter_idx, filter_weights, position, conv_result_length, weights1=None, weights2=None):
        if weights1 is None:
            weights1 = np.zeros_like(weights2)
        if weights2 is None:
            weights2 = np.zeros_like(weights1)
        return filter_weights * weights1[position + conv_result_length*filter_idx] - filter_weights * weights2[position + conv_result_length*filter_idx]

    @staticmethod
    def conv1d_out_shape(in_length, kernel_size=1, padding=0, stride=1, dilation=1):
        result = floor( ((in_length + (2 * padding) - ( dilation * (kernel_size - 1) ) - 1 )/ stride) + 1)
        return result

    def get_jaspar_matrices(self, tfbs):
        import coreapi, seqlogo
        helper_class = Helper()
        client = coreapi.Client()
        schema = client.get("http://jaspar.genereg.net/api/v1/docs")
        matrices = {}
        for tfbs_name, collection in tfbs.items():
            action = ["matrix", "list"]
            params = {
                "page_size": '1000',
                "version": 'latest'
            }
            if(tfbs_name is not None):
                params['name'] = tfbs_name
            if(collection is not None):
                params['collection'] = collection
            result_list = client.action(schema, action, params=params)

            action = ["matrix", "read"]
            for res in result_list['results']:
                params = {
                    "matrix_id": res['matrix_id']
                }
                result_read = client.action(schema, action, params=params)
                pfm = np.ndarray((len(result_read['pfm']), len(result_read['pfm']['A'])), dtype=int)
                for key, value in helper_class.get_DNA_dict().items():
                    pfm[value] = np.array(result_read['pfm'][key], dtype=int)
                pm = seqlogo.CompletePm(pfm=pfm)
                matrices[result_read['name']] = pm
        return matrices

    @staticmethod
    def get_filters(matrices, matrix_type):
        max_filter_len = 0
        pms = []
        for m_name, m in matrices.items():
            matrix = getattr(m, matrix_type)
            pm_len = len(matrix)
            if pm_len > max_filter_len:
                max_filter_len = pm_len
            pms.append([m_name, matrix._values.T])
        return max_filter_len, pms


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

# Always prefer setuptools over distutils
from setuptools import setup, find_packages
from codecs import open  # To use a consistent encoding
from os import path
import io

NAME = "trading_ig"
filename = "%s/version.py" % NAME
with open(filename) as f:
    exec(f.read())

here = path.abspath(path.dirname(__file__))

def readme():
    filename = path.join(here, "README.rst")
    with io.open(filename, "rt", encoding="UTF-8") as f:
        return f.read()

setup(
    name=NAME,
    # Versions should comply with PEP440.  For a discussion on single-sourcing
    # the version across setup.py and the project code, see
    # https://packaging.python.org/en/latest/development.html#single-sourcing-the-version
    # version='0.0.1',
    version=__version__,
    description="A lightweight wrapper for the IG Markets API written in Python",
    long_description=readme(),
    # The project's main homepage.
    url=__url__,
    # Author details
    author=__author__,
    author_email=__author_email__,
    # Choose your license
    license=__license__,
    # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[
        # How mature is this project? Common values are
        #   3 - Alpha
        #   4 - Beta
        #   5 - Production/Stable
        "Development Status :: 3 - Alpha",
        # Indicate who your project is intended for
        "Environment :: Console",
        # 'Topic :: Software Development :: Build Tools',
        "Intended Audience :: Science/Research",
        "Operating System :: OS Independent",
        # Specify the Python versions you support here. In particular, ensure
        # that you indicate whether you support Python 2, Python 3 or both.
        "Programming Language :: Cython",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Topic :: Scientific/Engineering",
        # Pick your license as you wish (should match "license" above)
        "License :: OSI Approved :: BSD License",
    ],
    python_requires='>=3.6',
    # What does your project relate to?
    keywords="trading, spread betting, CFDs",
    # You can just specify the packages manually here if your project is
    # simple. Or you can use find_packages().
    packages=find_packages(exclude=["contrib", "docs", "tests*"]),
    # List run-time dependencies here.  These will be installed by pip when your
    # project is installed. For an analysis of "install_requires" vs pip's
    # requirements files see:
    # https://packaging.python.org/en/latest/technical.html#install-requires-vs-requirements-files
    install_requires=[
        "munch==2.5.0",
        "pycryptodome==3.9.8",
        "requests==2.24.0",
        "requests-cache==0.5.2",
        "six==1.15.0",
        "pandas==1.0.5",
    ],  # bunch->lunch->infi.bunch->munch
    # List additional groups of dependencies here (e.g. development dependencies).
    # You can install these using the following syntax, for example:
    # $ pip install -e .[dev,test]
    extras_require={"dev": ["check-manifest", "pytest"], "test": ["pytest", "pytest-cov"],},
    # If there are data files included in your packages that need to be
    # installed, specify them here.  If using Python 2.6 or less, then these
    # have to be included in MANIFEST.in as well.
    # package_data={
    #    'sample': ['logging.conf'],
    # },
    # Although 'package_data' is the preferred approach, in some case you may
    # need to place data files outside of your packages.
    # see http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files
    # In this case, 'data_file' will be installed into '<sys.prefix>/my_data'
    # data_files=[('my_data', ['data/data_file'])],
    # To provide executable scripts, use entry points in preference to the
    # "scripts" keyword. Entry points provide cross-platform support and allow
    # pip to create the appropriate form of executable for the target platform.
    entry_points={"console_scripts": ["sample=sample:main",],},
)


"""
High level functions
--------------------

"""
from . import solubility as sol
from .area import get_area_from_dataset

def flux_bulk(
    temp_C,
    salt,
    pCO2_sea_uatm,
    pCO2_air_uatm,
    pres_hPa,
    kw_cmhr,
):
    """
    Calculates bulk air-sea CO2 fluxes

    .. math::
        FCO_2 = k_w \\cdot K_0 \\cdot \\Delta pCO_2

    Args:
        temp_C (array): temperature from OISST in degCelcius with an allowable
            range of [-2:45]
        salt (array): salinity from EN4 in PSU. Allowable range [5 : 50]
        pCO2_sea_uatm (array): partial pressure of CO2 in the sea in micro-atmospheres.
            Allowable range is [50 : 1000]
        pCO2_air_uatm (array): partial pressure of CO2 in the air in micro-atmospheres.
            Allowable range is [50 : 1000].
        press_hPa (array): atmospheric pressure in hecto-Pascals with allowable range [500:1500]
        kw_cmhr (array): the gas transfer velocity in (cm/hr). Given the careful choices
            involved in estimating kw, we require the user to explicitly provide kw.
            kw can be calculated with pyseaflux.gas_transfer_velocity.<func>. Things to be
            aware of when calculating kw: wind product and scaling coeffient of gas transfer,
            resolution resampling, and the formulation (i.e. quadratic, cubic).

    Returns:
        array:
            Sea-air CO2 flux where positive is out of the ocean and negative is
            into the ocean. Units are gC.m-2.day-1 (grams Carbon per metre squared
            per day). If the input is an xarray.DataArray, then the output will be
            a data array with fluxes, globally integrated flux, and the area used to
            integrate the fluxes.
    """
    import xarray as xr

    pres_atm = pres_hPa / 1013.25
    temp_K = temp_C + 273.15

    pCO2sea = pCO2_sea_uatm * 1e-6  # to atm
    pCO2air = pCO2_air_uatm * 1e-6

    K0 = sol.solubility_weiss1974(salt, temp_K, pres_atm)

    """unit analysis
    kw = (cm . hr-1) * hr . day-1 . cm-1 . m
    kw = m . day-1   """
    kw = kw_cmhr * (24 / 100)

    # molar mas of carbon in g . mmol-1
    mC = 12.0108 * 1000  # (g . mol-1) / (mmol . mol-1)

    """ unit analysis
    flux = (m . day-1) .  (mol . L-1 . atm-1) . atm . (gC . mmol-1)
    flux = (m . day-1) . (mmol . m-3 . atm-1) . atm . (gC . mmol-1)
    flux = gC . m-2 . day-1   """
    CO2flux_bulk = kw * K0 * (pCO2sea - pCO2air) * mC

    if isinstance(CO2flux_bulk, xr.DataArray):
        area = get_area_from_dataset(CO2flux_bulk)
        ds = xr.Dataset()
        ds["fgco2"] = CO2flux_bulk.assign_attrs(
            units="gC/m2/day",
            description="Air sea CO2 fluxes calculated using the bulk formulation.",
        )
        ds["area"] = area
        ds["fgco2_global"] = (
            (CO2flux_bulk * area * 365)
            .sum(["lat", "lon"])
            .assign_attrs(units="gC/Yr", description="integrated fluxes fgco2 * area")
        )
        return ds
    else:
        return CO2flux_bulk


# -*- coding: utf-8 -*-
"""
Created on Thu Apr 30 09:34:57 2020

@author: CS
"""

import librosa
import librosa.display
import json
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from matplotlib.pyplot import specgram
import pandas as pd
import seaborn as sns
import glob
import os
from tqdm import tqdm
import pickle
import IPython.display as ipd  # To play sound in the notebook

fname = 'D:/Project/Sound/ShipsEar/Data_frame/6__Passengers_19.wav'
data, sampling_rate = librosa.load(fname)
melspec = librosa.feature.melspectrogram(data, sampling_rate, n_mels=128)
logmelspec = librosa.power_to_db(melspec)
plt.figure(figsize=(8,4))
librosa.display.waveplot(data, sr=sampling_rate)
ipd.Audio(data, rate=sampling_rate)
plt.figure(figsize=(8,4))
librosa.display.specshow(logmelspec, y_axis='linear')
# =============================================================================
# # 数据增广方法（Augmentations Methods）
# =============================================================================
# (1) 添加白噪声
def noise(data):
    noise_amp = 0.05*np.random.uniform() * np.amax(data)
    data = data.astype('float') + noise_amp * np.random.normal(size=data.shape[0])
    return data
# 在背景中添加静态噪声。
x = noise(data)
plt.figure(figsize=(8,4))
librosa.display.waveplot(x, sr=sampling_rate)
ipd.Audio(x , rate=sampling_rate)

# (2) 随机移动
def shift(data):
    s_range = int(np.random.uniform(low=-5, high=5)*1000)
    return np.roll(data, s_range)

x = shift(data)
plt.figure(figsize=(8,4))
librosa.display.waveplot(x, sr=sampling_rate)

# (3) 时域拉伸
def stretch(data, rate=0.8):
    data = librosa.effects.time_stretch(data, rate)
    return data

x = stretch(data)
melspec = librosa.feature.melspectrogram(x, sampling_rate, n_mels=128)
logmelspec = librosa.power_to_db(melspec)
plt.figure(figsize=(8,4))
librosa.display.waveplot(x, sr=sampling_rate)
plt.figure(figsize=(8,4))
librosa.display.specshow(logmelspec, y_axis='linear')

# (4) 音调变换，该方法强调高音
def pitch(data, sample_rate):
    bins_per_octave = 12
    pitch_pm = -2
    pitch_change = pitch_pm * 2*(np.random.uniform())
    data = librosa.effects.pitch_shift(data.astype('float64'),
                                       sample_rate, n_steps=pitch_change,
                                       bins_per_octave=bins_per_octave)
    return data

x = pitch(data, sampling_rate)
melspec = librosa.feature.melspectrogram(x, sampling_rate, n_mels=128)
logmelspec = librosa.power_to_db(melspec)
plt.figure(figsize=(8, 4))
librosa.display.waveplot(x, sr=sampling_rate)
plt.figure(figsize=(8,4))
librosa.display.specshow(logmelspec, y_axis='linear')

# (5) 动态随机变化
def dyn_change(data):
    dyn_change = np.random.uniform(low=-0.5 ,high=7)
    return (data * dyn_change)

x = dyn_change(data)
plt.figure(figsize=(8, 4))
librosa.display.waveplot(x, sr=sampling_rate)

# (6)加速(压缩)和音调变换
def speedNpitch(data):
    length_change = np.random.uniform(low=0.8, high=1)
    speed_fac =1.2 / length_change
    tmp = np.interp(np.arange(0,len(data), speed_fac), np.arange(0, len(data)), data)
    minlen = min(data.shape[0], tmp.shape[0])
    data *= 0
    data[0:minlen] = tmp[0:minlen]
    return data

x = speedNpitch(data)
plt.figure(figsize=(8, 4))
librosa.display.waveplot(x, sr=sampling_rate)


import os
import json
import cfg
from copy import deepcopy

default_config = {"main_font_size": 11,
                  "title_font_size": 20,
                  "is_autoinput_last": False,
                  "autoinput_text": "",
                  "autoinput_last": "",
                  "is_autoreload": False,
                  "autoreload_timeout": 600}

config = None

def load_config():
    print("Loading config file from", cfg.CONFIG_LOCATION)
    try:
        new_config = json.load(open(cfg.CONFIG_LOCATION))
        for key in default_config:
            if key not in new_config:
                val = deepcopy(default_config[key])
                new_config[key] = val
                print("Loading new key:", key, "=", val)
        return new_config
    except json.decoder.JSONDecodeError:
        print("Your config file is corrupted")
        return reset_config()

def reset_config():
    print("Defaulting config")
    return deepcopy(default_config)

def save_config():
    location = cfg.CONFIG_LOCATION
    print("Saving config file to", location)
    dirlocation = os.path.dirname(location)
    if not os.path.exists(dirlocation):
        os.makedirs(dirlocation)
    json.dump(config, open(location, 'w'))

if os.path.exists(cfg.CONFIG_LOCATION):
    config = load_config()
else:
    config = reset_config()

save_config()


#!/usr/bin/python3

'''
Downloading/Updating Color spreads of One Piece from the fandom site.

Usage:
python ./OP-ColorSpreads-CLI.py
    --> will download all the images in the current directory

python ./OP-ColorSpreads-CLI.py -p "{PATH-TO-UPDATE}"
    --> will download in the provided directory the latest color spreads. (can also be used to download all to a specific directory)
    --> do not end the path with a "/", it may break the script. to lazy to handle error...
'''

import requests
from bs4 import BeautifulSoup
import os
import argparse

# URL where there is a list of all the imgs to download
baseUrl = "https://onepiece.fandom.com/wiki/Category:Color_Spreads"

## Cleanup output
LINE_UP = '\033[1A'
LINE_CLEAR = '\x1b[2K'

# PARAMETER verification
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--pathToUpdate", type=str, required=False)
args = parser.parse_args()

if args.pathToUpdate:
    imgPath = args.pathToUpdate
    localImgList = []
    for item in os.listdir(imgPath):
        if (os.path.isfile(f"{imgPath}/{item}")) and ((item.endswith(".png")) or (item.endswith(".jpg")) or (item.endswith(".jpeg"))):
            localImgList.append(item)
    print(f"Updating {imgPath} --> number of local images : {len(localImgList)}")
    update = True
else:
    imgPath = (os.getcwd()).replace("\\", "/")
    print(f"Downloading all images in current folder: {imgPath}")
    update = False

# DEFINITION
def DownloadImg(session, url,name,path):
    '''
    Download the content of a get request.
    '''
    r = session.get(url)
    if r.status_code == 200:
        with open(path + "/" + name, 'wb') as f:
            f.write(r.content)
    else:
        print(f"Failed to get: {url} -- Status Code: {r.status_code}")

# Session
s = requests.Session()
# Base GET request
r = s.get(baseUrl)
if r.status_code == 200:
    print(f"{baseUrl}: 200 - OK")
    # Parse Base HTML
    soup = BeautifulSoup(r.text, 'html.parser')
    lis = (soup.find('ul',{"class": "category-page__members-for-char"})).find_all('li')
    # Populate a list of remote image names
    remoteImgList = []
    for li in lis:
        imgSrc = li.find('img')['src']
        imgUrl = imgSrc.split("/revision/")[0]
        imgName = imgUrl.split("/")[-1]
        remoteImgList.append(imgName)

    print(f"Total number of remote images: {len(remoteImgList)}")
    # Download images following the passed parameter.
    if update:
        if sorted(remoteImgList) == sorted(localImgList):
            print(f"{imgPath} up-to-date. No new images to download")
        else:
            counter = 0
            for li in lis:
                imgSrc = li.find('img')['src']
                imgUrl = imgSrc.split("/revision/")[0]
                imgName = imgUrl.split("/")[-1]

                if imgName not in localImgList:
                    print(f"Downloading: {imgName} in {imgPath}")
                    print(LINE_UP, end=LINE_CLEAR)
                    DownloadImg(s, imgUrl, imgName, imgPath)
                    counter += 1

            print(f"Downloaded {counter} images in {imgPath}")
    else:
        counter=0
        for li in lis:
            imgSrc = li.find('img')['src']
            imgUrl = imgSrc.split("/revision/")[0]
            imgName = imgUrl.split("/")[-1]

            print(f"Downloading: {imgName}")
            print(LINE_UP, end=LINE_CLEAR)
            DownloadImg(s, imgUrl, imgName, imgPath)
            counter += 1
        print(f"Downloaded {counter} images in {imgPath}")
else:
    print(f"{baseUrl}: {r.status_code} - NOT OK")


import os
import shutil
from verions import Version
import requests
from baselib import is_downloadable, create_dir, proj_root, curr_dir, clear_dir, minecraft_path
from globals import *
from typing import List
import json
from zipfile import ZipFile
from selection_picker_joshika39 import *
import newmod
zip_url = "https://github.com/joshika39/minecraft-mods/raw/main/data/mods.zip"
pack_url = "https://raw.githubusercontent.com/joshika39/minecraft-mods/main/mod-downloader/config/packs.json"

zip_path = os.path.join(proj_root(), 'data', 'mods.zip')
json_dir = os.path.join(proj_root(), 'data', 'mods')
dev_json_dir = os.path.join(proj_root(), 'mod-downloader', 'config', 'mods')
mod_file_dir = os.path.join(proj_root(), 'data', 'jars')
pack_path = os.path.join(proj_root(), 'data', 'packs.json')
dev_pack_path = os.path.join(proj_root(), 'mod-downloader', 'config', 'packs.json')

def init(is_local=False):
    create_dir(os.path.join(proj_root(), 'data'))
    create_dir(json_dir)
    create_dir(mod_file_dir)
    if is_local:
        clear_dir(json_dir)
        pack_mods()
        unpack_mods()
        shutil.copyfile(dev_pack_path, pack_path)
        if len(os.listdir(json_dir)) <= 0 or not os.path.exists(zip_path):
            download_config()
            unpack_mods()
    else:
        download_config()
        unpack_mods()

def download_config():
    r = requests.get(zip_url, allow_redirects=True)
    open(zip_path, 'wb').write(r.content)
    r = requests.get(pack_url, allow_redirects=True)
    open(pack_path, 'wb').write(r.content)

def pack_mods():
    mods_path = os.path.join(proj_root(), 'mod-downloader', 'config', 'mods')
    with ZipFile(zip_path, 'w') as zip_obj:
        for mod in os.listdir(mods_path):
            file_path = os.path.join(mods_path, mod)
            if os.path.exists(file_path) and file_path.endswith('.json'):
                zip_obj.write(file_path, mod)

def unpack_mods():
    if not os.path.exists(zip_path):
        download_config()
    with ZipFile(zip_path, 'r') as zip_obj:
        path = os.path.join(proj_root(), 'data', 'mods')
        create_dir(path)
        os.chdir(path)
        zip_obj.extractall()

def remove_numbers(filename: str):
    return ''.join([i for i in filename if not i.isdigit()])

def remove_special(filename: str):
    return ''.join([i for i in filename if i.isalnum() or i == name_separator])

def construct_name(name: str):
    name = remove_numbers(name)
    name = name.replace('-', name_separator)
    name = name.replace('_', name_separator)
    name = name.replace('reforged', '')
    name = name.replace('build', '')
    name = name.replace('forge', '')
    name = name.replace('version', '')
    name = name.replace('-v', '')
    name = name.replace('-mc', '')
    name = name.replace('.jar', '')
    name = remove_special(name)
    for i in range(5, 2, -1):
        name = name.replace(name_separator * i, name_separator)

    while name.endswith(name_separator):
        name = name[:-1]
    while name.startswith(name_separator):
        name = name[1:]
    return name.lower()

def link_id(mod_id: str) -> str:
    if len(mod_id) == 7:
        result = ""
    while mod_id:
        result += f'{int(mod_id[:4])}/'
        mod_id = mod_id[4:]
    return result

def installed_mods_list() -> list[str]:
    mods = []
    if os.path.exists(mod_file_dir):
        for folder in os.listdir(mod_file_dir):
            folder_path = os.path.join(mod_file_dir, folder)
            if os.path.isdir(folder_path):
                for file in os.listdir(folder_path):
                    file_path = os.path.join(folder_path, file)
                    if os.path.exists(file_path) and file.endswith('.jar'):
                        mods.append(file)
    return mods

def mod_list_to_filenames(mod_list: list['Mod']):
    return [mod.filename for mod in mod_list]

class Mod:
    depend_on = []  # type: list['Mod']

    @classmethod
    def load_from_json(cls, json, category: str) -> 'Mod':
        return cls(json['id'], category, json['domain'], json['filename'], json['depend_on'], json['state'])

    def __str__(self):
        deps = ""
        for mod in self.depend_on:
            deps += f'{mod.name} '
        deps = deps[:-1]
        deps = deps[:100] + (deps[100:] and '..')
        return colorize(f"{self.name}{GRAY} - ({self.state}) - {YELLOW}({deps})", GREEN)

    def details(self) -> str:
        return f"Mod: {self.name}\n-> {self.category}\n-> {self.filename}\n-> {self.link}\n" + 10 * '-' + '\n'

    def __init__(self, mod_id: str, category: str, domain: str, file_name: str, depend_on_str: list[str], state: str):
        self.state = state
        self.mod_id = mod_id
        self.category = category
        self.filename = file_name.replace(' ', '')
        self.domain = domain
        self.name = construct_name(self.filename)
        self.depend_on_str = depend_on_str
        self.local_dir = os.path.join(mod_file_dir, self.category)
        self.local_path = os.path.join(mod_file_dir, self.category, self.filename)
        if self.state != 'inactive' or self.state == "download":
            self.link = f'https://{self.domain}.forgecdn.net/files/{link_id(self.mod_id)}{self.filename}'
        else:
            self.link = None

    def depend_on_to_str(self) -> list[str]:
        data = [dep.mod_id for dep in self.depend_on]
        return data

    def serializable_attrs(self):
        props = {
            'id': self.mod_id,
            'domain': self.domain,
            'filename': self.filename,
            'depend_on': self.depend_on_to_str(),
            'state': self.state
        }
        return props

    def download(self) -> bool:
        if not os.path.exists(self.local_path):
            create_dir(self.local_dir)
            try:
                if is_downloadable(self.link):
                    print(f'Downloading: {self.name}\n[TO]: {self.local_path}\n[FROM]: {self.link}')
                    r = requests.get(self.link, allow_redirects=True)
                    open(self.local_path, 'wb').write(r.content)
                    return True
                else:
                    print(f'Download failed for: {self.link}')
                    return False
            except requests.exceptions.HTTPError as err:
                print(f'Download failed for: {self.link}')
                print(err)
                return False

    def copy2(self, path: str):
        dest = os.path.join(path, self.filename)
        if not os.path.exists(self.local_path):
            create_dir(self.local_dir)
            if self.link is not None:
                if os.path.exists(self.local_dir) and not os.path.exists(self.local_path):
                    self.download()

        if not os.path.exists(dest) and self.state == "install" and os.path.exists(self.local_path):
            shutil.copyfile(self.local_path, dest)
            print(f'Copying: {self.local_path} -> {dest}')
        if self.depend_on is not None and len(self.depend_on) > 0:
            for dep in self.depend_on:
                dep.copy2(path)

    def remove(self):
        print(f"Removing: {self.local_path}")
        os.remove(os.path.join(mod_file_dir, self.local_path))
        if self.depend_on is not None and len(self.depend_on) > 0:
            for dep in self.depend_on:
                dep.remove()

class ModPack:
    @classmethod
    def load_from_json(cls, pack_json, pack_content: List[Mod]) -> 'ModPack':
        return cls(pack_json['name'], pack_json['display_name'], pack_json['description'], pack_content)

    @classmethod
    def create_pack(cls, name, display_name, description, mods) -> 'ModPack':
        return cls(name, display_name, description, mods)

    @classmethod
    def init_pack(cls, existing_pack: 'ModPack') -> 'ModPack':
        return cls(existing_pack.name, existing_pack.display_name, existing_pack.description, existing_pack.pack_content)

    def __init__(self, name: str, display_name: str, description: str, pack_content: List[Mod]):
        self.name = name
        self.display_name = display_name
        self.description = description
        self.pack_content = pack_content

    def __str__(self) -> str:
        contents = ""
        for mod in self.pack_content:
            contents += f'{mod.name} '
        contents = contents[:-1]
        contents = contents[:100] + (contents[100:] and '..')
        return f'{BOLD}{self.display_name}{RESET_ALL} ({self.name})\n\tDescription:\t{GREEN}{self.description}{RESET_COLOR}\n\tContents:\t[{GRAY}{contents}{RESET_COLOR}]'

    def serializable_attrs(self):
        props = {'name': self.name, 'display_name': self.display_name, 'description': self.description,
                 'pack_content': [i.mod_id for i in self.pack_content]}
        return props

    def list_contents(self, types=['inactive', 'download', 'install']) -> list[Mod]:
        mods = [] #type: list[Mod]
        for mod in self.pack_content:
            if mod.state in types:
                mods.append(mod)
        return mods

    def list_contents_ordered(self, categories: list[str], types=['inactive', 'download', 'install']) -> tuple[int, dict[str, list[Mod]]]:
        count = 0
        oredered = {}
        for category in categories:
            mod_category_content = []
            for mod in self.pack_content:
                if mod.state in types and mod.category == category:
                    mod_category_content.append(mod)
                    count += 1
            oredered[category] = mod_category_content
        return count, oredered

    def remove_mods(self, mods_to_remove: list[Mod]):
        new_list = set(self.pack_content) - set(mods_to_remove)
        self.pack_content = new_list

class ModManager:
    mod_list = []  # type: List[Mod]
    new_mod_list = []  # type: List[newmod.Mod]

    mod_categories = []

    def __init__(self, is_local=False):
        if len(self.mod_list) > 0:
            raise Exception
        init(is_local)
        if is_local:
            json_dir = dev_json_dir
        for file in os.listdir(json_dir):
            if file.endswith('.json') and "new" not in file:
                file_path = os.path.join(json_dir, file)
                json_data = open(file_path, 'r').read()
                mods_data = json.loads(json_data)
                for mod_data in mods_data:
                    category = file.replace('.json', '')
                    mod = Mod.load_from_json(mod_data, category)
                    if mod:
                        self.mod_list.append(mod)
                        n_mod = newmod.Mod(mod.mod_id, file_path, os.path.join(dev_json_dir, f'new{category}.json'), mod.category)
                        n_mod.setup(mod.domain, mod.filename, mod.depend_on_str, mod.state)
                        self.new_mod_list.append(n_mod)
                        # if not os.path.exists(mod.local_path) and mod.state != "inactive":
                        #     mod.download()
                        if category not in self.mod_categories:
                            self.mod_categories.append(category)

        for mod_obj in self.mod_list:
            temp = []
            for mod_str in mod_obj.depend_on_str:
                temp.append(self.get_mod_by_id(mod_str))
            mod_obj.depend_on = temp

    def __del__(self):
        self.mod_list.clear()
        self.mod_categories.clear()

    def print(self):
        print(f'Mod count: {len(self.mod_list)}')
        self.mod_list.sort(key=lambda m: m.name)
        for mod in self.mod_list:
            print(mod)
        input("Press any key to continue...")

    def mod_to_json(self):
        for category in self.mod_categories:
            target = os.path.join(curr_dir(), 'config', 'mods', f'{category}.json')
            data = []
            for mod in self.mod_list:
                if mod.category == category:
                    data.append(mod.serializable_attrs())

            json_object = json.dumps(data, indent=4)

            with open(target, "w") as outfile:
                outfile.write(json_object)
            print(f'{category} category done')

    def search(self, search_string: str) -> list[Mod]:
        results = []
        for mod in self.mod_list:
            if mod.state != "inactive" and (search_string in mod.name
                                            or search_string in mod.category
                                            or search_string in mod.link
                                            or search_string in mod.filename):
                results.append(mod)
        return results

    def get_mods_by_category(self, category: str) -> list[Mod]:
        mod_list = []
        for mod in self.mod_list:
            if mod.state == "install" and mod.category == category:
                mod_list.append(mod)
        return mod_list

    def get_mod_by_name(self, name: str) -> Mod:
        for mod in self.mod_list:
            if mod.name == name:
                return mod

    def get_mod_by_filename(self, filename: str) -> Mod:
        for mod in self.mod_list:
            if mod.filename == filename:
                return mod

    def get_mod_by_id(self, id: str) -> Mod:
        for mod in self.mod_list:
            if mod.mod_id == id:
                return mod

    def mod_id_list_to_mod(self, ids: list[str]) -> List[Mod]:
        return [self.get_mod_by_id(_mod_id) for _mod_id in ids]

class PackManager:
    mod_packs = []  # type: List[ModPack]

    def __init__(self, mod_manager: ModManager, is_local=False):
        if len(self.mod_packs) > 0:
            raise Exception
        init(is_local)
        if os.path.exists(pack_path):
            json_data = open(pack_path, 'r').read()
            packs = json.loads(json_data)
            for pack_data in packs:
                temp_mods = []  # type: list[Mod]
                for mod_id in pack_data['pack_content']:
                    if mod_manager.get_mod_by_id(mod_id) is not None:
                        temp_mods.append(mod_manager.get_mod_by_id(mod_id))
                self.mod_packs.append(ModPack.load_from_json(pack_data, temp_mods))

    def __del__(self):
        self.mod_packs.clear()

    def select_mod_packs(self) -> ModPack:
        if self.mod_packs is not None:
            return SingleMenu("Select a modpack:", self.mod_packs).show()

    def get_pack_content_by_name(self, name):
        for pack in self.mod_packs:
            if pack == name:
                return pack

    def packs_to_json(self):
        data = []
        for pack in self.mod_packs:
            data.append(pack.serializable_attrs())
        json_obj = json.dumps(data, indent=4)
        with open(dev_pack_path, "w") as outfile:
            outfile.write(json_obj)


from django.contrib import admin

from .models import Player
# Register your models here.
class PlayerAdmin(admin.ModelAdmin):
    fieldsets = [
        ('First Name',   {'fields': ['player_firstname']}),
        ('Last Name',    {'fields': ['player_lastname']}),
        ('Bio',          {'fields': ['player_description']}),
        (None,           {'fields': ['image']}),
    ]

admin.site.register(Player, PlayerAdmin)


#!/usr/bin/python
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from trajectory_msgs.msg import *
from control_msgs.msg import *
from trajectory_msgs.msg import JointTrajectory,JointTrajectoryPoint
from geometry_msgs.msg import Twist
from control_msgs.msg import JointTrajectoryControllerState
from nav_msgs.msg import Odometry
import numpy as np
import rospy
import time
from math import cos, acos, sin, asin, sqrt, exp, atan, atan2, pi, tan, ceil
from sklearn.gaussian_process.kernels import RBF, WhiteKernel
from sklearn.gaussian_process import GaussianProcessRegressor
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import matplotlib
import seaborn as sns
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D # <--- This is important for 3d plotting

#Data Record
ur_sub_record = []
car_sub_record = []
def cmap_map(function, cmap):
    """ Applies function (which should operate on vectors of shape 3: [r, g, b]), on colormap cmap.
    This routine will break any discontinuous points in a colormap.
    """
    cdict = cmap._segmentdata
    step_dict = {}
    # Firt get the list of points where the segments start or end
    for key in ('red', 'green', 'blue'):
        step_dict[key] = list(map(lambda x: x[0], cdict[key]))
    step_list = sum(step_dict.values(), [])
    step_list = np.array(list(set(step_list)))
    # Then compute the LUT, and apply the function to the LUT
    reduced_cmap = lambda step : np.array(cmap(step)[0:3])
    old_LUT = np.array(list(map(reduced_cmap, step_list)))
    new_LUT = np.array(list(map(function, old_LUT)))
    # Now try to make a minimal segment definition of the new LUT
    cdict = {}
    for i, key in enumerate(['red','green','blue']):
        this_cdict = {}
        for j, step in enumerate(step_list):
            if step in step_dict[key]:
                this_cdict[step] = new_LUT[j, i]
            elif new_LUT[j,i] != old_LUT[j, i]:
                this_cdict[step] = new_LUT[j, i]
        colorvector = list(map(lambda x: x + (x[1], ), this_cdict.items()))
        colorvector.sort()
        cdict[key] = colorvector

    return matplotlib.colors.LinearSegmentedColormap('colormap',cdict,1024)
light_jet = cmap_map(lambda x: x/2 +0.5, matplotlib.cm.coolwarm)

#ur state subsciber callback function
def ur_sub_callback(state):
    ur_sub_record.append(list(state.actual.positions))

#mobile base state subsciber callback function
def car_sub_callback(odom):
    x = odom.pose.pose.orientation.x
    y = odom.pose.pose.orientation.y
    z = odom.pose.pose.orientation.z
    w = odom.pose.pose.orientation.w
    theta = atan2(2*(w*z+x*y),1-2*(y**2+z**2))
    car_sub_record.append([odom.pose.pose.position.x+1, odom.pose.pose.position.y+1, theta ,odom.twist.twist.linear.x, odom.twist.twist.linear.y, odom.twist.twist.angular.z])

#read demonstration data
def readTxt(path):
    data = np.loadtxt(path)
    xy_path = data[:,0:2]
    xy_dot_path = np.concatenate((data[:,3].reshape(-1,1)*np.cos(data[:,2].reshape(-1,1)),data[:,3].reshape(-1,1)*np.sin(data[:,2]).reshape(-1,1)),axis=1)
    xy_theta_path = data[:,0:3]
    xy_theta_dot_path = np.concatenate((xy_dot_path,data[:,5].reshape(-1,1)),axis=1)

    return xy_path,xy_dot_path,xy_theta_path,xy_theta_dot_path

#given n*m numpy data,return the data filtered
def mean_filter(data,windows_size = 4):
    rows, cols = data.shape
    filtedData = np.zeros((rows,cols))
    windows = np.zeros((windows_size,cols))
    for i in range(windows_size):
        windows[i,:] = data[0,:]

    for i in range(rows):
        windows[i%windows_size,:] = data[i,:]
        filtedData[i,:] = np.sum(windows,axis=0)/windows_size
    return filtedData

#given n*m numpy data,return n/times * m numpy data
def down_sample(data,times):
    rows, cols = data.shape
    down_data = np.zeros((int(rows//times),cols))
    for i in range(int(rows//times)):
        down_data[i,:] = data[i*times,:]
    return down_data

#Gaussian Process Regression implementation
class MyGPR():
    def __init__(self,data_in,data_out):
        self.data_in = data_in
        self.data_out = data_out
        #kernel1 = 1 * RBF(length_scale=1e-3, length_scale_bounds=(1e-8, 1e2))+1*WhiteKernel(noise_level = 1e-7,noise_level_bounds=(1e-15,1e-2))
        kernel1 = 1 * RBF(length_scale=1e-3, length_scale_bounds=(1e-8, 1e2))+0.1*WhiteKernel(noise_level = 1e-7,noise_level_bounds=(1e-15,1e-2))
        self.gpr1 = GaussianProcessRegressor(kernel=kernel1,random_state=0,n_restarts_optimizer=5,normalize_y=False)

        print('fitting')
        self.gpr1.fit(data_in,data_out)
        print('fitted')

    def get_diff_varian_matrix(self,xmin,xmax,ymin,ymax,demonstrate_point,step=400):
        self.xmin = xmin
        self.xmax = xmax
        self.ymin = ymin
        self.ymax = ymax
        self.step = step
        self.demonstrate_point = demonstrate_point

        x_step = (xmax - xmin)/step
        y_step = (ymax - ymin)/step
        xx = np.arange(xmin,xmax,x_step)
        yy = np.arange(ymin,ymax,y_step)
        xxx,yyy = np.meshgrid(xx,yy)
        predict_point = np.concatenate((xxx.reshape(-1,1),yyy.reshape(-1,1)),1)
        result,std = self.gpr1.predict(predict_point,return_std=True)
        self.diff_std = np.zeros((step,step,2))
        std_matrix = std[:].reshape(step,step)
        self.std_matrix = std_matrix
        for i in range(step-1):
            for j in range(step-1):
                self.diff_std[i,j,0] = (std_matrix[i,j+1] - std_matrix[i,j])
                self.diff_std[i,j,1] = (std_matrix[i+1,j] - std_matrix[i,j])
        self.diff_std[:,:,0] /= x_step
        self.diff_std[:,:,1] /= y_step
        self.long_dis_stable = (np.max(abs(self.diff_std)))/4

        return self.diff_std
    def get_next_step_jens(self,predict_point):
        result,_ = self.gpr1.predict(predict_point,return_std=True)
        remap_j = int((predict_point[0,0]-self.xmin)/(self.xmax-self.xmin)*self.step)
        remap_i = int((predict_point[0,1]-self.ymin)/(self.ymax-self.ymin)*self.step)

        return np.array((result[0,0] - (self.diff_std[remap_i,remap_j,0])*3 ,  result[0,1] - (self.diff_std[remap_i,remap_j,1])*3)).reshape(1,-1)

    def get_next_step(self,predict_point):
        result,_ = self.gpr1.predict(predict_point,return_std=True)
        remap_j = int((predict_point[0,0]-self.xmin)/(self.xmax-self.xmin)*self.step)
        remap_i = int((predict_point[0,1]-self.ymin)/(self.ymax-self.ymin)*self.step)
        distance = np.min(np.sqrt((self.demonstrate_point[:,0]-predict_point[0,0])**2+(self.demonstrate_point[:,1]-predict_point[0,1])**2))
        min_index = np.argmin(np.sqrt((self.demonstrate_point[:,0]-predict_point[0,0])**2+(self.demonstrate_point[:,1]-predict_point[0,1])**2))
        long_dis_stable_direction = -(self.demonstrate_point[min_index] - predict_point)/np.linalg.norm(self.demonstrate_point[min_index] - predict_point)

        if distance > 2:
            distance = 5.4
        else:
            distance *= 2.7
        self.dis_cof = 0.05/np.max(self.diff_std)
        distance *= self.dis_cof
        return np.array((result[0,0] - (self.diff_std[remap_i,remap_j,0]+long_dis_stable_direction[0,0]*self.long_dis_stable)*distance ,  result[0,1] - (self.diff_std[remap_i,remap_j,1]+long_dis_stable_direction[0,1]*self.long_dis_stable)*distance)).reshape(1,-1)
    def get_varian(self,predict_point):
        #result,_ = self.gpr1.predict(predict_point,return_std=True)
        remap_j = int((predict_point[0,0]-self.xmin)/(self.xmax-self.xmin)*self.step)
        remap_i = int((predict_point[0,1]-self.ymin)/(self.ymax-self.ymin)*self.step)
        distance = np.min(np.sqrt((self.demonstrate_point[:,0]-predict_point[0,0])**2+(self.demonstrate_point[:,1]-predict_point[0,1])**2))
        min_index = np.argmin(np.sqrt((self.demonstrate_point[:,0]-predict_point[0,0])**2+(self.demonstrate_point[:,1]-predict_point[0,1])**2))
        long_dis_stable_direction = -(self.demonstrate_point[min_index] - predict_point)/np.linalg.norm(self.demonstrate_point[min_index] - predict_point)

        if distance > 2:
            distance = 5.4
        else:
            distance *= 2.7
        self.dis_cof = 0.07/np.max(self.diff_std)
        distance *= self.dis_cof
        return np.array((- (self.diff_std[remap_i,remap_j,0]+long_dis_stable_direction[0,0]*self.long_dis_stable)*distance ,   - (self.diff_std[remap_i,remap_j,1]+long_dis_stable_direction[0,1]*self.long_dis_stable)*distance)).reshape(1,-1)

    def plot_field1(self,xmin,xmax,ymin,ymax,demonstrate_point,step=75,field_density = 7):
        diff_std = self.get_diff_varian_matrix(xmin,xmax,ymin,ymax,demonstrate_point,step=step)

        x_step = (xmax - xmin)/step
        y_step = (ymax - ymin)/step
        xx = np.arange(xmin,xmax,x_step)
        yy = np.arange(ymin,ymax,y_step)
        xxx1,yyy1 = np.meshgrid(xx,yy)
        predict_point1 = np.concatenate((xxx1.reshape(-1,1),yyy1.reshape(-1,1)),1)

        varian_res = np.zeros((step*step))
        for i in range(predict_point1.shape[0]):
            varian_res[i]=np.linalg.norm((self.get_varian(predict_point1[i].reshape(1,-1))).ravel())
            print(i)
        varian_res = varian_res.reshape(step,step)
        vmax = np.max(varian_res)/0.8
        vmin = np.min(varian_res)/8

        #plt.figure()
        #plt.subplot(234)
        #plt.pcolormesh(xxx1,yyy1,self.std_matrix,cmap='Blues',alpha=0.5)
        # plt.xlim((xmin,xmax))
        # plt.ylim((ymin,ymax))

        x_step = (xmax - xmin)/field_density
        y_step = (ymax - ymin)/field_density
        xx = np.arange(xmin+x_step/2,xmax+x_step/2,x_step)
        yy = np.arange(ymin+y_step/2,ymax+y_step/2,y_step)
        xxx,yyy = np.meshgrid(xx,yy)
        predict_point = np.concatenate((xxx.reshape(-1,1),yyy.reshape(-1,1)),1)
        result,std = self.gpr1.predict(predict_point,return_std=True)

        #plt.subplot(223)
        #plt.scatter(predict_point[:,0],predict_point[:,1])
        half = int(demonstrate_point.shape[0]/2)
        color = 'yellow'
        color1= 'seashell'
        # for i in range(field_density**2):
        #     plt.arrow(predict_point[i,0],predict_point[i,1],result[i,0],result[i,1],head_width=0.05,color=color1)#seashell

        # plt.plot(demonstrate_point[:half-5,0],demonstrate_point[:half-5,1],linewidth=4,color=color,alpha=0.5)
        # plt.plot(demonstrate_point[half+5:,0],demonstrate_point[half+5:,1],linewidth=4,color=color,alpha=0.5)
        plt.subplot(143)
        plt.pcolormesh(xxx1[:-1,:-1],yyy1[:-1,:-1],np.sqrt(abs(diff_std[:-1,:-1,0])**2+abs(diff_std[:-1,:-1,1])**2)*6,cmap=light_jet,alpha=1,shading='gouraud')
        for i in range(field_density**2):
            remap_i = int(i//field_density) * int(step//field_density)
            remap_j = int(i%field_density) * int(step//field_density)
            res = self.get_next_step_jens(predict_point[i,:].reshape(1,-1))
            res*=2
            plt.arrow(predict_point[i,0],predict_point[i,1], res[0,0], res[0,1],head_width=0.1,color='purple')

        plt.plot(demonstrate_point[:half-5,0],demonstrate_point[:half-5,1],linewidth=4,color=color,alpha=0.7)
        plt.plot(demonstrate_point[half+5:,0],demonstrate_point[half+5:,1],linewidth=4,color=color,alpha=0.7)
        fontdict={'family':'Times New Roman','size':14}
        plt.xticks(fontproperties='Times New Roman',size=14)
        plt.yticks([],fontproperties='Times New Roman',size=14)
        plt.title('(c)',position=(0.07,0.85))
        plt.subplot(144)
        plt.pcolormesh(xxx1,yyy1,varian_res,cmap=light_jet,shading='gouraud')
        for i in range(field_density**2):
            remap_i = int(i//field_density) * int(step//field_density)
            remap_j = int(i%field_density) * int(step//field_density)
            res = self.get_next_step(predict_point[i,:].reshape(1,-1))
            res*=2
            plt.arrow(predict_point[i,0],predict_point[i,1], res[0,0], res[0,1],head_width=0.1,color='purple')
        plt.plot(demonstrate_point[:half-5,0],demonstrate_point[:half-5,1],linewidth=4,color=color,alpha=0.7)
        plt.plot(demonstrate_point[half+5:,0],demonstrate_point[half+5:,1],linewidth=4,color=color,alpha=0.7)
        plt.xlabel('x/m',fontdict=fontdict)
        plt.xticks(fontproperties='Times New Roman',size=14)
        plt.yticks([],fontproperties='Times New Roman',size=14)
        plt.title('(d)',position=(0.07,0.85))
    def plot_field(self,xmin,xmax,ymin,ymax,demonstrate_point,step=75,field_density = 7):
        diff_std = self.get_diff_varian_matrix(xmin,xmax,ymin,ymax,demonstrate_point,step=step)

        x_step = (xmax - xmin)/step
        y_step = (ymax - ymin)/step
        xx = np.arange(xmin,xmax,x_step)
        #xx = xx + x_step
        yy = np.arange(ymin,ymax,y_step)

        #yy = yy + y_step
        xxx1,yyy1 = np.meshgrid(xx,yy)
        predict_point1 = np.concatenate((xxx1.reshape(-1,1),yyy1.reshape(-1,1)),1)

        varian_res = np.zeros((step*step))
        for i in range(predict_point1.shape[0]):
            varian_res[i]=np.linalg.norm((self.get_varian(predict_point1[i].reshape(1,-1))).ravel())
            print(i)
        varian_res = varian_res.reshape(step,step)
        vmax = np.max(varian_res)/1.1
        vmin = np.min(varian_res)/10

        x_step = (xmax - xmin)/field_density
        y_step = (ymax - ymin)/field_density
        xx = np.arange(xmin+x_step/2,xmax+x_step/2,x_step)
        yy = np.arange(ymin+y_step/2,ymax+y_step/2,y_step)
        xxx,yyy = np.meshgrid(xx,yy)
        predict_point = np.concatenate((xxx.reshape(-1,1),yyy.reshape(-1,1)),1)
        result,std = self.gpr1.predict(predict_point,return_std=True)

        p1=plt.figure(figsize=(4,3))
        #fig,ax = plt.subplots(subplot_kw={"projection":"3d"})
        #surf = ax.plot_surface(xxx1,yyy1,self.std_matrix,cmap=cm.coolwarm,linewidth=0,antialiased=False)
        #plt.scatter(predict_point[:,0],predict_point[:,1])
        light_jet = cmap_map(lambda x: x/2 +0.5, matplotlib.cm.coolwarm)

        #print(predict_point)
        for i in range(field_density**2):
            plt.arrow(predict_point[i,0],predict_point[i,1],result[i,0]*2,result[i,1]*2,head_width=0.1,color='purple')#seashell
        plt.plot(demonstrate_point[:,0],demonstrate_point[:,1],linewidth=4,color='yellow',alpha=0.7)
        fontdict={'family':'Times New Roman','size':14}
        #plt.xlabel('x/m',fontdict=fontdict,labelpad=0)
        plt.ylabel('y/m',fontdict=fontdict,labelpad=0)
        xx = [-2,0,2,4]
        yy = [-2,0,2,4]

        plt.xticks(xx,fontproperties='Times New Roman',size=14)
        plt.yticks(yy,fontproperties='Times New Roman',size=14)
        pc=plt.pcolormesh(xxx1,yyy1,self.std_matrix,cmap=light_jet,alpha=1,shading='gouraud')
        p1.subplots_adjust(left=0.15,bottom=0.25,wspace=0.02)
        cax=p1.add_axes([0.18,0.1,0.7,0.03])

        c=p1.colorbar(pc,cax=cax,orientation='horizontal')
        c.ax.tick_params(labelsize=14)
        c.ax.set_yticklabels(['0','0.1'],family='Time New Roman')        #plt.contourf(xxx1,yyy1,self.std_matrix)
        plt.savefig('IMAGE/tttn.png',bbox_inches='tight',dpi=200)
        p=plt.figure(figsize=(12,3))
        matplotlib.colors.Normalize()

        plt.xlim((xmin,xmax))
        plt.ylim((ymin,ymax))
        plt.subplot(141)
        print(np.sqrt(abs(diff_std[:-1,:-1,0])**2+abs(diff_std[:-1,:-1,1])**2).shape)
        print(xxx1.shape)
        plt.pcolormesh(xxx1[:-1,:-1],yyy1[:-1,:-1],np.sqrt(abs(diff_std[:-1,:-1,0])**2+abs(diff_std[:-1,:-1,1])**2)*6,cmap=light_jet,shading='gouraud')
        for i in range(field_density**2):
            remap_i = int(i//field_density) * int(step//field_density)
            remap_j = int(i%field_density) * int(step//field_density)
            res = self.get_next_step_jens(predict_point[i,:].reshape(1,-1))
            res*=2
            plt.arrow(predict_point[i,0],predict_point[i,1], res[0,0], res[0,1],head_width=0.1,color='purple')
        plt.plot(demonstrate_point[:,0],demonstrate_point[:,1],linewidth=4,color='yellow',alpha=0.7)
        fontdict={'family':'Times New Roman','size':14}
        plt.ylabel('y/m',fontdict=fontdict)
        plt.xlabel('x/m',fontdict=fontdict)

        plt.xticks(fontproperties='Times New Roman',size=14)
        plt.yticks(fontproperties='Times New Roman',size=14)
        plt.title('(a)',position=(0.07,0.85))
        plt.subplot(142)
        pc = plt.pcolormesh(xxx1,yyy1,varian_res,cmap=light_jet,alpha=1,shading='gouraud')
        for i in range(field_density**2):
            remap_i = int(i//field_density) * int(step//field_density)
            remap_j = int(i%field_density) * int(step//field_density)
            res = self.get_next_step(predict_point[i,:].reshape(1,-1))
            res*=2
            plt.arrow(predict_point[i,0],predict_point[i,1], res[0,0], res[0,1],head_width=0.1,color='purple')
        plt.plot(demonstrate_point[:,0],demonstrate_point[:,1],linewidth=4,color='yellow',alpha=0.7)
        fontdict={'family':'Times New Roman','size':18}
        plt.xticks(fontproperties='Times New Roman',size=14)
        plt.yticks([],fontproperties='Times New Roman',size=14)
        plt.title('(b)',position=(0.07,0.85))
        p.subplots_adjust(left=0.07,right=0.96,bottom=0.25,wspace=0.02)
        cax=p.add_axes([0.2,0.1,0.6,0.05])

        c=p.colorbar(pc,cax=cax,orientation='horizontal')
        c.ax.tick_params(labelsize=14)
        c.ax.set_yticklabels(['0','0.1'],family='Time New Roman')
        return p
    def find_nearest_point(self,point):
        distance = np.linalg.norm((self.data_out-point),axis=1)
        print('arm',np.argmin(distance))
        return self.data_in[np.argmin(distance)]

#Bezier Curve
class BEZIER():
    def __init__(self):
        self.nSection=10000#根据经验，1米的路程分大于10000000份（7个零）
        self.step_previous=0
        self.f_t_x=[]
        self.f_t_y=[]
        self.length_of_t =[0]* self.nSection
        self.bezier_total_length = 0
    def bezier_tracjectory_generator(self, p0x,p0y,initA,p3x,p3y,endA):
        L=sqrt((p0x-p3x)**2+(p0y-p3y)**2)
        section=L/2.5
        p1x=p0x+section*cos(initA)
        p1y=p0y+section*sin(initA)
        p2x=p3x-section*cos(endA)
        p2y=p3y-section*sin(endA)
        n=self.nSection
        self.f_t_x=[]
        self.f_t_y=[]
        for ii in range(0,n+1):
            t=ii/n
            bezier=[p3x*t**3 - p0x*(t - 1)**3 + 3*p1x*t*(t - 1)**2 - p2x*t**2*(3*t - 3),
                    p3y*t**3 - p0y*(t - 1)**3 + 3*p1y*t*(t - 1)**2 - p2y*t**2*(3*t - 3)]
            self.f_t_x.append(bezier[0])
            self.f_t_y.append(bezier[1])
        bezier_total_length=0.0
        for ii in range(0,n):
            bezier_total_length=bezier_total_length+sqrt((self.f_t_x[ii+1]-self.f_t_x[ii])**2+(self.f_t_y[ii+1]-self.f_t_y[ii])**2)
            self.length_of_t[ii] = bezier_total_length
        self.bezier_total_length = bezier_total_length
        return p1x,p1y,p2x,p2y, bezier_total_length

    def find_nearest_t_of_s(self,s):
        step = self.step_previous
        while step < self.nSection and self.length_of_t[step] < s:
            step += 1
        self.step_previous = step
        return step

    def bezier_tangent_at_t(self, t,p0x,p0y,p1x,p1y,p2x,p2y,p3x,p3y):
        tangent_x=3*p1x*(t - 1)**2 - 3*p0x*(t - 1)**2 - 3*p2x*t**2 + 3*p3x*t**2 + 3*p1x*t*(2*t - 2) - 2*p2x*t*(3*t - 3)
        tangent_y=3*p1y*(t - 1)**2 - 3*p0y*(t - 1)**2 - 3*p2y*t**2 + 3*p3y*t**2 + 3*p1y*t*(2*t - 2) - 2*p2y*t*(3*t - 3)
        length=sqrt(tangent_x**2+tangent_y**2)
        norm_direction=[tangent_x/length, tangent_y/length]
        angle=atan2(norm_direction[1],norm_direction[0])
        return norm_direction, angle
    def plot_bezier(self):
        plt.plot(self.f_t_x,self.f_t_y)

    def get_result(self,p0x,p0y,initA,p3x,p3y,endA,step_size=0.002):
        p1x,p1y,p2x,p2y,total_length = self.bezier_tracjectory_generator(p0x,p0y,initA,p3x,p3y,endA)
        step = int(total_length//step_size)+1
        angle_record = []
        w_record = []
        t_record = []
        for i in range(step):
            t = self.find_nearest_t_of_s(step_size*i)
            t_record.append(t)
            _,angle = self.bezier_tangent_at_t(t/self.nSection,p0x,p0y,p1x,p1y,p2x,p2y,p3x,p3y)
            angle_record.append(angle)

        for i in range(len(angle_record)-1):
            w_record.append(angle_record[i+1] - angle_record[i])

        return w_record

def time_optimal_for_1D_tracking(x1_tf, x2_tf, x1_t0, x2_t0, abs_acc):
    string=''
    if abs(x1_tf-x1_t0)+abs(x2_tf-x2_t0)<0.001:
        abs_acc=0.001
    if x2_t0>x2_tf:#,走abs_acc<0
        string=string+'x2_t0>x2_tf  |'
        t_minus=(x2_t0-x2_tf)/(-abs_acc)
        temp=0.5*(-abs_acc)*t_minus**2+x2_t0*t_minus+x1_tf
        if x1_t0<temp:
            acc=abs_acc
            string=string+'x1_t0<temp  |加速'+str(acc)
        else:#x1_t0>=temp
            acc=-abs_acc
            string=string+'x1_t0>=temp  |减速'+str(acc)
    elif x2_t0<x2_tf:#,走abs_acc>0
        string=string+'x2_t0<x2_tf  |'
        t_plus=(x2_t0-x2_tf)/abs_acc
        temp=0.5*abs_acc*t_plus**2+x2_t0*t_plus+x1_tf
        if x1_t0<=temp:
            acc=abs_acc
            string=string+'x1_t0<=temp  |加速'+str(acc)

        else:
            acc=-abs_acc
            string=string+'x1_t0>temp  |减速'+str(acc)
    else:#x2_t0==x2_tf
        string=string+'x2_t0==x2_tf  |'
        if x1_t0<x1_tf:
            acc=abs_acc
            string=string+'x1_t0<x1_tf  |加速'+str(acc)

        elif x1_t0>x1_tf:
            acc=-abs_acc
            string=string+'x1_t0>x1_tf  |减速'+str(acc)

        else:#x1_t0==x1_tf
            acc=0.0
            string=string+'x1_t0==x1_tf  |不变'+str(acc)
    return acc,string

if __name__ == "__main__":
    #init ros node
    rospy.init_node("UR5Control")
    ur_pub = rospy.Publisher ("/arm_controller/command", JointTrajectory,queue_size=0)
    base_pub = rospy.Publisher ("/mobile_base_controller/cmd_vel", Twist,queue_size=0)
    ur_sub = rospy.Subscriber("/arm_controller/state",JointTrajectoryControllerState,ur_sub_callback)
    base_sub = rospy.Subscriber("/mobile_base_controller/odom",Odometry,car_sub_callback)

    #read data and pre-process
    xy_path,xy_dot_path,xy_theta_path,xy_theta_dot_path = readTxt('data/car_xytheta_dot_record1.txt')
    down_times = int(xy_path.shape[0]//40 + 1)
    print('down_times',down_times)
    xy_dot_path_filtered = mean_filter(xy_dot_path)
    xy_dot_path_filtered_down = down_sample(xy_dot_path,down_times)
    xy_path_down = down_sample(xy_path,down_times)

    gpr = MyGPR(xy_path_down,xy_dot_path_filtered_down)
    p=gpr.plot_field(-2,5,-2,5,xy_path)

    #read data and pre-process
    xy_path1,xy_dot_path1,xy_theta_path1,xy_theta_dot_path1 = readTxt('data/car_xytheta_dot_record4.txt')
    xy_path2,xy_dot_path2,xy_theta_path2,xy_theta_dot_path2 = readTxt('data/car_xytheta_dot_record5.txt')
    xy_path = np.vstack((xy_path1,xy_path2))
    xy_dot_path = np.vstack((xy_dot_path1,xy_dot_path2))
    down_times = int(xy_path.shape[0]//40 + 1)
    print('down_times',down_times)
    xy_dot_path_filtered = mean_filter(xy_dot_path)
    xy_dot_path_filtered_down = down_sample(xy_dot_path,down_times)
    xy_path_down = down_sample(xy_path,down_times)

    gpr = MyGPR(xy_path_down,xy_dot_path_filtered_down)
    gpr.plot_field1(-2,5,-2,5,xy_path)

    p.savefig('IMAGE/ttt1.png',bbox_inches='tight',dpi=200)
    #plt.show()

    #for gazebo ros controller
    msg = JointTrajectory()
    vel = Twist()
    msg.joint_names = ['shoulder_pan_joint', 'shoulder_lift_joint', 'elbow_joint',
               'wrist_1_joint', 'wrist_2_joint', 'wrist_3_joint']
    point = JointTrajectoryPoint()

    step = 0
    dt = 0.1
    bz_flag = False
    car_angular_vel = 0.48
    car_angular_acc = 0.5
    vel.linear.x = 0
    base_pub.publish(vel)
    time.sleep(0.5)

    #limits
    current_x,current_y,current_theta = car_sub_record[-1][0],car_sub_record[-1][1],car_sub_record[-1][2]
    car_sub_record.clear()
    ur_sub_record.clear()
    plt.figure()
    while True:

        #if the car is near the target, break the loop
        if np.sqrt((current_x-2.9)**2+(current_y-2.9)**2) < 0.2:
            break
        t0 = time.time()

        #disturb
        if step < 20:
            vel.linear.x = 0.5
            vel.linear.z = 0.5
            base_pub.publish(vel)
            time.sleep(dt)
            step += 1
            continue
        result = gpr.get_next_step(np.array((current_x,current_y)).reshape(1,-1))

        target_angle = np.arctan2(result[0,1],result[0,0])
        delta_theta = np.arctan2(result[0,1],result[0,0])-current_theta
        w0 = car_sub_record[-1][5]

        #if delta angle is small, run directly.
        if np.abs(delta_theta) > car_angular_vel*dt:
            bz_flag = True

        #if the angle is large, turn to the disired angle first
        if bz_flag ==True:
            bz_flag = False
            vel.linear.x = np.sqrt(result[0,0]**2+result[0,1]**2)*0.0
            acc,_ = time_optimal_for_1D_tracking(delta_theta, 0, 0, w0, car_angular_acc)
            vel.linear.x = 0
            print('Bezier','acc:',acc,'delta:',delta_theta,'w0',w0)
        else:
            acc,_ = time_optimal_for_1D_tracking(delta_theta, delta_theta*10, 0, w0, car_angular_acc)
            vel.linear.x = np.sqrt(result[0,0]**2+result[0,1]**2)*1
        vel.angular.z = w0+acc*dt

        base_pub.publish(vel)
        time.sleep(dt)
        current_x,current_y,current_theta = car_sub_record[-1][0],car_sub_record[-1][1],car_sub_record[-1][2]
    print('eeeeeeeeeee')
    #stop the car
    vel.linear.x = 0
    vel.angular.z = 0
    base_pub.publish(vel)

    plt.plot([x[0] for x in car_sub_record],[x[1] for x in car_sub_record])
    plt.plot(xy_path[:,0],xy_path[:,1])

    #np.savetxt('data/car_xytheta_dot_record3.txt',np.array(car_sub_record)[:,:])

    plt.show()
    plt.pause(0)


from collections import deque

n = int(input())
graph = []
max = 0
# 상 하 좌 우
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

for i in range(n):
    graph.append(list(map(int, input().split())))
    for j in range(n):
        if graph[i][j] > max: # 그래프에 입력된 값 중 최댓값 찾음
            max = graph[i][j]

def bfs(xPos, yPos, value, visited):
    q = deque()
    q.append((xPos, yPos))
    visited[xPos][yPos] = 1
    while q:
        x, y = q.popleft()
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < n and 0 <= ny < n: # 그래프의 영역을 벗어나지 않고
                if graph[nx][ny] > value and visited[nx][ny] == 0: # 방문하지 않은 지역이고 잠기지 않은지역이면 bfs수행
                    visited[nx][ny] = 1
                    q.append((nx, ny))

maximum = 0

#비가 안오는 0부터 최댓값 전까지 조회(최댓값은 모두 잠기므로 안전영역이 무조건 0임 조회할 필요 없음)
for a in range(max):
    visited = [[0] * n for _ in range(n)] # 비가 오는 매 높이 마다 bfs를 수행해야하므로 매번 visited를 초기화
    ans = 0
    for i in range(n):
        for j in range(n):
            if graph[i][j] > a and visited[i][j] == 0: # 방문하지 않고 비가 온 곳보다 높은 지역이면 bfs 수행
                bfs(i, j, a, visited)
                ans += 1
    if maximum < ans: # 안전영역의 최댓값을 구함
        maximum = ans
print(maximum)


#!/usr/bin/env python3
from PIL import Image
import sys

# Check the number of command-line arguments
if len(sys.argv) != 2:
    print ("Usage: python3 asciicode.py <IMAGE>\n   e.g., python3 asciicode.py image.png")
    sys.exit(1)

# Define the allowed image formats
allowed_formats = ["PNG", "JPG", "JPEG", "BMP"]

# Get the image file path from the command-line argument
img_path = sys.argv[1]

# Check if the file format is allowed
try:
    with Image.open(img_path) as img:
        # Get the format of the image (e.g., "PNG", "JPEG")
        image_format = img.format
    if image_format and image_format.upper() in allowed_formats:
        print(f"Processing {image_format} image...")
    else:
        print(f"Error: Only {', '.join(allowed_formats)} image formats are allowed.")
        sys.exit(1)
except Exception as e:
    print("Error: Could not open the image file.")
    sys.exit(1)

# Open the image
image = Image.open(img_path)

# Convert the image to grayscale
image_grayscale = image.convert("L")
print ("Break Point")

# Define the ASCII characters for conversion
ascii_chars = "@%#*+=-:. "

# Resize the image based on the number of characters you want to display per line
largeur, hauteur = image_grayscale.size
largeur_cible = 100
facteur_redimensionnement = largeur_cible / largeur
hauteur_cible = int(hauteur * facteur_redimensionnement)
image_redimensionnee = image_grayscale.resize((largeur_cible, hauteur_cible))

# Generate the ASCII image
pixels = image_redimensionnee.getdata()
ascii_image = ""
for pixel_value in pixels:
    if 0 <= pixel_value // 25 < len(ascii_chars):
        ascii_image += ascii_chars[pixel_value // 25]
    else:
        ascii_image += '?'

# Split the ASCII image into lines
lines_ascii = [ascii_image[i:i+largeur_cible] for i in range(0, len(ascii_image), largeur_cible)]

# Write the ASCII image to a text file
with open("ascii_image.txt", "w") as fichier:
    for line in lines_ascii:
        fichier.write(line + "\n")

print("The ASCII image has been saved to the ascii_image.txt file.")


import json
from collections import defaultdict

recursivedict = lambda: defaultdict(recursivedict)
emotions = recursivedict()
emotions = {
  "happy":{
    "playful":["aroused","cheeky"],
    "content":["free","joyful"],
    "interested":["curious","inquisitive"],
    "proud":["successful","confident"],
    "accepted":["respected","valued"],
    "powerful":["courageous","creative"],
    "peaceful":["loving","thankful"],
    "trusting":["sensitive","intimate"],
    "optimistic":["hopeful","inspired"]
  },
  "surprised":{
    "startled":["shocked","dismayed"],
    "confused":["disillusioned","perplexed"],
    "amazed":["astonished","awe"],
    "excited":["eager","energetic"]
  },
  "bad":{
    "bored":["indifferent","apathetic"],
    "busy":["pressured","rushed"],
    "stressed":["overwhelmed","out of control"],
    "tired":["sleepy","unfocussed"]
  },
  "fearful":{
    "scared":["helpless","frightened"],
    "anxious":["ooverwhelmed","worried"],
    "insecure":["inadequate","inferior"],
    "weak":["worthless","insignificant"],
    "rejected":["excluded","persecuted"],
    "threatened":["nervous","exposed"]
  },
  "angry":{
    "let down":["betrayed","resentful"],
    "humiliated":["disrespected","ridiculed"],
    "bitter":["indignant","vioted"],
    "mad":["furious","jealous"],
    "aggressive":["provoked","hostile"],
    "frustrated":["infuriated","annoyed"],
    "distant":["withdrawn","numb"],
    "critical":["sceptical","dismissive"]
  },
  "disgusted":{
    "dissaproving":["judgemental","embarrassed"],
    "disappointed":["appalled","revolted"],
    "awful":["nauseated","detestable"],
    "repelled":["horrified","hesitant"]
  },
  "sad":{
    "hurt":["embarrassed","dissapointed"],
    "depressed":["inferior","empty"],
    "guilty":["remorseful","ashamed"],
    "despair":["grief","powerless"],
    "vulnerable":["victimised","fragile"],
    "lonely":["isolated","abandoned"]
  }
}

print(json.dumps(emotions,indent=4,sort_keys=True))


# A module to parse commands embedded into the tests themselfes
from functools import partial
from steps     import create_step_append_flags
import logging
import re
import sys

_LOGGER = logging.getLogger("sisyphus")

def _check_regex(result, regex, txt, count, expected_result):
    realcount = 0
    for line in result.stdout.splitlines():
        if regex.search(line):
            realcount += 1
            if realcount > count:
                break
    if not expected_result and realcount > 0:
        result.error = "!check '%s' failed" % (txt,)
    elif expected_result and count == 0 and realcount == 0:
        result.error = "check '%s' failed" % (txt,)
    elif count > 0 and realcount != count:
        assert expected_result
        result.error = "check[%s] '%s' failed" % (count, txt,)

def _embedded_add_check(regex_string, count_arg, flag):
    """create a regex check (for assembler output)"""
    c = 0
    if count_arg:
        if flag:
            c = int(count_arg[1:-1])
        else:
            raise Exception("!check cannot be used with an argument")
    regex = re.compile(regex_string)
    return partial(_check_regex, regex=regex, txt=regex_string, count=c, expected_result=flag)

def _parse_embedded_command(cmd, flags, asm_checks):
    """parse one /*$ $*/ embedded command"""
    cmdre = re.compile("(!?check(\[[0-9]+\])?|cflags|ldflags)(.*)")
    m = cmdre.match(cmd)
    if m:
        base = m.group(1)
        if m.group(2):
            base = base[0:-len(m.group(2))]
        if m.group(3):
            arg = m.group(3).strip()

        if base == "check":
            check = _embedded_add_check(arg, m.group(2), True)
            asm_checks.append(check)
        elif base == "!check":
            check = _embedded_add_check(arg, m.group(2), False)
            asm_checks.append(check)
        elif base == "cflags":
            flags["cflags"] += " %s" % (arg,)
        elif base == "ldflags":
            flags["ldflags"] += " %s" % (arg,)
        else:
            _LOGGER.error("unsupported embedded command %s" % base)
    else:
        # treat as a cflag option
        flags["cflags"] += " %s" % (cmd.strip(), )

def parse_embedded_commands(test, filename, asm_step_factory=None):
    """Parse a given file for embedded test commands (/*$ ... $*/ sequences).
    Modifies the test and adds an additional assembly generation step with the
    asm_step callback if necessary."""
    cmd_regex  = re.compile("/\\*\\$ (.+) \\$\\*/")
    asm_checks = []
    flags      = dict(cflags="", ldflags="")
    for line in open(filename, "rb"):
        m = cmd_regex.match(line)
        if m:
            cmd = m.group(1)
            _LOGGER.info("%s: embedded cmd %s\n" % (filename, cmd))
            _parse_embedded_command(cmd, flags, asm_checks)
    if len(asm_checks) > 0:
        step = asm_step_factory()
        step.add_checks(asm_checks)
    if flags["cflags"] != "":
        test.prepend_step("embedded_cflags", create_step_append_flags(cflags=flags["cflags"]))
    if flags["ldflags"] != "":
        test.prepend_step("embedded_ldflags", create_step_append_flags(ldflags=flags["ldflags"]))