from django import forms
from lk.models import *

class SelectStock(forms.Form):
	all_stock = []
	stock = forms.ChoiceField(widget=forms.Select, choices=all_stock)
	stock.widget.attrs.update({'class': 'custom-select custom-select-sm', 'id': 'all_stock'})

class StockForm(forms.ModelForm):
	class Meta:
		model = Stock
		fields = ['title', 'country', 'region', 'city', 'street', 'house']

		widgets = {
			'title': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'По умолчанию: Склад № ... '}),
			'country': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Например: Российская Федерация'}),
			'region': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Например: Удмуртская Республика'}),
			'city': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Например: Ижевск'}),
			'street': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Например: Воткинское шоссе'}),
			'house': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '13'})
		}
	def save(self):
		new_stock = Stock.objects.create(
			title=self.cleaned_data['title'],
			value=self.cleaned_data['value'],
			country=self.cleaned_data['country'],
			region=self.cleaned_data['region'],
			city=self.cleaned_data['city'],
			street=self.cleaned_data['street'],
			house=self.cleaned_data['house'],
			account=self.cleaned_data['account'],
		)

		return new_stock


import requests

class CMCTools:

    def __init__(self):
        self.TICKER_URL = "https://api.coinmarketcap.com/v1/ticker/"
        self.GLOBAL_URL = "https://api.coinmarketcap.com/v1/global/"
        self.coin_name_info_list = []

        self.headers = { "User-agent": "cmcools - A lightweight coinmarketcap wrapper. https://github.com/DustinStuff/cmctools" }
        self.session = requests.Session()
        self.session.headers.update(self.headers)

    def ticker(self, coin_id="", limit="", start="", convert=""):
        """
        Gets information from the coinmarketcap API's ticker endpoint. Only valid coinmarketcap coin IDs can
        be used, however you can use a symbol or name if you pass it through name_info([symbol or name])['id'].

        Example return JSON:
        [
            {
                "id": "bitcoin",
                "name": "Bitcoin",
                "symbol": "BTC",
                "rank": "1",
                "price_usd": "8026.4",
                "price_btc": "1.0",
                "24h_volume_usd": "8925990000.0",
                "market_cap_usd": "135277547580",
                "available_supply": "16854075.0",
                "total_supply": "16854075.0",
                "max_supply": "21000000.0",
                "percent_change_1h": "-0.53",
                "percent_change_24h": "-0.73",
                "percent_change_7d": "-7.88",
                "last_updated": "1518151167"
        If convert is used, it also has these entries (using ETH as an example):
                "price_eth": "10.0965816895",
                "24h_volume_eth": "11228195.3546",
                "market_cap_eth": "170168545.0"
            },
            { ... }
        ]
        :param coin_id: A valid coinmarketcap coin id.
        :param limit: Returns a maximum of [limit] results. Defaults to 100, use 0 to return all results.
        :param start: Returns results from rank [start].
        :param convert: Returns price, 24h volume, and market cap in another specified currency.
        :return: /ticker/endpoint JSON data.
        """
        url = "{}{}/".format(self.TICKER_URL, coin_id) if coin_id else self.TICKER_URL
        params = {"limit": limit, "start": start, "convert": convert}
        return self.session.get(url, params=params).json()

    def global_data(self, convert=""):
        """
        Gets information from the coinmarketcap API's global endpoint.

        Example JSON output:
        {
            "total_market_cap_usd": 383330297293.0,
            "total_24h_volume_usd": 24300271821.0,
            "bitcoin_percentage_of_market_cap": 35.07,
            "active_currencies": 888,
            "active_assets": 593,
            "active_markets": 8641,
            "last_updated": 1518152066
        }
        :param convert: Returns price, 24h volume, and market cap in another specified currency
        :return: /global/ endpoint JSON data
        """
        params = {}
        if convert:
            params["convert"] = self.name_info(convert)["symbol"]
        url = self.GLOBAL_URL
        r = self.session.get(url, params=params)
        return r.json()

    def name_info(self, coin_input):
        """
        Takes a coinmarketcap ID, name, or symbol, and returns a dict containing all of that information.

        Example output:
        {
            "id": "bitcoin-cash",
            "name": "Bitcoin Cash",
            "symbol": "BCH"
        }
        :param coin_input: A valid ID, name, or symbol
        :return: Dict
        """
        if not self.coin_name_info_list:
            self._create_name_info_cache()
        coin_input = str(coin_input).lower()
        for coin in self.coin_name_info_list:
            if coin['id'].lower() == coin_input \
                    or coin['name'].lower() == coin_input \
                    or coin['symbol'].lower() == coin_input:
                return coin
        return {"error": "id not found for input \"{}\"".format(coin_input)}

    def _create_name_info_cache(self):
        """
        Creates a cache of all coinmarketcap IDs, names, and symbols so we don't have to keep hitting the API.
        :return: N/A
        """
        params = {"limit": 0} # Get all listings
        coin_list = self.session.get(self.TICKER_URL, params=params).json()
        for coin in coin_list:
            self.coin_name_info_list.append({
               "id": coin["id"],
               "name": coin["name"],
               "symbol": coin["symbol"]
            })


import sys, os, argparse
import json, csv
from time import time
from utils import *
from objects import *
import numpy as np

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='argparse')
    # parser.add_argument('--expnum', '-n', help='experiment number', required=True)
    # parser.add_argument('--perf', '-p', help='performance output', required=True)
    # parser.add_argument('--result', '-r', help='result output', required=True)
    parser.add_argument('--dbdir', '-b', help='database directory', required=True)
    parser.add_argument('--db', '-d', help='database name', required=True)
    parser.add_argument('--minsup', '-m', type=float, help='minimum support X%', required=True)
    parser.add_argument('--inc_number', '-i', type=int, help='number of increments', required=False, default=3)
    parser.add_argument('--granularity', '-g', type=int, help='size of increment', required=False, default=10000)
    args = parser.parse_args()

    totalDB = get_DB(args.dbdir, args.db)
    db_size = len(totalDB)
    minsup = args.minsup / 100 * db_size

    inc_number = args.inc_number
    granularity = args.granularity
    inc_split = db_size - inc_number * granularity
    assert(inc_split >= 0)

    baseDB = totalDB[:inc_split]
    zigzag = ZigZag(minsup, baseDB)
    zigzag.prep()
    zigzag.run()
    zigzag.updateRetainedFIs()

    for i in range(inc_number):
        incDB = totalDB[inc_split + granularity * i : inc_split + granularity * (i+1)]
        zigzag.update_incDB(incDB, inc_split)
        zigzag.runInc()
        zigzag.updateRetainedFIs()


import orjson
from pydantic import parse_raw_as
from redis.asyncio import Redis, ConnectionError

from core.helpers.utils import orjson_dumps
from db.storage.cache.base import AbstractAsyncCacheStorage, IndexModelType

class RedisAsyncCacheStorage(AbstractAsyncCacheStorage):
    """Redis is a repository class of Pydantic models that uses or json for serialization/deserialization."""

    def __init__(self, client: Redis):
        self._storage: Redis
        super().__init__(client)

    async def ping(self) -> bool:
        result = True
        try:
            # ping is unsafe
            self._storage and await self._storage.ping()
        except ConnectionError:
            result = False

        return result

    async def set(self, key: str, value: IndexModelType, ttl: int):
        await self._storage.set(key, value.json(encoder=orjson_dumps, by_alias=True), ex=ttl)

    async def get(self, key: str, model_type: type[IndexModelType]) -> IndexModelType | None:
        value = await self._storage.get(key)

        if value is None:
            return None

        return parse_raw_as(model_type, value, json_loads=orjson.loads)


import gym
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random
import copy
import math
from azure_rl.azure_client import AzureClient
from learning.learning_base import QTableLearningBase, EpisodeInfo
from collections import deque

import cProfile
from pstats import SortKey

from gym_ceo.envs.actions import ActionEnum
from gym_ceo.envs.seat_ceo_env import SeatCEOEnv
from gym_ceo.envs.seat_ceo_features_env import SeatCEOFeaturesEnv
from CEO.cards.eventlistener import EventListenerInterface, PrintAllEventListener

class QLearningTraces(QTableLearningBase):
    """
    Class implementing Watkins Q(\lambda) with eligibility traces for an OpenAI gym.
    See:
        1. Sutton and Barto 312 and the
        2. The screenshot from an earlier version of Sutton and Barto in
            https://stackoverflow.com/questions/40862578/how-to-understand-watkinss-q%CE%BB-learning-algorithm-in-suttonbartos-rl-book
        3. https://towardsdatascience.com/eligibility-traces-in-reinforcement-learning-a6b458c019d6 and
    """

    _train_episodes: int

    def __init__(self, env: gym.Env, base_env: gym.Env, train_episodes=100000, **kwargs):
        super().__init__("qlearning_traces", env, base_env, **kwargs)
        self._train_episodes = train_episodes

    def _pick_action(self, state_tuple: tuple, epsilon: float) -> ActionEnum:
        # If there is only one action, return it
        if self._env.action_space.n == 1:
            return self._env.action_space.actions[0], 0

        # Choose if we will explore or exploit
        exp_exp_sample = random.uniform(0, 1)

        exploit_action = self._qtable.greedy_action(state_tuple, self._env.action_space)
        # exploit_action = np.argmax(self._Q[(*state_tuple, slice(None))])

        explore_action_index = self._env.action_space.sample()
        explore_action = self._env.action_space.actions[explore_action_index]

        if exp_exp_sample > epsilon or exploit_action == explore_action:
            is_exploit = True
            action = exploit_action
        else:
            is_exploit = False
            action = explore_action

        assert isinstance(action, ActionEnum)

        return action, is_exploit

    def train(self, params: dict, do_log: bool):
        # Validate the parameters
        discount_factor = params["discount_factor"]
        if discount_factor is None:
            return "The parameter discount_factor is missing"

        lambda_val = params["lambda"]
        if lambda_val is None:
            return "The parameter lambda is missing"

        epsilon = params["epsilon"]
        if epsilon is None:
            return "The parameter epsilon is missing"

        max_epsilon = params["max_epsilon"]
        if max_epsilon is None:
            return "The parameter max_epsilon is missing"

        min_epsilon = params["min_epsilon"]
        if min_epsilon is None:
            return "The parameter min_epsilon is missing"

        decay = params["decay"]
        if decay is None:
            return "The parameter decay is missing"

        alpha_exponent = params["alpha_exponent"]
        if alpha_exponent is None:
            return "The parameter alpha_exponent is missing"

        print("Training with", self._train_episodes, "episodes")

        # Log the start of training to Azure, if necessary.
        if self._azure_client:
            params = dict()
            params["discount_factor"] = discount_factor
            params["lambda"] = lambda_val
            params["epsilon"] = epsilon
            params["max_epsilon"] = max_epsilon
            params["min_epsilon"] = min_epsilon
            params["decay"] = decay
            params["alpha_exponent"] = alpha_exponent

            self._azure_client.start_training(
                "qlearning_traces",
                self._base_env.action_space_type,
                self._env.full_env.num_players,
                self._env.full_env.seat_number,
                params,
                self._env.feature_defs,
            )

        # Training the agent
        total_training_reward = 0
        recent_episode_rewards = deque()
        recent_explore_counts = deque()
        recent_exploit_counts = deque()
        max_recent_episode_rewards = 10000
        states_visited = 0
        for episode in range(1, self._train_episodes + 1):
            if do_log:
                print("Starting episode")

            # Reseting the environment each time as per requirement
            state = self._env.reset()
            state_tuple = tuple(state.astype(int))
            info = dict()

            # Pick the initial action
            action, is_exploit = self._pick_action(state_tuple, epsilon)

            # Initialize the eligibility traces
            eligibility_traces = dict()

            # Initialize the episode information.
            episode_reward = 0
            episode_explore_count = 0
            episode_exploit_count = 0
            episode_infos: list[EpisodeInfo] = []
            episode_value_before = dict()
            episode_value_after = dict()

            # Run until the episode is finished
            while True:
                # Save off the action information
                if is_exploit:
                    episode_exploit_count += 1
                    action_type = "-------"
                else:
                    action_type = "explore"
                    episode_explore_count += 1

                if do_log:
                    print("Top of loop.")
                    action_space = self._env.action_space

                    print("State", state_tuple)
                    print("Obs info:")
                    for key, value in info.items():
                        print(" ", key, "->", value)

                    print("Action", action)
                    print("Action choice:", action_type)
                    print("Action values")
                    for a in range(self._env.full_env.max_action_value):
                        full_action = ActionEnum(a)
                        name = full_action.name if full_action in action_space.actions else ""
                        selected = "selected" if full_action == action else ""

                        print(
                            "  action",
                            a,
                            "value",
                            self._qtable.state_action_value((*state_tuple, full_action)),
                            "count",
                            self._qtable.state_visit_count((*state_tuple, full_action)),
                            name,
                        )

                # Save state information
                state_action_tuple = state_tuple + (action,)

                episode_info = EpisodeInfo()
                episode_infos.append(episode_info)
                episode_info.state = state_action_tuple
                episode_info.value_before = self._qtable.state_action_value(state_action_tuple)
                if state_action_tuple not in episode_value_before:
                    episode_value_before[state_action_tuple] = episode_info.value_before

                self._qtable.increment_state_visit_count(state_action_tuple)
                state_visit_count = self._qtable.state_visit_count(state_action_tuple)
                if state_visit_count == 1:
                    states_visited += 1

                # Perform the action
                action_index = self._env.action_space.actions.index(action)
                state_prime, reward, done, info = self._env.step(action_index)

                if state_prime is not None:
                    if do_log:
                        print("After action")
                        action_space = self._env.action_space

                        print("  State", state_tuple)
                        print("  Obs info:")
                        for key, value in info.items():
                            print(" ", key, "->", value)

                        print("  Action values")
                        for a in range(self._env.full_env.max_action_value):
                            full_action = ActionEnum(a)
                            name = ""

                            print(
                                "    action",
                                a,
                                "value",
                                self._qtable.state_action_value((*state_tuple, full_action)),
                                "count",
                                self._qtable.state_visit_count((*state_tuple, full_action)),
                                name,
                            )

                    state_prime_tuple = tuple(state_prime.astype(int))
                    state_prime_value = self._qtable.state_value(
                        state_prime_tuple, self._env.action_space
                    )

                    # Pick the next action
                    action_prime, is_exploit = self._pick_action(state_prime_tuple, epsilon)

                    # Find the action with the highest estimated reward
                    action_star = self._qtable.greedy_action(
                        state_prime_tuple, self._env.action_space
                    )
                    action_star_value = self._qtable.state_action_value(
                        (*state_prime_tuple, action_star)
                    )
                    if action_star_value == state_prime_value:
                        action_star = action_prime

                    # Calculate the update value
                    delta = (
                        reward
                        + discount_factor
                        * self._qtable.state_action_value((*state_prime_tuple, action_star))
                        - self._qtable.state_action_value((*state_tuple, action))
                    )

                    if do_log:
                        print("Action prime", action_prime)
                        print("  is exploit", is_exploit)
                        print("Action star", action_star)
                        print("Delta", delta)
                else:
                    if do_log:
                        print("Reward", reward)
                    assert done
                    assert reward != 0

                    state_prime_tuple = None
                    state_prime_value = 0
                    action_star = None
                    action_prime = None

                    # Calculate the update value
                    delta = reward - self._qtable.state_action_value((*state_tuple, action))

                if do_log:
                    print("delta", delta)

                # Update the eligibility trace for this state
                if state_action_tuple not in eligibility_traces:
                    eligibility_traces[state_action_tuple] = 1
                else:
                    eligibility_traces[state_action_tuple] += 1

                # Find the step size alpha
                # See Learning Rates for Q-learning, Even-Dar and Mansour, 2003
                # https://www.jmlr.org/papers/volume5/evendar03a/evendar03a.pdf
                alpha = 1.0 / (state_visit_count ** alpha_exponent)

                # Update Q and the traces
                for update_tuple in eligibility_traces.keys():
                    value_before = self._qtable.state_action_value(update_tuple)
                    update = alpha * delta * eligibility_traces[update_tuple]
                    self._qtable.update_state_visit_value(update_tuple, update)
                    # self._Q[update_tuple] += alpha * delta * eligibility_traces[update_tuple]
                    if action_prime == action_star:
                        eligibility_traces[update_tuple] *= discount_factor * lambda_val
                    else:
                        eligibility_traces[update_tuple] = 0

                    episode_value_after[update_tuple] = self._qtable.state_action_value(
                        update_tuple
                    )

                    if do_log:
                        print(
                            "Update ",
                            update_tuple,
                            " ",
                            value_before,
                            " -> ",
                            episode_value_after[update_tuple],
                        )
                        print("  trace ", eligibility_traces[update_tuple])
                    # episode_value_after[update_tuple] = self._Q[update_tuple]

                state = state_prime
                state_tuple = state_prime_tuple
                action = action_prime

                # Save information from after the update
                episode_info.value_after = self._qtable.state_action_value(state_action_tuple)
                episode_info.action_type = action_type
                episode_info.alpha = alpha
                episode_info.state_visit_count = state_visit_count
                # episode_info.value_after = self.self._Q[state_action_tuple]
                # episode_info.hand = copy.deepcopy(info)

                # Increasing our total reward and updating the state
                episode_reward += reward

                # See if the episode is finished
                if done == True:
                    break

            # Cutting down on exploration by reducing the epsilon
            epsilon = min_epsilon + (max_epsilon - min_epsilon) * np.exp(-decay * episode)

            # Save the reward
            total_training_reward += episode_reward
            recent_episode_rewards.append(episode_reward)
            recent_exploit_counts.append(episode_exploit_count)
            recent_explore_counts.append(episode_explore_count)
            if len(recent_episode_rewards) > max_recent_episode_rewards:
                recent_episode_rewards.popleft()
                recent_explore_counts.popleft()
                recent_exploit_counts.popleft()

            if (episode > 0 and episode % 2000 == 0) or (episode == self._train_episodes):
                ave_training_rewards = total_training_reward / episode
                recent_rewards = sum(recent_episode_rewards) / len(recent_episode_rewards)
                recent_explore_rate = sum(recent_explore_counts) / (
                    sum(recent_exploit_counts) + sum(recent_explore_counts)
                )

                print(
                    "Episode {} Ave rewards {:.3f} Recent rewards {:.3f} Explore rate {:.3f} States visited {}".format(
                        episode,
                        ave_training_rewards,
                        recent_rewards,
                        recent_explore_rate,
                        states_visited,
                    )
                )

                self.add_search_statistics(
                    "qlearning_traces",
                    episode,
                    ave_training_rewards,
                    recent_rewards,
                    recent_explore_rate,
                    states_visited,
                    min_epsilon,
                    max_epsilon,
                )

            if (
                episode > 0
                and episode % self._during_training_stats_frequency == 0
                and episode < self._train_episodes
            ):
                self.do_play_test(episode)

            if False and episode > 0 and episode % 5000 == 0:
                # Log the states for this episode
                print("Episode info")
                for info in episode_infos:
                    max_visit_count = max(map(lambda info: info.state_visit_count, episode_infos))
                    visit_chars = math.ceil(math.log10(max_visit_count))

                    format_str = "{action:2} value {val_before:6.3f} -> {val_after:6.3f} visit {visit_count:#w#} alpha {alpha:.3e}"
                    format_str = format_str.replace("#w#", str(visit_chars))

                    print(
                        format_str.format(
                            action=info.action_type,
                            val_before=episode_value_before[info.state],
                            val_after=episode_value_after[info.state],
                            state=info.state,
                            alpha=info.alpha,
                            visit_count=info.state_visit_count,
                        )
                    )
                print("Reward", episode_reward)
                print("Epsilon {:.5f}".format(epsilon))

            if False and episode > 0 and episode % 100000 == 0:
                # Iterate over the entire Q array and count the number of each type of element.
                # This is very slow.
                zero_count = 0
                pos_count = 0
                neg_count = 0
                for val in np.nditer(self._Q):
                    if val == 0:
                        zero_count += 1
                    elif val > 0:
                        pos_count += 1
                    else:
                        neg_count += 1

                print("  pos", pos_count, "neg", neg_count, "zero", zero_count)

        if self._azure_client:
            self._azure_client.end_training()

        return self._search_statistics[-1]

# Main function
def main():
    parser = argparse.ArgumentParser(description="Do learning")
    parser.add_argument(
        "--profile",
        dest="profile",
        action="store_const",
        const=True,
        default=False,
        help="Do profiling.",
    )
    parser.add_argument(
        "--log",
        dest="log",
        action="store_const",
        const=True,
        default=False,
        help="Do logging.",
    )
    parser.add_argument(
        "--episodes",
        dest="train_episodes",
        type=int,
        default=100000,
        help="The number of rounds to play",
    )
    parser.add_argument(
        "--azure",
        dest="azure",
        action="store_const",
        const=True,
        default=False,
        help="Save agent and log information to azure blob storage.",
    )

    args = parser.parse_args()
    print(args)

    kwargs = dict()
    if args.train_episodes:
        kwargs["train_episodes"] = args.train_episodes

    if args.azure:
        kwargs["azure_client"] = AzureClient()

    do_log = False
    if args.log:
        do_log = args.log

    random.seed(0)
    listener = PrintAllEventListener()
    listener = EventListenerInterface()
    base_env = SeatCEOEnv(listener=listener)
    env = SeatCEOFeaturesEnv(base_env)

    # Set up default parameters
    params = dict()
    params["discount_factor"] = 0.7
    params["lambda"] = 0.5
    params["epsilon"] = 1
    params["max_epsilon"] = 0.5
    params["min_epsilon"] = 0.01
    params["decay"] = 0.00001
    params["alpha_exponent"] = 0.85

    qlearning = QLearningTraces(env, **kwargs)

    if args.profile:
        print("Running with profiling")
        cProfile.run("qlearning.train()", sort=SortKey.CUMULATIVE)
    else:
        qlearning.train(params, do_log)

    # Save the agent in a pickle file.
    file_name = "qlearning_traces.pickle"
    qlearning.pickle(file_name)

if __name__ == "__main__":
    main()


import torch
import os
import numpy as np
import argparse

from tqdm import tqdm
from perf_gpu import save_image_grid

def test_om(args):
    bin_path = args.bin_path
    image_path = args.image_path
    bin_list = os.listdir(bin_path)
    bin_list.sort()
    for i in tqdm(range(len(bin_list))):
        images = np.fromfile(os.path.join(bin_path, bin_list[i]), dtype=np.float32)
        images = torch.Tensor(images)
        images = images.reshape(-1, 3, 512, 512)
        bs = images.shape[0]
        grid_size = (4, 4) if bs == 16 else (1, 1)
        os.makedirs(image_path, exist_ok=True)
        save_image_grid(images, os.path.join(image_path, f'gen_image_{i:04d}') + ".png", drange=[-1, 1],
                        grid_size=grid_size)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--bin_path', type=str, required=True)
    parser.add_argument('--image_path', type=str, default='./results')
    args = parser.parse_args()

    test_om(args)


from django.conf import settings
from django.views.generic import View
from django.shortcuts import render
from django.http import HttpResponseBadRequest

from .models import Tag
from .forms import TagForm

class Tags(View):
    def get(self, request, *args, **kwargs):
        context = {}
        tags = Tag.get_tags_sorted_by_number_of_resources()
        context['tags_grid'] = Tag.get_tags_grid(tags, settings.TAGS_PER_ROW)
        tag_form = TagForm()
        context['form'] = tag_form
        return render(request, 'tags/tags.html', context)

    def post(self, request, *args, **kwargs):
        if not request.user.is_authenticated():
            return HttpResponseBadRequest()
        context = {}
        tag_form = TagForm(request.POST)
        if tag_form.is_valid():
            tag_form.save()
            tag_form = TagForm()
        context['form'] = tag_form
        tags = Tag.get_tags_sorted_by_number_of_resources()
        context['tags_grid'] = Tag.get_tags_grid(tags, settings.TAGS_PER_ROW)
        return render(request, 'tags/tags.html', context)


# Autor Leonardo Dourado
# É proibida a utilização do programa ou cópia de parte do código sem a autorização do autor.

import requests
import time

def get_count_cryptocurrencies(category):
    '''Essa função retorna a quantidade de
     moedas disponíveis.'''

    if category == "spot":
        URL_query = "https://api.coinmarketcap.com/data-api/v3/cryptocurrency/listing?start=1&category=spot"
        r = requests.get(url = URL_query)
        data_count_crypto = r.json()
    elif category == "futures":
        URL_query = "https://api.coinmarketcap.com/data-api/v3/cryptocurrency/listing?start=1&category=futures"
        r = requests.get(url = URL_query)
        data_count_crypto = r.json()

    return int(data_count_crypto['data']['totalCount'])

def get_all_cryptocurrencies_spot(limit=get_count_cryptocurrencies("spot")):
    '''Essa função retorna as estatísticas das moedas no mercado spot.'''
    limit = get_count_cryptocurrencies("spot")
    URL_query = "https://api.coinmarketcap.com/data-api/v3/cryptocurrency/listing?start=1&limit={0}&sortBy=market_cap&category=spot&sortType=desc&convert=USD,BTC,ETH&cryptoType=all&tagType=all&audited=false&aux=ath,atl,high24h,low24h,num_market_pairs,cmc_rank,date_added,tags,platform,max_supply,circulating_supply,total_supply,volume_7d,volume_30d".format(limit)
    r = requests.get(url = URL_query)
    data_all_cryptocurrencies = r.json()

    return data_all_cryptocurrencies

def get_all_cryptocurrencies_future(limit=get_count_cryptocurrencies("futures")):
    '''Essa função retorna as estatísticas das moedas no mercado future.'''
    limit = get_count_cryptocurrencies("futures")
    URL_query = "https://api.coinmarketcap.com/data-api/v3/cryptocurrency/listing?start=1&limit={0}&sortBy=market_cap&category=futures&sortType=desc&convert=USD,BTC,ETH&cryptoType=all&tagType=all&audited=false&aux=ath,atl,high24h,low24h,num_market_pairs,cmc_rank,date_added,tags,platform,max_supply,circulating_supply,total_supply,volume_7d,volume_30d".format(limit)
    r = requests.get(url = URL_query)
    data_all_cryptocurrencies = r.json()

    return data_all_cryptocurrencies

def get_coins_by_exchange(exchange_slug):
    '''Essa função retorna todas as moedas de uma exchange específica e salva em arquivo.'''
    coins_set = set()
    for page in range(0,100):
        URL_query = "https://api.coingecko.com/api/v3/exchanges/{0}/tickers?page={1}".format(exchange_slug, page)
        r = requests.get(url = URL_query)
        data_exchange = r.json()

        if len(data_exchange['tickers']) > 0:
            for i in range(0,len(data_exchange['tickers'])):
                coins_set.add(data_exchange['tickers'][i]['base'])
        else:
            break

    try:
        file = open("coins_by_exchange.bin","w")
        for line in coins_set:
            file.write("{0}\n".format(line))
        file.close()
    except Exception as ex:
        print(ex)

    return list(coins_set)

def get_coins_list():
    '''Essa função retorna de um arquivo uma lista com as moedas armazenadas.'''
    coins_list = list()
    try:
        file = open("coins_by_exchange.bin","r")

        for line in file:
            coins_list.append(line.replace("\n",""))

        file.close()
    except Exception as ex:
        print(ex)

    return coins_list

#def get_exchanges_by_cryptocurrency(coin_slug):
#    '''Essa função retorna em quais exchanges uma determinada moeda está listada.'''
#    exchanges_set = set()
#    URL_query = "https://api.coinmarketcap.com/data-api/v3/cryptocurrency/market-pairs/latest?slug={0}".format(coin_slug)
#    r = requests.get(url = URL_query)
#    data_exchanges = r.json()
#    exchanges_array_size = len(data_exchanges['data']['marketPairs'])

#    for i in range(1,exchanges_array_size):
#        exchanges_set.add(data_exchanges['data']['marketPairs'][i]['exchangeSlug'])

#    return exchanges_set

def check_crypto_in_exchange(coin_symbol, exchange_coins_list):
    '''Essa função retorna True se uma moeda está listada em uma exchange específica e False se não está listada.'''
    if coin_symbol in exchange_coins_list:
        return True
    else:
        return False

def get_top_gain(top_n, marketcap_greater, volume24h, exchanges, window):
    '''Essa função atualiza os campos da janela com o top n de altas de moedas listada em ordem decrescente.'''
    cryptocurrencies = list()
    data = get_all_cryptocurrencies_spot()
    top_return = list()

    exchanges_coin_list = get_coins_list()

    for i in range(0, int(get_count_cryptocurrencies("spot"))):
        if float(data['data']['cryptoCurrencyList'][int(i)]['quotes'][2]['marketCap']) < marketcap_greater and volume24h > float(data['data']['cryptoCurrencyList'][int(i)]['quotes'][2]['volume24h']):
            continue
        cryptocurrencies.append({'name': data['data']['cryptoCurrencyList'][int(i)]['name'], 'slug': data['data']['cryptoCurrencyList'][int(i)]['slug'], 'symbol': data['data']['cryptoCurrencyList'][int(i)]['symbol'], 'Variação 1h': data['data']['cryptoCurrencyList'][int(i)]['quotes'][2]['percentChange1h'], 'Variação 24h': data['data']['cryptoCurrencyList'][int(i)]['quotes'][2]['percentChange24h']})

    cryptocurrencies_sorted = sorted(cryptocurrencies, key=lambda k: k['Variação 1h'], reverse=True)

    counter = 0

    for cryptocurrency in cryptocurrencies_sorted:
        if check_crypto_in_exchange(cryptocurrency['symbol'], exchanges_coin_list):
            index = str(counter+1)
            #print(".{0}.".format(cryptocurrency['symbol']))
            window.Element("top_gain_{0}_1".format(index)).Update(cryptocurrency['name'])
            window.Element("top_gain_{0}_2".format(index)).Update(cryptocurrency['slug'])
            window.Element("top_gain_{0}_3".format(index)).Update(cryptocurrency['symbol'])
            window.Element("top_gain_{0}_4".format(index)).Update("{0:.2f}".format(cryptocurrency['Variação 1h']))
            counter += 1

        if counter == top_n:
                break

def get_top_loss(top_n, marketcap_greater, volume24h, exchanges, window):
    '''Essa função atualiza os campos da janela com o top n de baixas de moedas listada em ordem crescente.'''
    cryptocurrencies = list()
    data = get_all_cryptocurrencies_future()
    top_return = list()

    exchange_coin_list = get_coins_list()

    for i in range(0, int(get_count_cryptocurrencies("futures"))):
        if float(data['data']['cryptoCurrencyList'][int(i)]['quotes'][2]['marketCap']) < marketcap_greater and volume24h > float(data['data']['cryptoCurrencyList'][int(i)]['quotes'][2]['volume24h']):
            continue
        cryptocurrencies.append({'name': data['data']['cryptoCurrencyList'][int(i)]['name'], 'slug': data['data']['cryptoCurrencyList'][int(i)]['slug'], 'symbol': data['data']['cryptoCurrencyList'][int(i)]['symbol'], 'Variação 1h': data['data']['cryptoCurrencyList'][int(i)]['quotes'][2]['percentChange1h'], 'Variação 24h': data['data']['cryptoCurrencyList'][int(i)]['quotes'][2]['percentChange24h']})

    cryptocurrencies_sorted = sorted(cryptocurrencies, key=lambda k: k['Variação 1h'])

    counter = 0

    for cryptocurrency in cryptocurrencies_sorted:
        if check_crypto_in_exchange(cryptocurrency['symbol'], exchange_coin_list):
            index = str(counter+1)
            #print(".{0}.".format(cryptocurrency['symbol']))
            window.Element("top_loss_{0}_1".format(index)).Update(cryptocurrency['name'])
            window.Element("top_loss_{0}_2".format(index)).Update(cryptocurrency['slug'])
            window.Element("top_loss_{0}_3".format(index)).Update(cryptocurrency['symbol'])
            window.Element("top_loss_{0}_4".format(index)).Update("{0:.2f}".format(cryptocurrency['Variação 1h']))
            counter += 1

        if counter == top_n:
            break

def repeat_by_time_top_gain(top_n, marketcap_greater, volume24h, exchanges, window):
    '''Essa função executa em loop temporal a função get_top_gain.'''
    while True:
        get_top_gain(top_n, marketcap_greater, volume24h, exchanges, window)
        time.sleep(int(returnConfigFile()[3]))

def repeat_by_time_top_loss(top_n, marketcap_greater, volume24h, exchanges, window):
    '''Essa função executa em loop temporal a função get_top_loss.'''
    while True:
        get_top_loss(top_n, marketcap_greater, volume24h, exchanges, window)
        time.sleep(int(returnConfigFile()[3]))

def clear_gain_fields(top_n, window):
    '''Essa função limpa os campos de ganho das moedas na janela.'''
    for index in range(1,top_n+1):
        window.Element("top_gain_{0}_1".format(index)).Update(" ")
        window.Element("top_gain_{0}_2".format(index)).Update(" ")
        window.Element("top_gain_{0}_3".format(index)).Update(" ")
        window.Element("top_gain_{0}_4".format(index)).Update(" ")

def clear_loss_fields(top_n, window):
    '''Essa função limpa os campos de perda das moedas na janela.'''
    for index in range(1,top_n+1):
        window.Element("top_loss_{0}_1".format(index)).Update(" ")
        window.Element("top_loss_{0}_2".format(index)).Update(" ")
        window.Element("top_loss_{0}_3".format(index)).Update(" ")
        window.Element("top_loss_{0}_4".format(index)).Update(" ")

def getValue(config_line):
    '''Essa função separa o parâmetro do valor atribuído, retornando apenas o valor atribuído.'''
    value = config_line.split("=")
    return value[1]

def loadConfigFile(window):
    '''Essa função carrega nos campos da interface GUI os parâmetros salvos no arquivo.'''
    try:
        file = open("config.bin","r")
        counter = 1
        for line in file:
            window.Element(counter).Update(getValue(line))
            counter += 1
        file.close()
    except FileNotFoundError:
        return False

def saveParametersInFile(top_currencies,marketcap_greater,volume24h,time_refresh_seconds):
    '''Essa função salva os parâmetros no arquivo de configuração.'''
    try:
        file = open("config.bin","w")
        file.write("top_currencies={0}\n".format(top_currencies.replace("\n","")))
        file.write("marketcap_greater={0}\n".format(marketcap_greater.replace("\n","")))
        file.write("volume24h={0}\n".format(volume24h.replace("\n","")))
        file.write("time_refresh_seconds={0}\n".format(time_refresh_seconds.replace("\n","")))
        file.close()
    except Exception as ex:
        print(ex)

def returnConfigFile():
    ''' Essa função retorna os parâmetros definidos e salvos no arquivo de configuração.'''
    try:
        file = open("config.bin","r")
        config_parameters = list()
        counter = 1
        for line in file:
            line_return = getValue(line)
            line_return = line_return.replace("\n","")
            config_parameters.append(line_return)
            counter += 1
        file.close()
        return config_parameters
    except FileNotFoundError:
        return [None,None,None,None]

#get_coins_by_exchange("binance")


from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    username = models.CharField(blank=True, null=True, max_length=50)
    # Overwrtiting to make it available to have a blank username, because the email field will be the main field
    # used for authentication, for easier maintenanace
    email = models.EmailField("email address", unique=True)
    # We have to overwrite this field from an AbstractUser model,
    # to make the 'unique' constraint - so it will be available for using for the authentication
    USERNAME_FIELD = "email"
    # That means that this field will be used for authentication
    REQUIRED_FIELDS = ["username", "first_name", "last_name"]
    # Fields required by 'createsuperuser' command, username has to be there, otherwise Django will complain

    def __str__(self):
        return "{}".format(self.email)

class CustomUserProfile(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="profile"
    )
    date_of_birth = models.DateField(null=True, blank=True)
    country = models.CharField(max_length=50, blank=True)
    city = models.CharField(max_length=50, blank=True)
    bio = models.TextField(max_length=500, blank=True)
    photo = models.ImageField(upload_to="uploads", blank=True)

class Article(models.Model):
    title = models.CharField(max_length=255)
    content = models.TextField()
    author = models.ForeignKey(
        CustomUser, on_delete=models.DO_NOTHING, related_name="articles"
    )
    created_at = models.DateTimeField(auto_now_add=True)


#!/usr/bin/env python3
"""Specify the parameters for all the commands in gridgen
"""
from pathlib import Path
from json import load
from collections.abc import Iterable

PKG_PATH = Path(__file__).parents[2]

DIMENSIONS = 'x', 'y', 'rotation'

REQUIRED = {
    'grid2d': [
        'grid2d',
        ],
    'ecog': [
        'ecog',
        ],
    'grid3d': [
        'grid3d',
        'mri',
        'initial',
        ],
    'fit': [
        'grid3d',
        'mri',
        'initial',
        'fit',
        ],
    'matlab': [
        'mri',
        'matlab',
        ]
    }

TEMPLATE = {
    "output_dir": {
        'type': 'str',
        'necessary': False,
        'help': 'path to output directory',
        },
    "grid2d": {
        "n_rows": {
            'type': 'int',
            'necessary': True,
            'help': 'number of rows',
            },
        "n_columns": {
            'type': 'int',
            'necessary': True,
            'help': 'number of columns',
            },
        "direction": {
            'type': 'str',
            'necessary': True,
            'values': ['TBLR', 'TBRL', 'BTLR', 'BTRL', 'LRTB', 'LRBT', 'RLTB', 'RLBT'],
            'help': 'direction of the grid (wires are by definition at the bottom)',
            'default': 'TBLR'
            },
        "chan_pattern": {
            'type': 'str',
            'necessary': False,
            'help': 'pattern to name the channels (it should match the naming pattern of the data). Examples are "chan{}" or "chan{03d}',
            },
        },
    "ecog": {
        "ecog_file": {
            'type': 'str',
            'necessary': True,
            'help': 'path to ECoG file',
            },
        "freq_range": {
            'type': 'list',
            'necessary': True,
            'help': 'low and high threshold of the frequency range of interest',
            'default': [65, 95],
            },
        "begtime": {
            'type': 'float',
            'necessary': False,
            'help': 'start time in seconds from the beginning of the file',
            },
        "endtime": {
            'type': 'float',
            'necessary': False,
            'help': 'end time in seconds from the beginning of the file',
            },
        "bad_channels": {
            'type': 'list',
            'necessary': False,
            'help': 'list of str, name of the channels to exclude',
            },
        },
    "grid3d": {
        "interelec_distance": {
            "type": "float",
            "necessary": False,
            "help": "distance between the electrode centers (pitch), in mm",
            "default": 3,
            },
        "maximum_angle": {
            "type": "float",
            "necessary": False,
            "help": "maximum angle, in degrees, that the grid can flex, between two neighboring electrodes (elasticity of the grid)",
            "default": 5,
            },
        "step_angle": {
            "type": "float",
            "necessary": False,
            "help": "step size, in degrees, when computing range between -`maximum_angle` and +`maximum_angle`. The smaller the step size, the faster the generation of the grid",
            "default": 0.25,
            },
        },
    "mri": {
        "T1_file": {
            'type': 'str',
            'necessary': True,
            'help': 'path to T1 image (in particular, the T1.mgz from freesurfer). Only used to compute the mapping between T1 RAS space and surface RAS space',
            },
        "dura_file": {
            'type': 'str',
            'necessary': True,
            'help': 'path to dura surface (for example, the smoothed pial surface). This surface will be used to generate the 3D grid',
            },
        "pial_file": {
            'type': 'str',
            'necessary': False,
            'help': 'path to pial surface (in particular, the lh.pial or rh.pial from freesurfer). You need to specify key `morphology` in `parameters.json`',
            'default': None,
            },
        "func_file": {
            'type': 'str',
            'necessary': False,
            'help': 'path to angiogram or fMRI (in NIfTI format). You need to specify key `functional` in `parameters.json`',
            'default': None,
            },
        },
    "initial": {
        "label": {
            'type': 'str',
            'necessary': True,
            'help': 'label for the reference electrode',
            },
        "RAS": {
            'type': 'list',
            'necessary': True,
            'help': 'initial location for the reference electrode (coordinates in MRI space)',
            },
        "rotation": {
            'type': 'float',
            'necessary': True,
            'help': 'degree of rotation of the grid (in degrees, 0° is roughly pointing up)',
            },
        },
    "morphology": {
        "distance": {
            "type": "str",
            "necessary": False,
            "values": ['ray', 'minimum', 'view', 'cylinder', 'pdf'],
            "help": "how to compute the distance of the morphology",
            "default": "ray",
            },
        "maximum_distance": {
            'type': 'float',
            'necessary': False,
            'help': 'maximum distance between electrode and pial surface. Exact interpretation depends on the type of `morphology`',
            'default': 10,
            },
        "penalty": {
            "type": "float",
            "necessary": False,
            "help": "exponent when computing the penalty from the distance. Morphology = 1 / distancepenalty. More simply, 1 = activity decreases linearly with distance; 2 = activity decreases with the square of the distance",
            "default": 1,
            },
        },
    "functional": {
        "threshold": {
            'type': 'float',
            'necessary': True,
            'help': 'value to threshold the func_file and binarize it. If None, func_file won\'t be binarized',
            'default': None,
            },
        "metric": {
            "type": "str",
            "necessary": False,
            "values": ['gaussian', 'sphere', 'inverse'],
            "help": "method to compute the value at each electrode. `sphere` is the sum of the voxels around the electrodes, within `kernel` distance. `gaussian` is the weighted average of the voxels around an electrode, weighted by a 3d gaussian kernel. With `inverse`, the weights are based on the inverse of the distance.",
            "default": "inverse",
            },
        "kernel": {
            "type": "float",
            "necessary": False,
            "help": "With method `sphere`, this is the size of the sphere. With method `gaussian`, this is the size of the sigma of the 3D weighting kernel. With method `inverse`, this is the exponent (1 -> linear, 2 -> quadratic).",
            "default": 2,
            },
        },
    "fit": {
        "method": {
            "type": "str",
            "necessary": True,
            "values": ['brute', 'simplex'],
            "help": "method to use (brute includes simplex as a second step)",
            "default": "brute",
            },
        "metric": {
            "type": "str",
            "necessary": False,
            "values": ['parametric', 'nonparametric', 'sum'],
            "help": "'parametric' correlation (Pearson, default), 'nonparametric' correlation (rank) or 'sum' (grid position where the sum of the electrode values is highest)",
            "default": "parametric",
            },
        "morphology_weight": {
            "type": "float",
            "necessary": False,
            "help": "whether morphology is positively (+1) or negatively (-1) correlated to ECoG (or whether morphology contributes positively or negatively to the highest sum). You can also adjust this value to adjust the scale of the morphology values.",
            "default": 1,
            },
        "functional_weight": {
            "type": "float",
            "necessary": False,
            "help": "whether functional is positively (+1) or negatively (-1) correlated to ECoG (or whether functional contributes positively or negatively to the highest sum). You can also adjust this value to adjust the scale of the functional values.",
            "default": 1,
            },
        "functional_contribution": {
            "type": "list",
            "necessary": False,
            "help": "If both present, it's possible to combine morphology and functional models with varying weights. This parameter indicates which weights will be tested (10 = 10% of functional contribution and 90% of morphology contribution)",
            "default": [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
            },
        "steps": {
            "x": {
                "type": "float",
                "necessary": False,
                "help": "Step size in mm for x-direction, for method simplex",
            },
            "y": {
                "type": "float",
                "necessary": False,
                "help": "Step size in mm for y-direction, for method simplex",
            },
            "rotation": {
                "type": "float",
                "necessary": False,
                "help": "Step size in degrees for rotation, for method simplex",
            },
        },
        "ranges": {
            "x": {
                "type": "list",
                "necessary": False,
                "help": "Range in mm for x-direction, in format [low, step, high], for method brute",
            },
            "y": {
                "type": "list",
                "necessary": False,
                "help": "Range in mm for y-direction, in format [low, step, high], for method brute",
            },
            "rotation": {
                "type": "list",
                "necessary": False,
                "help": "Range in degrees for rotation, in format [low, step, high], for method brute",
            },
        },
    },
    "matlab": {
        "input": {
            "subjectInfo_file": {
                'type': 'str',
                'necessary': True,
                'help': 'path to subjectInfo.mat (can also be subj_info.json if subjectInfo.mat is not available)'
                },
            "gridInfo_file": {
                'type': 'str',
                'necessary': True,
                'help': 'path to gridInfo.mat',
                },
            },
        "comparison": {
            "angiomap_file": {
                'type': 'str',
                'necessary': False,
                'help': 'path to _angiomap.mat',
                },
            "model_file": {
                'type': 'str',
                'necessary': False,
                'help': 'path to full models ("ROI")',
                },
            "prediction_file": {
                'type': 'str',
                'necessary': False,
                'help': 'path to coordinates with the best fit',
                },
            }
        }
    }

def convert_to_path(d, parent):
    """Convert strings to paths. If the path is relative, then this script makes
    it absolute in reference to "parent"

    Parameters
    ----------
    d : dict
        if a key ends in _file or _dir, it gets converted to path
    parent : path
        absolute path to use to make relative paths absolute

    Returns
    -------
    dict
        where keys ending in _file and _dir are all absolute paths
    """
    for k, v in d.items():
        if v is None:
            continue
        elif isinstance(v, dict):
            v = convert_to_path(v, parent)
        elif k.endswith('_file') or k.endswith('_dir'):
            v = Path(v)
            if not v.is_absolute():
                v = (parent / v).resolve()
            d[k] = v
    return d

def prepare_template(temp):
    """Prepare template by reading the TEMPLATE and converting it to markdown

    Parameters
    ----------
    temp : dict
        TEMPLATE (or its subfields)

    Returns
    -------
    dict
        with default values and necessary
    """
    out = {}
    for k, v in temp.items():
        if 'type' in v:
            if not v['necessary']:
                out[k] = None

            elif v['type'] == 'int':
                out[k] = v.get('default', 0)

            elif v['type'] == 'float':
                out[k] = v.get('default', 0.0)

            elif v['type'] == 'str':
                out[k] = v.get('default', '')

            elif v['type'] == 'list':
                out[k] = v.get('default', [])

        else:
            out[k] = prepare_template(v)
    return out

def help_template(temp):
    """Prepare template by reading the TEMPLATE and converting it to markdown

    Parameters
    ----------
    temp : dict
        TEMPLATE (or its subfields)

    Returns
    -------
    list of str
        dict converted to list, so that it can be exported to markdown
    """
    modules = _invert_dict(REQUIRED)

    out = []
    for k, v in temp.items():
        if 'type' in v:
            out_ = f'- **{k}**: '

            if v['type'] == 'int':
                out_ += '(int'

            elif v['type'] == 'float':
                out_ += '(float'

            elif v['type'] == 'str':
                out_ += '(str'

            elif v['type'] == 'list':
                out_ += '(list'

            if v['necessary']:
                out_ += ') '
            else:
                out_ += ', optional) '

            out_ += v['help']

            if 'values' in v:
                out_ += '. Values: [' + ', '.join(v['values']) + ']'

            if 'default' in v:
                out_ += f'. Default: {v["default"]}'

            out.append(out_)

        else:
            out_ = help_template(v)
            if k in modules:
                extra = ' (required by command ' + ', '.join(f'`{x}`' for x in modules[k]) + ')'
            else:
                extra = ''
            out.append(f'- **{k}**' + extra)
            out.extend([f'  {x}' for x in out_])

    return out

def validate_template(temp, d):
    """Check whether the parameters passed in json file are in accordance with
    the template

    Parameters
    ----------
    temp : dict
        TEMPLATE of parameters
    d : dict
        actual parameters

    Returns
    -------
    dict
        input dictionary, with checked values and added defaults if necessary
    """
    out = {}
    for k, v in temp.items():

        if 'method' in d:
            if d['method'] == 'brute':
                for dim in DIMENSIONS:
                    if d.get('ranges', {}).get(dim, None) is None:
                        raise ValueError(f'{dim} should be defined in "ranges" with method "brute"')

                d.pop('steps', None)

            if d['method'] == 'simplex':
                for dim in DIMENSIONS:
                    if d.get('steps', {}).get(dim, None) is None:
                        raise ValueError(f'{dim} should be defined in "steps" with method "simplex"')

                d.pop('ranges', None)

        if k not in d or d[k] is None:
            if v.get('default', False):
                out[k] = v['default']

        elif 'type' in v:
            if v['necessary']:
                if k not in d:
                    raise ValueError(f'{k} should be defined')
                if isinstance(d[k], Iterable) and len(d[k]) == 0:
                    raise ValueError(f'{k} should be defined')

            if 'values' in v and d[k] not in v['values']:
                values = ', '.join(f'{x}' for x in v['values'])
                raise ValueError(f'{k} should be one of these values: {values}')

            out[k] = d[k]

        elif k in d:
            out[k] = validate_template(v, d[k])

    return out

def _invert_dict(d):
    new_dict = {}
    for k, v in d.items():
        for x in v:
            new_dict.setdefault(x, []).append(k)
    return new_dict

def parse_parameters(parameters, function, output_dir=None):
    """Read the parameters file, for a specific command

    Parameters
    ----------
    parameters : str
        path to json file
    function : str
        one of the commands (grid2d, ecog, grid3d etc)
    output_dir : path
        path to use as output, if not specified in parameters

    Returns
    -------
    dict
        complete parameters
    """
    p_json = Path(parameters).resolve()
    with p_json.open() as f:
        parameters = load(f)

    required = REQUIRED[function].copy()
    if 'mri' in parameters:
        if parameters['mri'].get('func_file', None) is not None:
            required.append('functional')
        if parameters['mri'].get('pial_file', None) is not None:
            required.append('morphology')

    for k in required:
        if k not in parameters:
            raise ValueError(f'You need to specify "{k}" when running {function}')
        else:
            parameters[k] = validate_template(TEMPLATE[k], parameters[k])

    if output_dir is not None:
        output = output_dir
    elif 'output_dir' in parameters:
        output = parameters['output_dir']
    else:
        output = 'gridgen_output'

    parameters['output_dir'] = output
    parameters = convert_to_path(parameters, p_json.parent)
    parameters['output_dir'].mkdir(exist_ok=True, parents=True)

    # move one parameter to mri because it's more straightforward
    if parameters.get('mri', {}).get('func_file', None) is not None:
        parameters['mri']['func_threshold'] = parameters['functional'].pop('threshold', None)

    return parameters

def main():
    """Simple function to create parameters.md"""
    md = [
        '## Parameters',
        '',
        'Parameters should be saved as a `json` file.',
        'You can pass the parameters with the command `gridgen parameters.json func` where `func` is one of the possible functions (`grid2d`, `ecog`, `fit` etc).',
        'You can also use the command `gridgen parameters.json parameters` to generate a template `parameters.json` file.',
        '',
        ]
    md = md + help_template(TEMPLATE)
    parameters_file = PKG_PATH / 'docs' / 'parameters.md'

    with parameters_file.open('w') as f:
        f.write('\n'.join(md))

if __name__ == '__main__':
    main()


from layout import Layout
from pos import Pos
from size import Size

import pygame
import colour
import styles
import orientation

class ListLayout(Layout):
    def __init__(
        self,
        size,
        pos=Pos(0,0),
        border=0,
        spacing=0,
        orientation=orientation.VERTICAL,
        scaling=styles.FREE):

        super(ListLayout, self).__init__(size, pos, border)
        self.next_child_origin = Pos(
            self.origin.x + self.pos.x + self.border,
            self.origin.y + self.pos.y + self.border
        )
        self.orientation = orientation
        self.spacing = spacing
        self.scaling = scaling

    def add(self, child):

        if self.scaling:
            self._rescale_layout(child)

        self.children.append(child)
        child.origin = self.next_child_origin

        self._update_child_pos(child)

    def render(self, surface):
        rect = (
            self.origin.x + self.pos.x,
            self.origin.y + self.pos.y,
            self.size.w,
            self.size.h
        )
        pygame.draw.rect(surface, colour.RED, rect)

        self._render_children(surface)

    def _rescale_layout(self, child):
        if not self.children:
            self.size = Size(
                child.size.w + 2*self.border,
                child.size.h + 2*self.border
            )
        else:
            self.size = Size(
                self.size.w + (child.size.w + self.spacing)*int(self.orientation),
                self.size.h + (child.size.h + self.spacing)*int(not self.orientation)
            )

    def _update_child_pos(self, child):
        self.next_child_origin = Pos(
            self.next_child_origin.x + (child.size.w + self.spacing)*int(self.orientation),
            self.next_child_origin.y + (child.size.h + self.spacing)*int(not self.orientation)
        )


from django.shortcuts import render
from django.http import HttpResponse
import os
# Create your views here.

import csv
import json

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# print(BASE_DIR)
def index(request,):
	Budget = request.GET['Budget']
	path_to_file = BASE_DIR +'/data/Detail_Of_Places.csv'
	with open(path_to_file,'rt')as f:
		data = csv.reader(f)
		context = {
		'list_of_places': []
		}
		count =0
		for row in data:
			if(count == 0):
				count =1
				continue
			if(row[4]==''):

				row[4] = 0.0
			# print(type(int(float(row[4]))))
			# print(row[4])

			if(float(row[4])<= float(Budget)):
				context['list_of_places'].append({
					"nameOfPlace" : row[1],
					"linkOfImage" : row[2],
					"description" : row[3],
					"budget"	  : row[4]
					})

	return HttpResponse(json.dumps(context),content_type="application/json")


#!/usr/bin/env python

import rospy
from geometry_msgs.msg import PoseStamped
from styx_msgs.msg import Lane, Waypoint
from std_msgs.msg import Int32
from scipy import spatial
import numpy as np
import math

'''
This node will publish waypoints from the car's current position to some `x` distance ahead.

As mentioned in the doc, you should ideally first implement a version which does not care
about traffic lights or obstacles.

Once you have created dbw_node, you will update this node to use the status of traffic lights too.

Please note that our simulator also provides the exact location of traffic lights and their
current status in `/vehicle/traffic_lights` message. You can use this message to build this node
as well as to verify your TL classifier.

TODO (for Yousuf and Aaron): Stopline location for each traffic light.
'''

LOOKAHEAD_WPS = 100 # Number of waypoints we will publish. You can change this number
MAX_DECEL = 0.5
LOOP_RATE = 5

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

        #rospy.init_node('waypoint_updater',log_level=rospy.DEBUG)
        rospy.init_node('waypoint_updater')

        self.final_waypoints_pub = rospy.Publisher('/final_waypoints', Lane, queue_size=1)

        self.base_waypoints = None
        self.waypoints_2d   = None
        self.waypoint_tree  = None
        self.pose           = None

        self.stopline_idx = -1
        self.obstacle_idx = -1
        self.stop_buffer  = 2

        rospy.Subscriber('/base_waypoints', Lane, self.waypoints_cb)
        rospy.Subscriber('/current_pose', PoseStamped, self.pose_cb, queue_size=10)

        # these recieve only a waypoint index that refers back to the self.base_waypoints list
        rospy.Subscriber('/traffic_waypoint', Int32, self.traffic_cb, queue_size=10)

        # optional:
        #rospy.Subscriber('/obstacle_waypoint', Int32, self.obstacle_cb)

        self.loop()

    def loop(self):

        rate = rospy.Rate(LOOP_RATE)
        while not rospy.is_shutdown():
            if self.pose and self.base_waypoints and self.waypoint_tree:
                closest_waypoint_idx = self.get_closest_waypoint_idx()
                #rospy.logerr("cwp %s",closest_waypoint_idx)
                self.publish_waypoints(closest_waypoint_idx)
            rate.sleep()

    def get_closest_waypoint_idx(self):

        x = self.pose.pose.position.x
        y = self.pose.pose.position.y

        if self.waypoint_tree == None:
            rospy.logerr("error: get_closest_waypoint_idx - waypoint_tree (kdtree) not assigned")
            return

        closest_idx = self.waypoint_tree.query([x,y],1)[1]

        # ahead or behind?
        closest_coord = self.waypoints_2d[closest_idx]
        prev_coord = self.waypoints_2d[closest_idx-1]

        # Equation for hyperplane through closest_coords
        cl_vect = np.array(closest_coord)
        prev_vect = np.array(prev_coord)
        pos_vect = np.array([x, y])

        val = np.dot(cl_vect - prev_vect, pos_vect - cl_vect)

        if val > 0:
            closest_idx = (closest_idx + 1) % len(self.waypoints_2d)

        return closest_idx

    def publish_waypoints(self, closest_idx):

        #rospy.logerr("--- closest: %s: ",closest_idx)

        lane = Lane()
        lane.header = self.base_waypoints.header

        if self.base_waypoints.waypoints == None:
            rospy.logerr("error: self.base_waypoints.waypoints== none!")
            return

        size = len(self.base_waypoints.waypoints)
        sl_idx = self.stopline_idx

        lane.waypoints = self.base_waypoints.waypoints[closest_idx:closest_idx+LOOKAHEAD_WPS]

        if sl_idx >= 0 and sl_idx < size:
            #rospy.logerr("decelerate waypoint")
            lane.waypoints = self.decelerate_waypoints(closest_idx,lane.waypoints)

        self.final_waypoints_pub.publish(lane)

    def pose_cb(self, msg):
        self.pose = msg

    def waypoints_cb(self, waypoints):

        #rospy.logerr("wp updater: waypoints_cp")
        size = len(waypoints.waypoints)

        # make a copy as they are only sent once
        self.base_waypoints = waypoints

        #use scipi KDTree to get closes waypoint
        if not self.waypoints_2d:
             self.waypoints_2d = [[waypoint.pose.pose.position.x,waypoint.pose.pose.position.y] for waypoint in waypoints.waypoints]
             self.waypoint_tree = spatial.KDTree(self.waypoints_2d)
        else:
            rospy.logerr("self.waypoints_2d already assigned?: %s",self.waypoints_2d)

    def traffic_cb(self, msg):
        self.stopline_idx = msg.data
        #rospy.logerr("waypoint_updater: way point index %s ",self.stopline_idx)

    # def obstacle_cb(self, msg):
    #     self.obstacle_idx = msg

    def get_waypoint_velocity(self, waypoint):
        return waypoint.twist.twist.linear.x

    def set_waypoint_velocity(self, waypoints, waypoint, velocity):
        waypoints[waypoint].twist.twist.linear.x = velocity

    def distance(self, waypoints, wp1, wp2):
        dist = 0
        dl = lambda a, b: math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2  + (a.z-b.z)**2)
        for i in range(wp1, wp2+1):
            dist += dl(waypoints[wp1].pose.pose.position, waypoints[i].pose.pose.position)
            wp1 = i
        return dist

    def decelerate_waypoints(self,closest_idx,wp_in):

        wp_out = []

        for i, wp in enumerate(wp_in):

            p = Waypoint()
            p.pose = wp.pose

            stop_idx = max(self.stopline_idx - closest_idx - self.stop_buffer,0)

            dist = self.distance(self.base_waypoints.waypoints, i, stop_idx)
            vel = math.sqrt(2*MAX_DECEL*dist)
            if vel < 1.0:
                vel = 0

            p.twist.twist.linear.x = min(vel, wp.twist.twist.linear.x)
            wp_out.append(p)

        return wp_out

if __name__ == '__main__':
    try:
        WaypointUpdater()
    except rospy.ROSInterruptException:
        rospy.logerr('Could not start waypoint updater node.')


import plotly.graph_objects as go
import csv
from datetime import datetime

Alphabet = []
filename = 'data/Eminence_Data.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    # gets date, high, and low temps
    dates, rains = [], []
    for row in reader:
        current_date = datetime.strptime(row[1], '%Y-%m-%d')
        try:
            high = float(row[9])
        except ValueError:
            print(f"Missing data for {current_date}")
        else:
            dates.append(current_date)
            rains.append(high)

for i in rains:
    Alphabet.append(rains[0:5])
    del rains[0:5]

headerColor = '#00AADD'
rowEvenColor = '#BD5E00'
rowOddColor = '#EB7500'

fig = go.Figure(data=[go.Table(
    header=dict(values=['DATES', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021'],
                line_color='black',
                fill_color='darkorange',
                align='left'),
    cells=dict(values=[['July 21', 'July 22', 'July 23', 'July 24' , 'July 25'],
                       Alphabet[0],
                       Alphabet[1],
                       Alphabet[2],
                       Alphabet[3],
                       Alphabet[4],
                       Alphabet[5],
                       Alphabet[6],
                       Alphabet[7],
                       Alphabet[8],
                       Alphabet[9]],
               line_color='black',
               fill_color='orange',
               align='left'))
])

fig.show()


"""
CH23. 85_Fibonacci_Number.py
"""

import collections
import enum
import heapq
import functools
import itertools
import re
import sys
import math
import bisect

# 입력 1
input1 = 5

# 출력
output1 = 5

# 입력 2
input2 = 10

# 출력 2
output2 = 55

# --------------------------------------------------
def fib(N):
    if N <= 1:
        return N
    return fib(N-1) + fib(N-2)

print(fib(input1))
print(fib(input1) == output1)
print(fib(input2))
print(fib(input2) == output2)

# --------------------------------------------------
dp = collections.defaultdict(int)
def fib2(N):
    global dp
    if N <= 1:
        return N

    if dp[N]:
        return dp[N]
    dp[N] = fib2(N-1) + fib2(N-2)
    return dp[N]

print(fib2(input1))
print(fib2(input1) == output1)
print(fib2(input2))
print(fib2(input2) == output2)

# --------------------------------------------------
class Solution:
    dp = collections.defaultdict(int)

    def fib2_1(self, N):
        if N <= 1:
            return N

        if self.dp[N]:
            return self.dp[N]

        self.dp[N] = self.fib2_1(N-1) + self.fib2_1(N-2)
        return self.dp[N]

sol = Solution()

print(sol.fib2_1(input1))
print(sol.fib2_1(input1) == output1)
print(sol.fib2_1(input2))
print(sol.fib2_1(input2) == output2)

# --------------------------------------------------
dp = collections.defaultdict(int)
def fib3(N):
    global dp
    dp[0] = 0
    dp[1] = 1

    for i in range(2, N + 1):
        dp[i] = dp[i-1] + dp[i-2]
    return dp[N]

print(fib3(input1))
print(fib3(input1) == output1)
print(fib3(input2))
print(fib3(input2) == output2)

# --------------------------------------------------
class Solution:
    dp = collections.defaultdict(int)

    def fib3_1(self, N):
        self.dp[0] = 0
        self.dp[1] = 1

        for i in range(2, N+1):
            self.dp[i] = self.dp[i-1] + self.dp[i-2]
        return self.dp[N]

sol = Solution()

print(sol.fib3_1(input1))
print(sol.fib3_1(input1) == output1)
print(sol.fib3_1(input2))
print(sol.fib3_1(input2) == output2)

# --------------------------------------------------
def fib4(N):
    x, y = 0, 1
    for i in range(2, N+1):
        x, y = y, x+y
    return y

print(fib4(input1))
print(fib4(input1) == output1)
print(fib4(input2))
print(fib4(input2) == output2)

# --------------------------------------------------
import numpy as np

def fib5(N):
    M = np.matrix([[0, 1], [1, 1]])
    print(M)
    vec = np.array([[0], [1]])

    return np.matmul(M ** N, vec)[0]

print(fib5(input1))
print(fib5(input1) == output1)
print(fib5(input2))
print(fib5(input2) == output2)


from unittest.mock import patch

from sqlalchemy import func
from truth.truth import AssertThat

from freshcart.lib.embedder import embed_product
from freshcart.lib.models import db
from freshcart.lib.models.products import Product
from freshcart.tests import AppTestCase, DbMixin

class TestEmbedder(DbMixin, AppTestCase):
    def test_embedder_product_does_not_exist(self):
        embed_product(1)

        AssertThat(
            db.session.query(func.count(Product.id)).scalar()
        ).IsEqualTo(0)

    def test_embedder_product_already_embedded(self):
        embedding = embedding = [1] * 1536
        product = Product(name="test", embedding=embedding)
        db.session.add(product)
        db.session.commit()

        embed_product(product.id)

        AssertThat(
            Product.query.get(product.id).embedding
        ).ContainsExactlyElementsIn(embedding).InOrder()

    @patch("freshcart.lib.embedder.embed_text", return_value=[2] * 1536)
    def test_embedder_product_not_embedded(self, _):
        product = Product(name="test")
        db.session.add(product)
        db.session.commit()

        embed_product(product.id)

        AssertThat(
            Product.query.get(product.id).embedding
        ).ContainsExactlyElementsIn([2] * 1536).InOrder()


# -*- coding: utf-8 -*-
"""
Created on Thu Jun  6 06:03:15 2019

@author: hp
"""

my_image = "cat1.jpg" # change this to the name of your image file
my_label_y = [1] # the true class of your image (1 -> cat, 0 -> non-cat)
from sklearn.externals import joblib
parameters=joblib.load('model.pkl')

fname = "images/" + my_image
image = np.array(ndimage.imread(fname, flatten=False))
my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((num_px*num_px*3,1))
my_image = my_image/255.
my_predicted_image = predict(my_image, my_label_y, parameters)

plt.imshow(image)
print ("y = " + str(np.squeeze(my_predicted_image)) + ", your L-layer model predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") +  "\" picture.")


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/3/13 20:27

#import some necessary librairies

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt  # Matlab-style plotting
import seaborn as sns
color = sns.color_palette()
sns.set_style('darkgrid')
import warnings
def ignore_warn(*args, **kwargs):
    pass
warnings.warn = ignore_warn #ignore annoying warning (from sklearn and seaborn)
from scipy import stats
from scipy.stats import norm, skew #for some statistics
pd.set_option('display.float_format', lambda x: '{:.3f}'.format(x)) #Limiting floats output to 3 decimal points
from subprocess import check_output
# print(check_output(["ls", "../input"]).decode("utf8")) #check the files available in the directory

train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')
# print(train.head(5))

print("The train data size before dropping Id feature is : {} ".format(train.shape))
print("The test data size before dropping Id feature is : {} ".format(test.shape))

train.drop("Id", axis = 1, inplace = True)
test.drop("Id", axis = 1, inplace = True)

print("\nThe train data size after dropping Id feature is : {} ".format(train.shape))
print("The test data size after dropping Id feature is : {} ".format(test.shape))

# fig, ax = plt.subplots()
# ax.scatter(x = train['GrLivArea'], y = train['SalePrice'])
# plt.ylabel('SalePrice', fontsize=13)
# plt.xlabel('GrLivArea', fontsize=13)
# plt.show()

# var = 'GrLivArea'
# # data = pd.concat([train['SalePrice'],train[var]],1)
# f, ax = plt.subplots()
# plt.scatter(x=train['GrLivArea'],y=train['SalePrice'])
# plt.show()

print(train.shape)
train = train.drop(train[(train['GrLivArea'] > 4000) & (train['SalePrice'] < 300000)].index)
print(train.shape)

# 观察SalePrcie数据分布
# sns.distplot(train['SalePrice'] , fit=norm)
#
# # Get the fitted parameters used by the function
# (mu, sigma) = norm.fit(train['SalePrice'])
# print( '\n mu = {:.2f} and sigma = {:.2f}\n'.format(mu, sigma))
#
# #Now plot the distribution
# plt.legend(['Normal dist. ($\mu=$ {:.2f} and $\sigma=$ {:.2f} )'.format(mu, sigma)],loc='best')
# plt.ylabel('Frequency')
# plt.title('SalePrice distribution')
#
# #Get also the QQ-plot
# fig = plt.figure()
# res = stats.probplot(train['SalePrice'], plot=plt)
# plt.show()

# 进行log变换
train['SalePrice'] = np.log(train['SalePrice'])
# sns.distplot(train['SalePrice'],fit=norm)
# (mu,sigma) = norm.fit(train['SalePrice'])
# print('the $\mu$ = {:.2f} \n the $\sigma$ = {:.2f}'.format(mu,sigma))
# plt.legend(['Norm distribution( $\mu$ = {:.2f} , $\sigma$ = {:.2f})'.format(mu,sigma)],loc='best')
# plt.ylabel('Frequency')
# plt.xlabel('SalePrice')
# plt.title('SalePrice distribution')
# fig = plt.figure()
# stats.probplot(train['SalePrice'],plot=plt)
# plt.show()

all_data = pd.concat([train,test],ignore_index=True)
ntrain = train.shape[0]
ntest = test.shape[0]
y_train = train.SalePrice.values
all_data.drop('SalePrice',1,inplace=True)
print(all_data.shape)

# 处理缺失数据
all_data_na = (all_data.isnull().sum() / len(all_data)) *100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio':all_data_na})
# print(missing_data.head(20))

# 画数据丢失率
# sns.barplot(all_data_na.index,all_data_na.values)
# plt.xticks(rotation=90)
# plt.title('data missing percent')
# plt.show()

# 画数据相关性图
corrmat = train.corr()
# plt.subplots(figsize=(12,9))
# sns.heatmap(corrmat,vmax=0.9,square=True)
# plt.xticks(rotation=90)
# plt.yticks(rotation=360)
# plt.show()

# print(np.abs(corrmat['SalePrice']).sort_values())
var = ['PoolQC','MiscFeature','Alley','Fence','FireplaceQu','GarageType', 'GarageFinish', 'GarageQual', 'GarageCond',
       'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2','MSSubClass', 'MasVnrType']
# all_data['PoolQC'] = all_data['PoolQC'].fillna('None')
for v in var:
    all_data[v] = all_data[v].fillna('None')
# print('为null的值',max(all_data[var].isnull().sum()))

all_data['LotFrontage'] = all_data.groupby('Neighborhood')['LotFrontage'].transform(lambda x:x.fillna(x.median()))
var = ['GarageYrBlt', 'GarageArea', 'GarageCars','BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF',
       'BsmtFullBath', 'BsmtHalfBath','MasVnrArea']
for v in var:
    all_data[v] = all_data[v].fillna(0)

var = ['Electrical','MSZoning','KitchenQual','Exterior1st','Exterior2nd','SaleType']
for v in var:
    all_data[v] = all_data[v].fillna(all_data[v].mode()[0])

# print(all_data['Utilities'].value_counts())
all_data.drop('Utilities',1,inplace=True)

all_data["Functional"] = all_data["Functional"].fillna("Typ")
print(max(all_data.isnull().sum()))

#MSSubClass=The building class
all_data['MSSubClass'] = all_data['MSSubClass'].astype(str)

#Changing OverallCond into a categorical variable
all_data['OverallCond'] = all_data['OverallCond'].astype(str)

#Year and month sold are transformed into categorical features.
all_data['YrSold'] = all_data['YrSold'].astype(str)
all_data['MoSold'] = all_data['MoSold'].astype(str)

from sklearn.preprocessing import LabelEncoder

# my1 = pd.get_dummies(all_data).to_csv('my1.csv')
columns = ('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',
        'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',
        'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
        'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',
        'YrSold', 'MoSold')
# columns = all_data.columns.values
# print(type(all_data['Street'].values))
for column in columns:
    lb_make = LabelEncoder()
    all_data[column] = lb_make.fit_transform(all_data[column].values)
print('Shape all_data: {}'.format(all_data.shape))

all_data['TotalSF'] = all_data['TotalBsmtSF'] + all_data['1stFlrSF'] + all_data['2ndFlrSF']

# 算一下数据斜度
numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
skewed_feats = all_data[numeric_feats].apply(skew).sort_values(ascending=False)
print("\nSkew in numerical features: \n")
skewness = pd.DataFrame({'Skew' :skewed_feats})

# 进行box-cox变换
skewness = skewness[abs(skewness)>0.75]
# print('斜度大于0.75的有{}个'.format(skewness.shape[0]))

from scipy.special import boxcox1p
skewed_features = skewness.index
lam = 0.15
for feat in skewed_features:
    #all_data[feat] += 1
    all_data[feat] = boxcox1p(all_data[feat], lam)

all_data = pd.get_dummies(all_data)
print(all_data.shape)

# 建立模型

from sklearn.linear_model import ElasticNet, Lasso,  BayesianRidge, LassoLarsIC
from sklearn.ensemble import RandomForestRegressor,  GradientBoostingRegressor
from sklearn.kernel_ridge import KernelRidge
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
from sklearn.model_selection import KFold, cross_val_score, train_test_split
from sklearn.metrics import mean_squared_error
import xgboost as xgb
import lightgbm as lgb

train = all_data[:ntrain]
test = all_data[ntrain:]
# 交叉验证
n_folds = 5
def rmsle_cv(model):
    kf = KFold(n_folds, shuffle=True, random_state=42)
    rmse = np.sqrt(-cross_val_score(model, train.values, y_train, scoring='neg_mean_squared_error',cv=kf))
    return rmse

# 基本模型
# LASSO Regression :拉索回归
lasso = make_pipeline(RobustScaler(), Lasso(alpha =0.0005, random_state=1))

#Elastic Net Regression :弹性网络回归
ENet = make_pipeline(RobustScaler(), ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))

# Kernel Ridge Regression :内核岭回归
KRR = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
                                   max_depth=4, max_features='sqrt',
                                   min_samples_leaf=15, min_samples_split=10,
                                   loss='huber', random_state =5)

#Gradient Boosting Regression : :
GBoost = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
                                   max_depth=4, max_features='sqrt',
                                   min_samples_leaf=15, min_samples_split=10,
                                   loss='huber', random_state =5)

#XGBoost :
model_xgb = xgb.XGBRegressor(colsample_bytree=0.4603, gamma=0.0468,
                             learning_rate=0.05, max_depth=3,
                             min_child_weight=1.7817, n_estimators=2200,
                             reg_alpha=0.4640, reg_lambda=0.8571,
                             subsample=0.5213, silent=1,
                             random_state =7, nthread = -1)

#LightGBM :
model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=5,
                              learning_rate=0.05, n_estimators=720,
                              max_bin = 55, bagging_fraction = 0.8,
                              bagging_freq = 5, feature_fraction = 0.2319,
                              feature_fraction_seed=9, bagging_seed=9,
                              min_data_in_leaf =6, min_sum_hessian_in_leaf = 11)

# 单个模型的结果
# score = rmsle_cv(lasso)
# print("\nLasso score: {:.4f} ({:.4f})\n".format(score.mean(), score.std()))
# score = rmsle_cv(ENet)
# print("ElasticNet score: {:.4f} ({:.4f})\n".format(score.mean(), score.std()))
# score = rmsle_cv(KRR)
# print("Kernel Ridge score: {:.4f} ({:.4f})\n".format(score.mean(), score.std()))
# score = rmsle_cv(GBoost)
# print("Gradient Boosting score: {:.4f} ({:.4f})\n".format(score.mean(), score.std()))
# score = rmsle_cv(model_xgb)
# print("Xgboost score: {:.4f} ({:.4f})\n".format(score.mean(), score.std()))
# score = rmsle_cv(model_lgb)
# print("LGBM score: {:.4f} ({:.4f})\n" .format(score.mean(), score.std()))

# class AveragingModels(BaseEstimator, RegressorMixin, TransformerMixin):
#     def __init__(self, models):
#         self.models = models
#     def fit(self,X,y):
#         self.models_ = [clone(x) for x in self.models]
#
#         for model in self.models_:
#             model.fit(X,y)
#         return self
#
#     def predict(self,X):
#         predictions = np.column_stack([model.predict(X) for model in self.models_])
#         return np.mean(predictions, axis=1)

    # def score(self, X, y, sample_weight=None):
    #     return np.mean(self.predict(X))

# averaged_models = AveragingModels(models = (ENet, GBoost, KRR, lasso))
# score = rmsle_cv(averaged_models)
# print(score.mean(),score.std())

class StackingAveragedModels(BaseEstimator, RegressorMixin, TransformerMixin):
    def __init__(self, base_models, meta_model, n_folds=5):
        self.base_models = base_models
        self.meta_model = meta_model
        self.n_folds = n_folds

    # We again fit the data on clones of the original models
    def fit(self, X, y):
        self.base_models_ = [[]] * len(self.base_models)
        self.meta_model_ = clone(self.meta_model)
        kfold = KFold(n_splits=self.n_folds, shuffle=True, random_state=156)

        # Train cloned base models then create out-of-fold predictions
        # that are needed to train the cloned meta-model
        out_of_fold_predictions = np.zeros((X.shape[0], len(self.base_models)))
        for i, model in enumerate(self.base_models):
            for train_index, holdout_index in kfold.split(X, y):
                instance = clone(model)
                self.base_models_[i].append(instance)
                instance.fit(X[train_index], y[train_index])
                y_pred = instance.predict(X[holdout_index])
                out_of_fold_predictions[holdout_index, i] = y_pred

        # Now train the cloned  meta-model using the out-of-fold predictions as new feature
        self.meta_model_.fit(out_of_fold_predictions, y)
        return self

    # Do the predictions of all base models on the test data and use the averaged predictions as
    # meta-features for the final prediction which is done by the meta-model
    def predict(self, X):
        # d = len(self.base_models_)
        # for base_models in self.base_models_:
        #     a = [model.predict(X) for model in base_models]
        #     c = len(base_models)
        #     for model in base_models:
        #         b = model.predict(X)
        #         pass
        meta_features = np.column_stack([np.column_stack([model.predict(X) for model in base_models]).mean(axis=1)
                                         for base_models in self.base_models_])
        return self.meta_model_.predict(meta_features)

stacked_averaged_models = StackingAveragedModels(base_models = (ENet, GBoost, KRR, GBoost, model_xgb, model_lgb),
                                                 meta_model = lasso)

score = rmsle_cv(stacked_averaged_models)
stacked_averaged_models.fit(train.values,y_train)
final_result = stacked_averaged_models.predict(train.values)
final_result = pd.DataFrame({'final_result':final_result})
final_result.to_csv('result.csv')
print("Stacking Averaged models score: {:.4f} ({:.4f})".format(score.mean(), score.std()))


from collections import deque

with open('input.txt') as f:
    lines = f.read().split('\n')[:-2]

heights = []
start_x = 0
start_y = 0
end_x = 0
end_y = 0

for y, line in enumerate(lines):
    heights.append([])

    for x, c in enumerate(line):
        if c == 'S':
            start_x = x
            start_y = y
            c = 'a'

        if c == 'E':
            end_x = x
            end_y = y
            c = 'z'

        heights[-1].append(ord(c) - ord('a'))

h = len(heights)
w = len(heights[0])

q = deque([(start_x, start_y)])
distance_to = {(start_x, start_y): 0}

while len(q):
    point = q.popleft()
    x, y = point

    for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        new_x = x + dx
        new_y = y + dy

        if not (0 <= new_x < w and 0 <= new_y < h):
            continue

        new_point = new_x, new_y

        if new_point in distance_to:
            continue

        if heights[new_y][new_x] - heights[y][x] > 1:
            continue

        distance_to[new_point] = distance_to[point] + 1
        q.append(new_point)

print(distance_to[(end_x, end_y)])


import os
import json
import pickle

text_var = 'Данные для практической работы 2/4/products_3.pkl'
file_products = os.path.join(os.path.dirname(__file__), os.path.normpath(text_var))
text_var = 'Данные для практической работы 2/4/price_info_3.json'
file_change = os.path.join(os.path.dirname(__file__), os.path.normpath(text_var))

result = os.path.join(os.path.dirname(__file__), os.path.normpath('price_change.pkl'))

def changes(prod, change):
    # print(prod['price'], 'start')
    if change['method'] == 'sum':
        prod['price'] += change['param']
    elif change['method'] == 'sub':
        prod['price'] -= change['param']
    elif change['method'] == 'percent+':
        prod['price'] *= (change['param'] + 1)
    elif change['method'] == 'percent-':
        prod['price'] *= (1 - change['param'])
    # print(prod['price'])

with open(file_products, "rb") as file:
    products = pickle.load(file)

with open(file_change) as file:
    info = json.load(file)

info_dict = {}
for item in info:
    info_dict[item["name"]] = item

for prod in products:
    changes(prod, info_dict[prod['name']])

# print(products)

with open(result, "wb") as file:
    file.write(pickle.dumps(products))


import requests
import json
from flask import Flask, jsonify, request
from flask_cors import CORS
from PIL import Image
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.applications.resnet_v2 import preprocess_input

app = Flask(__name__)
CORS(app)
# api = Api(app)

@app.route('/predict', methods=['POST'])
def predict_label():
    if 'image' not in request.files:
        return jsonify({"error": "No image part"})

    file = request.files['image']

    if file.filename == '':
        return jsonify({"error": "No selected image"})

    # Load the trained model
    model = load_model('covid_detection_model.h5')

    # Load and preprocess the new image
    # image_path = '/content/Data/test/COVID19/COVID19(460).jpg'
    # input_size = (128, 128)

    # image = load_img(image_path, target_size=input_size)

    image = Image.open(file)
    input_size = (128, 128)
    image = image.resize(input_size)

    image_array = img_to_array(image)
    image_array = preprocess_input(image_array)

    # Expand dimensions to match the input shape expected by the model
    image_array = np.expand_dims(image_array, axis=0)

    # Make predictions
    predictions = model.predict(image_array)

    # Assuming predictions is a one-hot encoded array, you can get the predicted class index
    predicted_class_index = np.argmax(predictions)

    class_labels = ["COVID19", "NORMAL", "PNEUMONIA"]
    predicted_class_label = class_labels[predicted_class_index]

    print("Predicted Class:", predicted_class_label)
    print("Predictions:", predictions)

    return jsonify({"predicted_class": predicted_class_label})

# api.add_resource(TestClass, "/next")

if __name__ == "__main__":
    app.run(debug=False,host='0.0.0.0')


"""
Comic Maker Helper Script

In one command this script:
  - Sets up the venv
  - Runs the tool
  - Runs the test server.
"""

from pathlib import Path
import os, http.server, socketserver, platform

def setup_venv():
    print("Checking environment...")
    if Path("venv").exists():
        print("Found existing venv")
        return

    print("Setting up new venv...")

    if "Windows" in platform.system():
        os.system("python -m venv venv")
        os.system(".\\venv\\Scripts\\pip install -r requirements.txt")
    else:
        os.system("python3 -m venv ./venv")
        os.system("./venv/bin/pip install -r requirements.txt")

def run_server(outdir="dest"):
    os.chdir(outdir)

    print("Starting preview website on http://localhost:8000")
    print("Note this website will be slower than a real one.")
    print("Close the window to exit (or press Ctrl+C)")

    Handler = http.server.SimpleHTTPRequestHandler

    class ReuseAddrServer(socketserver.TCPServer):
        allow_reuse_address = True

    httpd = ReuseAddrServer(("", 8000), Handler)
    try:
        with httpd:
            httpd.serve_forever()
    except KeyboardInterrupt:
        print("Server stopped.")

def run_comic_maker():

    print()
    print("Running the comic maker.")
    print("Please place your images in the comic_pages folder,")
    print("and press enter when ready!")
    input()

    if "Windows" in platform.system():
        os.system("venv\\Scripts\\python comicmaker.py")
    else:
        os.system("./venv/bin/python comicmaker.py")

    print()
    print("Your website files are now available in the dest folder.")
    print()

if __name__ == '__main__':
    setup_venv()
    run_comic_maker()
    run_server()