# ------------------------------------------------------------------------------
# Project : CNTree                                                 /          \
# Filename: cntree_cli.py                                         |     ()     |
# Date    : 09/02/2023                                            |            |
# Author  : cyclopticnerve                                        |   \____/   |
# License : WTFPLv2                                                \          /
# ------------------------------------------------------------------------------

"""
    This module handles the command line for creating a tree using cntree
"""

# ------------------------------------------------------------------------------
# Imports
# ------------------------------------------------------------------------------

# system imports
import argparse

# local imports
import cntree

# ------------------------------------------------------------------------------
# Constants
# ------------------------------------------------------------------------------

# the description to print in help
__CN_SHORT_DESC__   = \
"""
This program creates a tree of the specified directory, with names being
formatted according to the specified formats, and paths being ignored by the
filter list.
"""

# the version to print in help
__CN_VERSION__      = 'Version 0.0.1'

# a string to combine description and version strings
DV_STR              = f'{__CN_SHORT_DESC__}\n{__CN_VERSION__}'

# print this at the end of help
EPILOG              = \
"""
The format strings for directory and file names will have the value of '$NAME'
replaced by the directory or file name.
Example:
    cntree_cli.py -d " [] $NAME"
    item name = "Foo"
    result = " [] Foo"

Also, leading spaces in the directory format, when applied to the initial
directory, will be left-trimmed to make the name start at the first column.

Items in the filter list will be skipped. These items should be directory or
file paths relative to the start directory.
Example:
    cntree_cli.py -l "Foo/bar.txt,Foo"
    An entry of 'Foo/bar.txt' will skip a file with the absolute path '<start
    dir>/Foo/bar.txt'. An entry of 'Foo' (if it points to a directory) will skip
    a directory with the absolute path '<start dir>/Foo/'and everything under
    it.

-)
"""

# ------------------------------------------------------------------------------
# A dummy class to combine argparse formatters
# ------------------------------------------------------------------------------
class CNFormatter(
    argparse.RawTextHelpFormatter,          # format argument help
    argparse.RawDescriptionHelpFormatter,   # format description/version/epilog
    # argparse.ArgumentDefaultsHelpFormatter, # add default values to arg help
):
    """
        A dummy class to combine argparse formatters
    """

# ------------------------------------------------------------------------------
# This code runs when the file is called from the command line
# ------------------------------------------------------------------------------
if __name__ == '__main__':

    # create parser
    parser = argparse.ArgumentParser(
        description=DV_STR,             # shown in help (-h) and version (-v)
        epilog=EPILOG,                  # shown in help (-h)
        formatter_class=CNFormatter,    # the combined formatters above
    )

    # version arg
    # NB: you should NOT include any other params to add_argument(), as
    # VersionAction will choke on them
    parser.add_argument(
        '-v',
        action='version',
        version=DV_STR,
    )

# -----------------------------------------------------------

    # parser.add_argument(
    #     '-s',                   # what to pass on command line
    #     action='store'
    #     choices=['1', '2'],
    #     const='True',           # value to store if option is flag-only
    #     default='',             # value when not in cmdline (defaults to None)
    #     dest='DEST',            # the prop name in Namespace (not user facing)
    #     # defaults to name of arg, prefer long name, ie 'start_dir' or 's')
    #     help='start_dir help',  # help string (duh)
    #     metavar='start',        # name of value (user facing, used in help)
    #     nargs='?',
    #     required=True,
    #     type='string',
    # )

    # usage: %(prog)s [-o, --option <metavar>] pos

    parser.add_argument(    # positional
        'directory',
        help='the path to the root directory of the tree',
    )

    # dir_format
    parser.add_argument(    # optional
        '-d',
        help='the format string to use for directory names',
        metavar='',
    )

    # file_format
    parser.add_argument(    # optional
        '-f',
        help='the format string to use for file names',
        metavar='',
    )

    # filter_list
    parser.add_argument(    # optional
        '-l',
        help='a list of directory/file names to filter',
        metavar='',
    )

# -----------------------------------------------------------

    args = parser.parse_args()
    # print(args)

# -----------------------------------------------------------

    # we need to convert list param (a string) into a real list
    LIST_FILTER = None
    if args.l:
        LIST_FILTER = args.l.split(',')
        # print(LIST_FILTER)

    tree = cntree.CNTree()

    TREE = tree.build_tree(
        start_dir=args.directory,
        filter_list=LIST_FILTER,
        dir_format=args.d,
        file_format=args.f,
    )

    print(TREE)

# start_dir
#     skip      (nice error), need param
#     none      (not nice, not a dir)
#     ''        (ok, use current)
#     invalid   (not nice, not a dir)
#     full      (ok)
#     rel       (ok)
# filter
#     skip
#     none
#     []
#     dir
#     file
#     dir/file
#     invalid
#     rel
#     abs
# dir_format
#     skip      (ok)
#     none      (ok)
#     ''        (ok)
#     no $NAME  (ok)
# file_format
#     skip
#     none
#     ''
#     no $NAME

# -----------------------------------------------------------

# action
# Specify how an argument should be handled
# 'store', 'store_const', 'store_true', 'append', 'append_const', 'count'
# 'help', 'version'

# choices
# Limit values to a specific set of choices
# ['foo', 'bar'], range(1, 10), or Container instance

# const
# Store a constant value

# default
# Default value used when an argument is not provided
# Defaults to None

# dest
# Specify the attribute name used in the result namespace

# help
# Help message for an argument

# metavar
# Alternate display name for the argument as shown in help

# nargs
# Number of times the argument can be used
# int, '?', '*', or '+'

# required
# Indicate whether an argument is required or optional
# True or False

# type
# Automatically convert an argument to the given type
# int, float, argparse.FileType('w'), or callable function

# -----------------------------------------------------------

# description = DESCRIPTION\nVERSION
# version = DESCRIPTION\nVERSION
# needs RawDescriptionHelpFormatter


from bs4 import BeautifulSoup
import requests

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'}

class Ders(object):
    def __init__(self,derskodu,dersadi,kredi,yariyil):
        self.kod=derskodu
        self.ad=dersadi
        self.kredi=kredi
        self.yariyil=yariyil
        self.__harfnotu="G"

    def __str__(self):
        return "{} {} {} {}".format(self.kod,self.ad,self.yariyil,self.kredi)

    @property
    def harfnotu(self,yeninot):
        self.__harfnotu=yeninot

    @harfnotu.getter
    def harfnotu(self):
        return self.__harfnotu

def dersPlaniCek(plan_url):
    dersler_listesi = []
    dersplani_sayfasi = requests.get(plan_url,headers=headers)
    dersplani_soup = BeautifulSoup(dersplani_sayfasi.content, "lxml")
    donemler = dersplani_soup.findAll("table",attrs={"class":"plan"})
    for donem in donemler:
        satirlar = donem.findAll("tr")
        satirlar.pop(0)
        for satir in satirlar:
            kolonlar = satir.findAll("td")
            if(len(kolonlar[0])>1):
                dersler_listesi.append(Ders(*[kolon.text for i,kolon in enumerate(kolonlar) if (i==0 or i==1 or i==2 or i==9) ]))
            elif("Seçime Bağlı" in kolonlar[1].text):
                secmeliders_url=plan_url[:-11]+kolonlar[1].find("a")['href']
                print(secmeliders_url)
                secmeliders_sayfasi = requests.get(secmeliders_url, headers=headers)
                secmeliders_soup = BeautifulSoup(secmeliders_sayfasi.content, "lxml")
                secmelisatirlar = secmeliders_soup.find("table", attrs={"class": "plan"}).find_all("tr")
                secmelisatirlar.pop(0)
                for secmelisatir in secmelisatirlar:
                    secmelikolonlar = secmelisatir.findAll("td")
                    __kod,__ad,__kredi,__donem="","","",""
                    for secmeli_i,secmelikolon in enumerate(secmelikolonlar):
                        if(secmeli_i==0):
                            __kod=secmelikolon.text
                        elif(secmeli_i==1):
                            __ad=secmelikolon.text
                        elif(secmeli_i==2):
                            __kredi=secmelikolon.text
                    __donem=kolonlar[9].text
                    dersler_listesi.append(Ders(__kod,__ad,__kredi,__donem))

    return dersler_listesi


from setuptools import setup, find_packages

requires = [
    'django>=1.8',
]

setup(
    name='beproud.django.basemodels',
    version='0.1.0',
    description='beproud.django.basemodels provides base models and fields, that comes from bpcommons.',
    author='BeProud Inc.',
    author_email='xiao@beproud.jp',
    url='http://www.beproud.jp/',
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Environment :: Plugins',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Programming Language :: Python',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ],
    packages=find_packages(),
    namespace_packages=[
        'beproud',
        'beproud.django',
    ],
    install_requires=requires,
    zip_safe=False,
)


import os
import re
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
import argparse

def parse_file(file):
    with open(file, 'r') as f:
        lines = [line.strip() for line in f.readlines()]

    data = []
    pattern = "Average elasped time: \((.*?)\) second, performance: \((.*?)\) GFLOPS. size: \((.*?)\)."
    for line in lines:
        r = re.match(pattern, line)
        if r:
            gflops = float(r.group(2))
            data.append(gflops)
    return data

def plot(num1, num2, y1, y2, save_dir):
    x = [(i + 1) * 256 for i in range(len(y1))]
    fig = plt.figure(figsize=(12, 10))
    if num1 == 0:
        num1 = "culas"

    plt.plot(x, y1, c='k', linewidth=2, label=f"kernel_{num1}")
    plt.plot(x, y2, c='b', linewidth=2, label=f"kernel_{num2}")
    plt.legend()

    plt.scatter(x, y1, marker="s", s=60, c='', edgecolors='k', linewidth=2)
    plt.scatter(x, y2, marker="^", s=60, c='', edgecolors='b', linewidth=2)

    plt.tick_params(labelsize=10)
    plt.xlabel("Matrix size (M=N=K)", fontsize=12, fontweight='bold')
    plt.ylabel("Performance (GFLOPS)", fontsize=12, fontweight='bold')

    plt.title(f"Comparison bewteen: kernel_{num1} and kernel_{num2}", fontsize=16, fontweight='bold')

    x_major_locator = MultipleLocator(256)
    plt.gca().xaxis.set_major_locator(x_major_locator)

    plt.savefig(f"{save_dir}/kernel_{num1}_vs_{num2}.png")

def main(args):
    root = os.path.dirname(os.path.abspath(__file__))
    data1 = parse_file(os.path.join(root, f'test/test_kernel_{args.one}.txt'))
    data2 = parse_file(os.path.join(root, f'test/test_kernel_{args.another}.txt'))
    plot(args.one, args.another, data1, data2, args.save_dir)

def parse_args():
    parser = argparse.ArgumentParser(description='plot kernel performance')
    parser.add_argument('one', type=int, help='one kernel num')
    parser.add_argument('another', type=int, help='another kernel num')
    parser.add_argument('--save_dir', default='images')
    return parser.parse_args()

if __name__ == "__main__":
    args = parse_args()
    main(args)

# python plot.py 0 1


import tweepy
from config import APIConfig

class BitcoinStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print(status)

    def on_error(self, status):
        print(status)

apiConfig = APIConfig()
auth = apiConfig.createAuth()

btcStreamListener = BitcoinStreamListener()
stream = tweepy.Stream(auth, btcStreamListener)
stream.filter(track=['bitcoin'])


import matplotlib
import numpy as np
import pandas as pd
import tensorflow as tf
from pandas import read_csv
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_absolute_error as MAE
from sklearn.metrics import mean_squared_error as MSE
from tensorflow.contrib import rnn
import time

from config import *
from lib.preprocess_data.preprocessing_data import DataPreprocessor
from lib.pre_autoencoder.pre_autoencoder import PreAutoEncoder

matplotlib.use(Config.PLT_ENV)
import matplotlib.pyplot as plt

class PreAutoEncoderPredictor:
    def __init__(self, data=None, scaler=None, train_size=None, valid_size=None, sliding_encoder=None, batch_size=None,
                 num_units_lstm=None, num_units_inference=None, dropout_rate=None, variation_dropout=False, activation=None, optimizer=None,
                 variant=None, optimizer_approach=None, learning_rate=None, epochs=None, patience=None, time_list=None):
        self.data = data
        self.scaler = scaler
        self.train_size = train_size
        self.valid_size = valid_size
        self.sliding_encoder = sliding_encoder
        # self.sliding_inference = sliding_inference
        self.batch_size = batch_size
        self.num_units_lstm = num_units_lstm
        self.num_units_inference = num_units_inference

        self.dropout_rate = dropout_rate
        self.variation_dropout = variation_dropout
        self.optimizer_approach = optimizer_approach
        self.learning_rate = learning_rate
        self.epochs = epochs
        self.patience = patience
        self.time_list = time_list

        self.variant = variant
        self.activation = activation
        if self.activation == 'sigmoid':
            self.activation_func = tf.nn.sigmoid
        elif self.activation == 'relu':
            self.activation_func = tf.nn.relu
        elif self.activation == 'tanh':
            self.activation_func = tf.nn.tanh
        elif self.activation == 'elu':
            self.activation_func = tf.nn.elu
        else:
            print(">>> Can not apply your activation <<<")

        self.optimizer = optimizer
        if self.optimizer == 'momentum':
            self.optimizer_method = tf.train.MomentumOptimizer(learning_rate=self.learning_rate, momentum=0.9)
        elif self.optimizer == 'adam':
            self.optimizer_method = tf.train.AdamOptimizer(learning_rate=self.learning_rate)
        elif self.optimizer == 'rmsprop':
            self.optimizer_method = tf.train.RMSPropOptimizer(learning_rate=self.learning_rate)
        else:
            print(">>> Can not apply your optimizer <<<")
        self.create_name()
        if Config.DATA_EXPERIMENT == 'google_trace':
            self.x_data_name = Config.GOOGLE_TRACE_DATA_CONFIG['train_data_type']
            self.y_data_name = Config.GOOGLE_TRACE_DATA_CONFIG['predict_data']

        self.evaluation_path = Config.EVALUATION_PATH.format(
            Config.MODEL_EXPERIMENT, self.optimizer_approach, self.x_data_name, self.y_data_name)

        self.model_saved_path = Config.MODEL_SAVE_PATH.format(
            Config.MODEL_EXPERIMENT, self.optimizer_approach, self.x_data_name, self.y_data_name)
        if not os.path.exists(self.model_saved_path):
            os.mkdir(self.model_saved_path)

        self.model_saved_path += self.file_name
        if not os.path.exists(self.model_saved_path):
            os.mkdir(self.model_saved_path)

        # self.model_saved_path += '/model'

        self.train_loss_path = Config.TRAIN_LOSS_PATH.format(
            Config.MODEL_EXPERIMENT, self.optimizer_approach, self.x_data_name, self.y_data_name) + self.file_name

        self.results_save_path = Config.RESULTS_SAVE_PATH.format(
            Config.MODEL_EXPERIMENT, self.optimizer_approach, self.x_data_name, self.y_data_name) + self.file_name + '.csv'

    def create_name(self):
        def create_name_network(num_units):
            name = ''
            for i in range(len(num_units)):
                if (i == len(num_units) - 1):
                    name += str(num_units[i])
                else:
                    name += str(num_units[i]) + '_'
            return name

        name_lstm = create_name_network(self.num_units_lstm)

        part1 = 'sli_encoder-{}_batch-{}_name_lstm-{}'.format(self.sliding_encoder, self.batch_size, name_lstm)
        part2 = 'activation-{}_optimizer_{}'.format(self.activation, self.optimizer)
        if self.variation_dropout:
            part3 = '_dropout_rate-{}'.format(self.dropout_rate)
        else:
            part3 = ''

        part4 = f'_time_{self.time_list}'

        self.file_name = part1 + part2 + part3 + part4
        self.encoder_file_name = part1 + part2 + part3 + part4

    def preprocessing_data(self):
        data_preprocessor = DataPreprocessor(self.data, self.train_size, self.valid_size)
        self.x_train_encoder, self.x_train_decoder, self.y_train_decoder, self.x_valid_encoder, self.x_valid_decoder,\
            self.y_valid_decoder, self.x_test_encoder, self.x_test_decoder, self.y_test_decoder\
            = data_preprocessor.init_data_autoencoder(self.sliding_encoder)
        self.y_train, self.y_valid, self.y_test = data_preprocessor.init_data_inference(self.sliding_encoder)

    def draw_train_loss(self, cost_train_set, cost_valid_set, model_name):
        plt.plot(cost_train_set)
        plt.plot(cost_valid_set)

        plt.title('model loss')
        plt.ylabel('loss')
        plt.xlabel('epoch')
        plt.legend(['train', 'validation'], loc='upper left')
        plt.savefig(self.train_loss_path + model_name + '.png')
        plt.close()

    def mlp(self, input, num_units, activation):
        num_layers = len(num_units)
        prev_layer = input
        for i in range(num_layers):
            prev_layer = tf.layers.dense(prev_layer, num_units[i], activation=activation, name='layer' + str(i))
            # drop_rate = self.dropout_rate
            # prev_layer = tf.layers.dropout(prev_layer, rate=drop_rate)

        prediction = tf.layers.dense(inputs=prev_layer, units=1, activation=activation)
        return prediction

    def early_stopping(self, array, patience):
        value = array[len(array) - patience - 1]
        arr = array[len(array) - patience:]
        check = 0
        for val in arr:
            if val >= value:
                check += 1
        if check >= patience - 1:
            return True
        else:
            return False

    def init_RNN(self, num_units):
        num_layers = len(num_units)
        hidden_layers = []
        for i in range(num_layers):
            if i == 0:
                cell = tf.nn.rnn_cell.LSTMCell(num_units[i], activation=self.activation_func)
                if self.variation_dropout:
                    cell = tf.nn.rnn_cell.DropoutWrapper(cell, input_keep_prob=1.0, output_keep_prob=self.dropout_rate,
                                                         state_keep_prob=self.dropout_rate, variational_recurrent=True,
                                                         input_size=self.x_train.shape[2], dtype=tf.float32)
                hidden_layers.append(cell)
            else:
                cell = tf.nn.rnn_cell.LSTMCell(num_units[i], activation=self.activation_func)
                if self.variation_dropout:
                    cell = tf.nn.rnn_cell.DropoutWrapper(cell, input_keep_prob=1.0, output_keep_prob=self.dropout_rate,
                                                         state_keep_prob=self.dropout_rate, variational_recurrent=True,
                                                         input_size=num_units[i - 1], dtype=tf.float32)
                hidden_layers.append(cell)
        return tf.nn.rnn_cell.MultiRNNCell(hidden_layers, state_is_tuple=True)

    def __fit_with_bp(self):
        self.n_input_encoder = self.x_train_encoder.shape[1]
        self.n_input_decoder = self.x_train_decoder.shape[1]
        self.n_output_inference = self.y_train.shape[1]

        tf.reset_default_graph()
        # x1 = tf.placeholder("float", [None, self.sliding_encoder * len(self.original_data) / self.input_dim, self.input_dim])
        # x2 = tf.placeholder("float",shape = (None, self.sliding_decoder*len(self.original_data)/self.input_dim, self.input_dim))
        # define placeholder of graph
        x_encoder = tf.placeholder("float", [None, self.x_train_encoder.shape[1], self.x_train_encoder.shape[2]],
                                   name='x_encoder')
        x_decoder = tf.placeholder("float", [None, self.x_train_decoder.shape[1], self.x_train_decoder.shape[2]],
                                   name='x_decoder')
        y_decoder = tf.placeholder("float", [None, self.y_train_decoder.shape[1], self.y_train_decoder.shape[2]],
                                   name='y_decoder')

        with tf.variable_scope('encoder'):
            encoder_cell = self.init_RNN(self.num_units_lstm)
            outputs_encoder, state_encoder = tf.nn.dynamic_rnn(encoder_cell, x_encoder, dtype="float32")
            outputs_encoder = tf.identity(outputs_encoder, name='outputs_encoder')
            if self.variant:
                mean = tf.layers.dense(state_encoder[-1].h, self.num_units_lstm[-1], name='z_mean')
                log_var = tf.layers.dense(state_encoder[-1].h, self.num_units_lstm[-1], name='z_log_var')
                batch = tf.shape(mean)[0]
                dim = tf.keras.backend.int_shape(mean)[1]
                # by default, random_normal has mean=0 and std=1.0
                epsilon = tf.random_normal(shape=(batch, dim))
                hidden_state_encoder = mean + tf.exp(0.5 * log_var) * epsilon
            else:
                hidden_state_encoder = state_encoder[-1].h
            hidden_state_encoder = tf.identity(hidden_state_encoder, name='hidden_state_encoder')
        with tf.variable_scope('decoder'):
            decoder_cell = self.init_RNN(self.num_units_lstm)
            outputs_decoder, state_decoder = tf.nn.dynamic_rnn(decoder_cell, x_decoder, dtype="float32",
                                                               initial_state=state_encoder)
            outputs_decoder = tf.identity(outputs_decoder, name='outputs_decoder')
            prediction_autoencoder = outputs_decoder[:, :, -1]
            prediction_autoencoder = tf.reshape(
                prediction_autoencoder, (tf.shape(outputs_decoder)[0], 1, tf.shape(outputs_decoder)[1]))
        prediction_autoencoder = tf.identity(prediction_autoencoder, name='prediction_autoencoder')
        # loss_function
        if self.variant:
            # Kullback-Leibler divergence with 2 gaussian distribution when reparameterize is:
            # kl = log(sigma2/sigma1) + (sigma1^2 + (mu1 -mu2)^2)/2*sigma2^2 - 1/2
            # => just for one dimension, we need to scale it up
            reconstruction_loss = tf.reduce_mean(tf.square(y_decoder - prediction_autoencoder))
            kl = 0.5 * tf.reduce_mean(tf.square(mean) + tf.exp(2.0 * log_var) - 2.0 * log_var - 1.0)
            loss_autoencoder = reconstruction_loss + kl
        else:
            loss_autoencoder = tf.reduce_mean(tf.square(y_decoder - prediction_autoencoder))
        loss_autoencoder = tf.identity(loss_autoencoder, name='loss')
        # optimization
        optimizer_autoencoder = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(loss_autoencoder)

        y_inf = tf.placeholder("float", [None, self.y_train.shape[1]])

        prediction_inf = self.mlp(hidden_state_encoder, self.num_units_inference, self.activation_func)
        loss_inf = tf.reduce_mean(tf.square(y_inf - prediction_inf))
        # optimization
        optimizer_inf = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(loss_inf)

        cost_train_set = []
        cost_valid_set = []

        init = tf.global_variables_initializer()
        # try:
        with tf.Session() as sess:
            sess.run(init)
            saver = tf.train.Saver()
            print("start training autoencoder model")
            for epoch in range(self.epochs):
                # print(f'epoch {epoch}:')
                # Train with each example
                start_time = time.time()
                total_batch = int(len(self.x_train_encoder) / self.batch_size)
                # sess.run(updates)
                avg_cost = 0
                for i in range(total_batch):
                    batch_xs_encoder = self.x_train_encoder[i * self.batch_size:(i + 1) * self.batch_size]
                    batch_xs_decoder = self.x_train_decoder[i * self.batch_size:(i + 1) * self.batch_size]
                    batch_ys = self.y_train_decoder[i * self.batch_size:(i + 1) * self.batch_size]
                    sess.run(optimizer_autoencoder,
                            feed_dict={x_encoder: batch_xs_encoder, x_decoder: batch_xs_decoder, y_decoder: batch_ys})

                    avg_cost += sess.run(loss_autoencoder,
                                        feed_dict={x_encoder: batch_xs_encoder, x_decoder: batch_xs_decoder,
                                                    y_decoder: batch_ys}) / total_batch
                # Display logs per epoch step
                training_history = 'Epoch autoencoder %04d: cost = %.9f with time: %.9f'\
                    % (epoch + 1, avg_cost, time.time() - start_time)
                print(training_history)
                cost_train_set.append(avg_cost)
                val_cost = sess.run(loss_autoencoder,
                                    feed_dict={x_encoder: self.x_valid_encoder, x_decoder: self.x_valid_decoder,
                                            y_decoder: self.y_valid_decoder})
                cost_valid_set.append(val_cost)
                if epoch > self.patience:
                    if self.early_stopping(cost_train_set, self.patience):
                        print('early stop training auto encoder model')
                        break

            if not os.path.exists(self.model_saved_path + '/autoencoder/model/'):
                os.mkdir(self.model_saved_path + '/autoencoder')
                os.mkdir(self.model_saved_path + '/autoencoder/model/')
            saver.save(sess, self.model_saved_path + '/autoencoder/model/model')
            self.draw_train_loss(cost_train_set, cost_valid_set, '_autoencoder')

            # training bnn model
            print("start training bnn model")
            cost_train_set = []
            cost_valid_set = []
            for epoch in range(self.epochs):
                # Train with each example
                total_batch = int(len(self.x_train_encoder) / self.batch_size)

                start_time = time.time()
                avg_cost = 0
                for i in range(total_batch):
                    batch_xs_encoder = self.x_train_encoder[i * self.batch_size:(i + 1) * self.batch_size]
                    batch_ys_inf = self.y_train[i * self.batch_size:(i + 1) * self.batch_size]
                    sess.run(optimizer_inf,
                            feed_dict={x_encoder: batch_xs_encoder, y_inf: batch_ys_inf})

                    avg_cost += sess.run(loss_inf,
                                        feed_dict={x_encoder: batch_xs_encoder, y_inf: batch_ys_inf})
                # Display logs per epoch step
                training_history = 'Epoch inference %04d: cost = %.9f with time: %.2f'\
                    % (epoch + 1, avg_cost, time.time() - start_time)
                print(training_history)
                val_cost = sess.run(loss_inf, feed_dict={x_encoder: self.x_valid_encoder, y_inf: self.y_valid})
                cost_train_set.append(avg_cost)
                cost_valid_set.append(val_cost)
                if epoch > self.patience:
                    if self.early_stopping(cost_train_set, self.patience):
                        print("early stopping pretrain_predictor training process")
                        break

            if not os.path.exists(self.model_saved_path + '/predictor/model/'):
                os.mkdir(self.model_saved_path + '/predictor')
                os.mkdir(self.model_saved_path + '/predictor/model/')
            saver.save(sess, self.model_saved_path + '/predictor/model/model')
            self.draw_train_loss(cost_train_set, cost_valid_set, '_predictor')
            print('training BNN with back propagation complete!!!')
            prediction_inf = sess.run(prediction_inf, feed_dict={x_encoder: self.x_test_encoder})
            sess.close()
            prediction_inverse = self.scaler.inverse_transform(prediction_inf)
            prediction_inverse = np.asarray(prediction_inverse)
            self.y_test_inversed = self.scaler.inverse_transform(self.y_test)

            mae_err = MAE(prediction_inverse, self.y_test_inversed)
            rmse_err = np.sqrt(MSE(prediction_inverse, self.y_test_inversed))

            prediction_df = pd.DataFrame(np.array(prediction_inverse))
            prediction_df.to_csv(self.results_save_path, index=False, header=None)

            with open(self.evaluation_path, 'a+') as f:
                f.write(self.file_name + ',' + str(mae_err) + ',' + str(rmse_err) + '\n')

            print("=== error = {}, {} ===".format(mae_err, rmse_err))

            return rmse_err
        # except:
        #     return 1

    def fit(self):
        # training_time_evaluation_file = CORE_DATA_DIR + '/bnn/training_time_evaluation.csv'
        # start_all_time = time.time()

        print(">>> Start training bnn model <<<")
        self.preprocessing_data()
        if self.optimizer_approach.lower() == 'bp':
            print(">>> Training bnn with back propagation <<<")
            self.__fit_with_bp()
        else:
            ">>> error: We don't support this optimzer approach! <<<"
        # with open(training_time_evaluation_file, 'a+') as f:
        #     f.write(self.model_saved_path + ',' + str(time.time() - start_all_time) + '\n')


import sqlite3

def create_connection(db_file):
    """Create a connection to SQLite database"""
    conn = None
    try:
        conn = sqlite3.connect(db_file)
        print(f'Successfully connected to database: {sqlite3.version}')
    except sqlite3.Error as e:
        print(e)
    return conn

def create_patient(conn, patient):
    """Insert new patient into the patients table"""
    sql = '''INSERT INTO patients(name, dob, gender, address, phone, email)
             VALUES(?,?,?,?,?,?)'''
    cur = conn.cursor()
    cur.execute(sql, patient)
    conn.commit()
    return cur.lastrowid

def main():
    database = 'hospital.db'
    conn = create_connection(database)

    with conn:
        # example patient data
        patient = ('John Smith', '1990-01-01', 'Male', '123 Main St', '555-555-5555', 'john.smith@example.com')
        patient_id = create_patient(conn, patient)
        print(f'Patient record created with ID: {patient_id}')

if __name__ == '__main__':
    main()


from typing import List

from rxbp.indexed.observables.concatindexedobservable import ConcatIndexedObservable
from rxbp.indexed.selectors.bases.concatbase import ConcatBase
from rxbp.indexed.selectors.flowablebaseandselectors import FlowableBaseAndSelectors
from rxbp.mixins.flowablemixin import FlowableMixin
from rxbp.subscriber import Subscriber
from rxbp.subscription import Subscription

class ConcatIndexedFlowable(FlowableMixin):
    def __init__(self, sources: List[FlowableMixin]):
        super().__init__()

        self._sources = sources

    def unsafe_subscribe(self, subscriber: Subscriber) -> Subscription:
        def gen_subscriptions():
            for source in self._sources:
                subscription = source.unsafe_subscribe(subscriber)
                yield subscription

        subscriptions = list(gen_subscriptions())

        observable = ConcatIndexedObservable(
            sources=[s.observable for s in subscriptions],
            scheduler=subscriber.scheduler,
            subscribe_scheduler=subscriber.subscribe_scheduler,
        )

        base = ConcatBase(
            underlying=tuple(s.index for s in subscriptions),
            sources=tuple(observable.selectors),
        )

        return subscriptions[0].copy(
            index=FlowableBaseAndSelectors(base=base),
            observable=observable,
        )


#! /usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
x = np.arange(0, 5, 0.1);
y = np.sin(x)
z = np.cos(x)
ax1.plot(x, y, x, z)
plt.show()


#!/usr/local/bin/python
# coding: utf-8

import cv2
from PIL import Image
from matplotlib import pyplot as plt
from collections import Counter
import numpy

class TextImage(object):
	"""Represents an image containing text characters."""
	image = None
	empty_rows = []
	empty_cols = []
	height = None
	width = None

	def __init__(self, filepath=None, array=None):
		"""Creates a new TextImage from a file or numpy array.

		If filepath is specified, reads in the image from filepath.
		If array is specified, makes a TextImage out of the array.
		Otherwise, creates a new, empty array.

		Args:
			filepath: String, the relative or absolute path of the image file
				to read in. If not provided, array will be checked instead.
			array: numpy array representing a portion of another
				TextImage object.
				This argument is only checked if filepath is None.

		Raises:
			ValueError: A filepath is provided, but it does not point
				to a valid image.
		"""
		# Check filepath first, and read in an image as black and white
		if filepath:
			# imread returns None for an invalid file
			parsed_image = cv2.imread(filepath, 0)
			# May return an array of 0s, so cannot use 'if parsed_image'
			if parsed_image is None:
				raise ValueError("Invalid filepath: {}".format(filepath))
			self.image = parsed_image
		elif array is not None and len(array) > 0:
			self.image = array
		else:
			# Make a blank, empty image
			self.image = numpy.full((1, 1), 255, numpy.uint8)
		assert self.image is not None
		# Empty rows/columns are not yet identified
		self.empty_rows = []
		self.empty_cols = []
		# shape is in the format (height, width, color_channels)
		self.height = self.image.shape[0]
		self.width = self.image.shape[1]
		self.threshold()
		self._set_ranges()

	def threshold(self):
		"""Converts this TextImage to black on white using Otsu's method."""
		# CV2 settings for Otsu's binary thresholding
		thresh_settings = cv2.THRESH_BINARY+cv2.THRESH_OTSU
		# threshold returns the Otsu threshold value and threshed image array
		t_value, t_img = cv2.threshold(self.image, 0, 255, thresh_settings)
		# Check four corners of image; the most common color there is likely
		# to be the background color
		corners = (t_img[0][0], t_img[-1][-1], t_img[0][-1], t_img[-1][0])
		# Get the most common corner pixel (any if tied)
		if Counter(corners).most_common(1)[0][0] <= t_value:
			# If background is darker than the threshold value, invert black/white
			# In cases where the Otsu technique was not needed, t_value will be 0.
			t_img = cv2.bitwise_not(t_img)
		self.image = t_img

	def _set_ranges(self):
		"""Identifies ranges (splits) of blank (255) pixels."""
		pixels = numpy.asarray(self.image)
		x, y = self.width, self.height
		empty_rows = []
		for row in xrange(y):
			for col in xrange(x):
				if pixels[row][col] != 255:
					# Found non-background pixel; row is not blank
					break
			else:
				empty_rows.append(row)
		empty_cols = []
		for col in xrange(x):
			for row in xrange(y):
				if pixels[row][col] != 255:
					# Found non-background pixel; column is not blank
					break
			else:
				empty_cols.append(col)
		if len(empty_rows) == 1 and len(empty_cols) == 1:
			# Single empty rows and columns; most likely with a blank image
			# Match formatting of get_split_ranges
			self.empty_rows = [(empty_rows[0], empty_rows[0])]
			self.empty_cols = [(empty_cols[0], empty_cols[0])]
			return
		# Otherwise, the above just notes the individual empty rows and columns,
		# so convert those numbers into ranges before assigning
		self.empty_rows = TextImage.get_split_ranges(empty_rows)
		self.empty_cols = TextImage.get_split_ranges(empty_cols)

	# Argument is "full_image" for disambiguation from TextImage.image
	@staticmethod
	def crop_border(full_image, crop_vertical=True):
		"""Removes blank borders from the image.

		Args:
			full_image: TextImage, the image to crop.

			crop_vertical: boolean, whether or not to crop on the y-axis.
				If set to False, this method can be used to trim empty space
				from the left and right borders, while leaving vertical spacing
				intact.
				Defaults to True.

	 Raises:
			ValueError: empty_rows and empty_cols are both empty for this image.
				This results from incorrect initialization of the TextImage.
		"""
		# If no blank borders are found, keep the full image
		x1, y1 = 0, 0
		x2 = full_image.width
		y2 = full_image.height
		# Check for top, bottom, left, and right borders
		if crop_vertical:
			for start, stop in full_image.empty_rows:
				if start == 0:
					# Leave an extra pixel on each side for visibility
					y1 = max(0, stop - 1)
				elif stop == full_image.height - 1:
					y2 = start + 1
		for start, stop in full_image.empty_cols:
			if start == 0:
				x1 = max(0, stop - 1)
			elif stop == full_image.width - 1:
				x2 = start + 1
		# Crop by slicing the numpy array
		full_image.image = full_image.image[y1:y2, x1:x2]
		# Adjust blank rows/columns by top and left borders and update sizes
		if crop_vertical:
			rows = [(max(s, y1), e) for s, e in full_image.empty_rows]
			full_image.empty_rows = rows
			full_image.height = y2 - y1
		# Filter out any range ends that are beyond the new size
		c = [(s - x1, e - x1) for s, e in full_image.empty_cols if e <= x2 - x1]
		full_image.empty_cols = c
		full_image.width = x2 - x1

	@staticmethod
	def split_row(row):
		"""Given a TextImage row, split it into character TextImages.

		Args:
			row: TextImage, a single row of text to split into single
				characters.
				Use get_text_rows to split an image into these rows.

		Returns:
			A list of TextImage objects of each character in row in the
				same order.

		Raises:
			AttributeError: row is not a valid TextImage.

			IndexError: No spaces between characters were found.
		"""
		chrs = []
		widths = sorted(e - s for s, e in row.empty_cols if s != e)
		# Median horizontal spacing in this row
		space_size = widths[len(widths)/2] - 1
		# Get columns that are large enough to be full spaces
		spaces = [(x, y) for x, y in row.empty_cols if y >= (x + space_size)]
		space_index = 0
		previous_x = 0 # Where the last character left off
		last_char = False # Is this the last character in the row?
		while not last_char:
			c_start = previous_x
			if space_index >= len(spaces) or c_start >= row.width:
				# If this is the last character, it should reach the end of row
				c_end = row.width
				last_char = True
			else:
				# Pad with extra pixels for clarity
				c_end = spaces[space_index][0] + 2
			if c_end - c_start <= row.height + 2:
				# Small or normal sized character
				chrs.append(row.image[0:row.height, c_start:c_end])
				if not last_char:
					previous_x = spaces[space_index][1] - 1
			else:
				# Too big to be a single character; split into pieces
				previous_x, new_chars = TextImage._split_character(row, c_start, c_end)
				chrs.extend(new_chars)
			space_index += 1
		# Make TextImages out of the sliced array portions
		return [TextImage(array=c) for c in chrs]

	def _split_character(self, c_start, c_end):
		"""Split a single large character into smaller images, if possible."""
		new_chars = []
		while c_end - c_start > self.height + 2:
			gap = TextImage._find_split(c_start, c_end, self.empty_cols)
			if not gap:
				# Couldn't find any splits in this section; add it as is
				new_chars.append(self.image[0:self.height, c_start:c_end])
				break
			c_end = gap
			if c_end - c_start <= self.height + 2:
				new_chars.append(self.image[0:self.height, c_start:c_end])
			c_start = gap # Next character starts where this left off
		return (c_end, new_chars)

	@staticmethod
	def _find_split(chr_start, chr_end, cols):
		"""Returns the location of the largest gap within a character."""
		# Given start and end indices and a series of blank columns, finds
		#    the largest split between those indices and returns the middle
		#    of that split.
		midpoint = 0
		max_gap = 0
		for gap_start, gap_end in cols:
			# Gap occurs after this character; stop looking
			if gap_start > chr_end:
				return midpoint
			if chr_start < gap_start and gap_end < chr_end:
				# Gap divides this character
				# Choose the first such gap if there is a tie
				if gap_end - gap_start > max_gap:
					max_gap = gap_end - gap_start
					midpoint = (gap_start + gap_end) / 2
		# Returns 0 when no splits are found
		return midpoint

	@staticmethod
	def get_text_rows(img):
		"""Splits a TextImage into a list of TextImages based on vertical spaces."""
		text_lines = []
		previous = 0
		for row in img.empty_rows:
			# row is a tuple of (blank_row_start, blank_row_end)
			row_img = TextImage(array=img.image[previous:row[0] + 1, 0:img.width])
			# Remove extra horizontal (x axis) space from each row
			# Don't crop vertically, leave some vertical padding intact
			TextImage.crop_border(row_img, crop_vertical=False)
			text_lines.append(row_img)
			# Start from the next non-blank row
			previous = row[1]
		return text_lines

	@staticmethod
	def get_split_ranges(array):
		"""Given a list of integers, condense the numbers into ranges.

		Args:
			array: list of distinct integers. In order to find all consecutive
				number ranges, array must be in sorted order.

				If array contains duplicate values, the ranges will start and end
				on those values rather than including them and continuing.

		Returns:
			A list of two-tuples containing integers indicating the start and end
				(inclusive) of each consecutive range. If an integer is not part of
				a consecutive sequence, it will be added as both a start and end
				value.

			For example, if array is [1, 2, 3, 6, 7, 8, 10], the return value
				would be [(1, 3), (6, 8), (10, 10)].
			"""
		if not array:
			return []
		ranges = []
		start = array[0]
		end = array[0]
		for pos in array[1:]:
			if (pos != end + 1):
				ranges.append((start, end))
				# New range encountered, so reset start
				start = pos
			# End always moves regardless of whether a match was found
			end = pos
		if start != end:
			# The last range contained at least one blank
			ranges.append((start, end))
		return ranges


import jax
import sys

from experiments.parse_args import parse_args
from experiments.logging import init_logger, log_results
from util import *
from agents.agents import get_agent
from environments.rollout import RolloutWrapper

def make_train(args):
    def train(rng):
        # --- Initialize environment ---
        env = RolloutWrapper(args.env_name, args.num_rollout_steps)
        env_params = env.default_env_params
        rng, _rng = jax.random.split(rng)
        _rng = jax.random.split(_rng, args.num_env_workers)
        obsv, env_state = env.batch_reset(_rng, env_params)

        # --- Initialize agent train states and step function ---
        rng, _rng = jax.random.split(rng)
        # train_state contains actor (and critic if used) to be used for rollouts,
        # aux_train_states contains all other trainable parameters
        train_state, aux_train_states, agent_train_step_fn = get_agent(
            args,
            _rng,
            env.obs_shape,
            env.num_actions,
            env.discrete_actions,
            env.action_lims,
        )

        # --- Execute train loop ---
        def _train_step(runner_state, _):
            train_state, aux_train_states, env_state, last_obs, rng = runner_state
            # --- Collect trajectories ---
            rng, _rng = jax.random.split(rng)
            _rng = jax.random.split(_rng, args.num_env_workers)
            new_env_state, new_last_obs, traj_batch = env.batch_rollout(
                _rng, train_state, env_params, last_obs, env_state
            )

            # --- Update agent ---
            rng, _rng = jax.random.split(rng)
            train_state, aux_train_states, loss, metric = agent_train_step_fn(
                train_state, aux_train_states, traj_batch, new_last_obs, _rng
            )

            runner_state = (
                train_state,
                aux_train_states,
                new_env_state,
                new_last_obs,
                rng,
            )
            return runner_state, (loss, metric, traj_batch)

        rng, _rng = jax.random.split(rng)
        runner_state = (train_state, aux_train_states, env_state, obsv, _rng)
        runner_state, (loss, metric, traj_batch) = jax.lax.scan(
            _train_step, runner_state, None, args.num_train_steps
        )
        ret = {
            "runner_state": runner_state,
            "metrics": metric,
            "loss": loss,
        }
        return ret

    return train

def train_agents(args):
    # --- Initialize experiment ---
    init_logger(args)
    train_fn = make_train(args)

    # --- Run training for num_agents ---
    rng = jax.random.PRNGKey(args.seed)
    rng = jax.random.split(rng, args.num_agents)
    results = jax.jit(jax.vmap(train_fn))(rng)
    log_results(args, results)

def main(cmd_args=sys.argv[1:]):
    args = parse_args(cmd_args)
    experiment_fn = jax_debug_wrapper(args, train_agents)
    return experiment_fn(args)

if __name__ == "__main__":
    main()


from typing import List, Optional

import numpy as np

import pyrado
from pyrado.environments.base import Env
from pyrado.logger.step import LoggerAware
from pyrado.policies.base import Policy
from pyrado.sampling.sampler import SamplerBase
from pyrado.sampling.step_sequence import StepSequence

def select_cvar(rollouts, epsilon: float, gamma: float = 1.0):
    """
    Select a subset of rollouts so that their mean discounted return is the CVaR(eps) of the full rollout set.

    :param rollouts: list of rollouts
    :param epsilon: chosen return quantile
    :param gamma: discount factor to compute the discounted return, default is 1 (no discount)
    :return: list of selected rollouts
    """
    # Select epsilon-quantile of returns
    # Do this first since it is easier here, even though we have to compute the returns again
    # To do so, we first sort the paths by their returns
    rollouts.sort(key=lambda ro: ro.discounted_return(gamma))

    # Compute the quantile on a sorted list is easy
    num_sel_ros = round(len(rollouts) * epsilon)

    if num_sel_ros == 0:
        raise pyrado.ValueErr(given=num_sel_ros, g_constraint=0)

    # Only use the selected rollouts
    return rollouts[0:num_sel_ros]

class CVaRSampler(SamplerBase, LoggerAware):
    """
    Samples rollouts to optimize the CVaR of the discounted return.
    This is done by sampling more rollouts, and then only using the epsilon-qunatile of them.
    """

    def __init__(
        self, wrapped_sampler, epsilon: float, gamma: float = 1.0, *, min_rollouts: int = None, min_steps: int = None
    ):
        """
        Constructor

        :param wrapped_sampler: the inner sampler used to sample the full data set
        :param epsilon: quantile of rollouts that will be kept
        :param gamma: discount factor to compute the discounted return, default is 1 (no discount)
        :param min_rollouts: minimum number of complete rollouts to sample
        :param min_steps: minimum total number of steps to sample
        """
        self._wrapped_sampler = wrapped_sampler
        self.epsilon = epsilon
        self.gamma = gamma

        # Call SamplerBase constructor
        super().__init__(min_rollouts=min_rollouts, min_steps=min_steps)

    def set_min_count(self, min_rollouts=None, min_steps=None):
        # Set inner sampler's parameter values (back) to the user-specified number of rollouts / steps
        super().set_min_count(min_rollouts=min_rollouts, min_steps=min_steps)

        # Increase the number of rollouts / steps that will be sampled since we will discard (1 - eps) quantile
        # This modifies the inner samplers parameter values, that's ok since we don't use them afterwards
        if min_rollouts is not None:
            # Expand rollout count to full set
            min_rollouts = int(min_rollouts / self.epsilon)
        if min_steps is not None:
            # Simply increasing the number of steps as done for the rollouts is not identical, however it is goo enough
            min_steps = int(min_steps / self.epsilon)
        self._wrapped_sampler.set_min_count(min_rollouts=min_rollouts, min_steps=min_steps)

    def reinit(self, env: Optional[Env] = None, policy: Optional[Policy] = None):
        # Delegate to inner sampler
        self._wrapped_sampler.reinit(env=env, policy=policy)

    def sample(self) -> List[StepSequence]:
        # Sample full data set
        fullset = self._wrapped_sampler.sample()

        # Log return-based metrics using the full data set
        rets = np.asarray([ro.undiscounted_return() for ro in fullset])
        ret_avg = np.mean(rets)
        ret_med = np.median(rets)
        ret_std = np.std(rets)
        self.logger.add_value("full avg rollout len", np.mean([ro.length for ro in fullset]))
        self.logger.add_value("full avg return", ret_avg)
        self.logger.add_value("full median return", ret_med)
        self.logger.add_value("full std return", ret_std)

        # Return subset
        return select_cvar(fullset, self.epsilon, self.gamma)


import pandas as pd
import numpy as np
import os as os
import matplotlib.pyplot as plt
import sys as sys

##1. feladat

def read_etf_file(etf):
    filename = os.path.join(etf + '.csv')
    dataframe = pd.read_csv(filename, index_col=0)
    dataframe.index = pd.to_datetime(dataframe.index)
    return dataframe

def get_etf_returns(etf_name, return_type, fieldname='Adj Close'):

    df = read_etf_file(etf_name)
    df = df[[fieldname]]

    df['shifted'] = df.shift(1)
    if return_type=='log':
        df['return'] = np.log(df[fieldname]/df['shifted'])
    if return_type=='simple':
        df['return'] = df[fieldname]/df['shifted']-1

    df = df[['return']]
    df.columns = [etf_name]
    return df

def get_portfolio_return(d_weights):
    l_df = []
    for etf, value in d_weights.items():
        df_temp = get_etf_returns(etf, return_type='simple')
        l_df.append(df_temp)
    df_joined = pd.concat(l_df, axis=1)
    df_joined.sort_index(inplace=True)
    df_joined.dropna(inplace=True)
    df_weighted_returns = df_joined * pd.Series(d_weights)
    s_portfolio_return = df_weighted_returns.sum(axis=1)
    return pd.DataFrame(s_portfolio_return, columns=['pf'])

def generate_portfolio_returns():
    weights = np.arange(0, 1.0, 0.05)
    returns_data = []
    for weight in weights:
        d_weights = {'SPY': weight, 'GLD': 1 - weight}
        df_returns = get_portfolio_return(d_weights)
        returns_data.append(df_returns)

    df_portfolio_returns = pd.concat(returns_data, axis=1)
    df_portfolio_returns.columns = [f'Weight_{weight:.2f}' for weight in weights]
    return df_portfolio_returns

def plotting():
    df_portfolio_returns = generate_portfolio_returns()
    df_portfolio_returns = df_portfolio_returns[["Weight_0.05", "Weight_0.50", "Weight_0.95"]]
    plt.figure(figsize=(12, 6))
    for column in df_portfolio_returns.columns:
        plt.plot(df_portfolio_returns[column], label=column)

    plt.xlabel('Dátum')
    plt.ylabel('A Portfólio Hozamai')
    plt.title('A Portfólió hozamai különböző súlyok mellett')
    plt.legend()
    plt.grid(True)
    plt.tight_layout()
    plt.show()

def calculate_historical_var(df_portfolio_returns, alpha):
    l_quantiles = [1 - alpha]
    df_pf = df_portfolio_returns
    df_result = df_pf.quantile(l_quantiles)
    df_result.index = [alpha]
    return df_result.iloc[0]

df_portfolio_returns = generate_portfolio_returns()
alpha = 0.95
var_values = {}
for column in df_portfolio_returns.columns:
    var_value = calculate_historical_var(df_portfolio_returns[column], alpha)
    var_values[column] = var_value

def plot_var_values(var_values):
    weights = np.arange(0, 1.0, 0.05)
    var_list = [var_values[f'Weight_{weight:.2f}'] for weight in weights]

    plt.figure(figsize=(8, 6))
    plt.plot(weights, var_list, marker='o', linestyle='-', color='blue')

    plt.xlabel('SPY súlya')
    plt.ylabel('VaR')
    plt.title('VaR az SPY különböző súlyai mellett')
    plt.grid(True)
    plt.tight_layout()
    plt.show()

#2. feladat

def simulated_returns(expected_return, volatility, correlation, num_of_sim):
    cov_matrix = np.array([[volatility[0]**2, correlation * volatility[0] * volatility[1]],
                           [correlation * volatility[0] * volatility[1], volatility[1]**2]])

    np.random.seed(0)
    simulated_returns = np.random.multivariate_normal(expected_return, cov_matrix, num_of_sim)

    return simulated_returns

df_SPY = get_etf_returns("SPY", "simple")
df_GLD = get_etf_returns("GLD", "simple")
return_SPY = df_SPY["SPY"]
return_GLD = df_GLD["GLD"]
expected_return_SPY = return_SPY.mean()
expected_return_GLD = return_GLD.mean()
vola_SPY = return_SPY.std()
vola_GLD = return_GLD.std()
weight_1 = 0.45
weight_2 = 0.55
total_volatility = vola_SPY + vola_GLD
weight_1 = weight_1 * (vola_GLD / total_volatility)
weight_2 = weight_2 * (vola_SPY / total_volatility)

def plot_simulated_returns(simulated_returns):
    num_of_simulations, num_of_assets = simulated_returns.shape

    fig, ax = plt.subplots(figsize=(10, 6))
    for i in range(num_of_assets):
        ax.plot(simulated_returns[:, i], label=f"Eszköz {i+1} hozama")

    ax.set_ylabel("Hozamok")
    ax.legend()

    plt.tight_layout()
    plt.show()

correlation_values = np.arange(-1.0, 1.0, 0.2)
var_values2 = []
def calculate_simulated_return_var(asset_returns, weights, alpha):
    portfolio_returns = np.dot(asset_returns, weights)
    portfolio_var = np.percentile(portfolio_returns, 1-alpha)  # Calculate VaR at alpha confidence level

    return portfolio_var
num_of_simulations = 500
for correlation in correlation_values:
    simulated_returns_array = simulated_returns([expected_return_SPY, expected_return_GLD],
                                                 [vola_SPY, vola_GLD],
                                                 correlation,
                                                 num_of_simulations)

    portfolio_var = calculate_simulated_return_var(simulated_returns_array, [weight_1, weight_2], 0.95)
    var_values2.append(portfolio_var)

def plotting_correlation():
    plt.plot(correlation_values, var_values2, marker='s', linestyle='-', linewidth=2)
    plt.xlabel('Korreláció')
    plt.ylabel('VaR')
    plt.title('VaR érték különböző korreláció mellett')
    plt.grid(True)
    plt.show()


from django.shortcuts import render
from .models import Course, UserCourses, CourseLesson
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.contrib.auth.decorators import login_required

# Create your views here.
def index(req):
    allcourses = Course.objects.all()
    usercourses = []
    if req.user.is_authenticated:
        usercourses = [i.course for i in UserCourses.objects.filter(user=req.user)]

    return render(req, 'courses.html',{
        'allcourses':allcourses,
        'usercourses':usercourses,
    })

@login_required
def usercourses(req):
    user_courses = UserCourses.objects.filter(user=req.user)
    if len(user_courses) == 0:
        print('No courses')
        user_courses=None
    return render(req, 'user_courses.html',{
        'user_courses':user_courses,
    })

@login_required
def addcourse(req):
    if req.method == 'GET':
        user = req.user
        course_to_add_name = req.GET.get('coursename')
        print(user_has_course(user,course_to_add_name))
        if user_has_course(user,course_to_add_name):
            return HttpResponse("Course not added"+' '+ course_to_add_name)
        elif user_has_course(user,course_to_add_name) == None:
            return HttpResponse('No such course named' + ' ' + course_to_add_name)
        else:
            course = Course.objects.get(course_name=course_to_add_name)
            usercourse = UserCourses(user=user, course=course)
            usercourse.save()
            return HttpResponseRedirect(reverse('user_courses'))

@login_required
def remove_course(req):
    if req.method == 'GET':
        user = req.user
        course_to_remove_name = req.GET.get('coursename')
        print(user_has_course(user,course_to_remove_name))
        if user_has_course(user,course_to_remove_name):
            course = Course.objects.get(course_name=course_to_remove_name)
            usercourse = UserCourses.objects.get(course=course, user=user)
            usercourse.delete()
            return HttpResponseRedirect(reverse('user_courses'))

        elif user_has_course(user,course_to_remove_name) == None:
            return HttpResponse('No such course named' + ' ' + course_to_remove_name)
        else:
            return HttpResponse("Course not removed"+' '+ course_to_remove_name)

def user_has_course(user,course_name):
    all_user_courses = [i.course for i in UserCourses.objects.filter(user=user)]
    try:
        course = Course.objects.get(course_name=course_name)
    except Course.DoesNotExist:
        course = None
    if course==None:
        return course
    elif course in all_user_courses:
        return True
    else:
        return False

def python_beginners_course(req):
    course = Course.objects.get(course_url='python-beginners-course')
    course_lessons = CourseLesson.objects.filter(course=course)
    print(course_lessons)
    return render(req, course.course_url+'/coursepage.html', {
        'course_lessons':course_lessons,
    })

def web_development_easy(req):
    return HttpResponse('Under development')

def lesson_urls(lesson):
    course = lesson.course
    def lesson_view(req):
        print(str(lesson.lesson_url))
        return render(req, lesson.course.course_url+'/'+lesson.lesson_url+'.html',{
            'lesson':lesson,
        })
    return lesson_view


from pyspark import SparkConf, SparkContext
import sys
import json                       #Added
assert sys.version_info >= (3, 5) # make sure we have Python 3.5+
from operator import is_not
from functools import partial
# add more functions as necessary

def add_pairs(x,y):
    (a,b) = x
    (c,d) = y
    return (a+c,b+d)

def key_value(text):
    key = text.get("subreddit")  #subreddit as key
    val1 = text.get("score")     #count, score_sum pair as value
    count=1
    return(key,(val1,count))

def avg_score(xyz):
    x,(y,z) = xyz
    a_score = float(y/z)
    return (x,a_score)

def split(text):
    subreddit = text.get("subreddit")  #subreddit as key
    score = text.get("score")     #count, score_sum pair as value
    author=text.get("author")
    transform = {"author": author, "subreddit": subreddit,"score":score}
    return transform
def search(text):
    s=text.get("subreddit")
    score = text.get("score")     #count, score_sum pair as value
    author=text.get("author")
    if 'e' in s:
            transform = {"author": author, "subreddit": s,"score":score}
            return transform

def main(inputs, output):
    text = sc.textFile(inputs)
    outdata = text.map(json.loads)
    #outdata.saveAsTextFile(output)
    new=outdata.map(search)
    new = new.filter(partial(is_not, None))
    #new.saveAsTextFile(output)
    new_out=new.map(key_value)
    #new_out.saveAsTextFile(output)
    f_out=new_out.reduceByKey(add_pairs).cache()
    f2_out=f_out.map(avg_score)
    #f2_out.saveAsTextFile(output+'/3')

    f3_out=f2_out.filter(lambda n:n[1]>0)
    f3_out.map(json.dumps).saveAsTextFile(output + '/positive')
    f4_out=f2_out.filter(lambda n:n[1]<=0)
    f4_out.map(json.dumps).saveAsTextFile(output + '/negative')

    #f3_out.saveAsTextFile(output)
if __name__ == '__main__':
    conf = SparkConf().setAppName('reddit average code')
    sc = SparkContext(conf=conf)
    assert sc.version >= '2.3'  # make sure we have Spark 2.3+
    inputs = sys.argv[1]
    output = sys.argv[2]
    main(inputs, output)

#-------------------------------------------------------------------------------------------------


from collections import defaultdict

def main():
    N = int(input())
    SS = [input() for _ in range(N)]
    cnts = defaultdict(int)
    for s in SS:
        u = cnts[s]
        if u == 0:
            print(s)
        else:
            print(s + f"({u})")
        cnts[s] += 1

if __name__ == '__main__':
    main()


import os
import shutil

import numpy as np
import pandas as pd

import skimage.io
from skimage import transform as tf
import cv2

DIR = './'
TRAIN_DIR = 'train/'
TEST_DIR = 'validation/'

train_CSV = pd.read_csv(DIR + 'train_multibox.csv')
test_CSV = pd.read_csv(DIR + 'test_multibox.csv')

def createSet(data, directory, filename, width, height):
	x = np.empty((len(data), width, height, 3))

	for index, row in data.iterrows():
		path = row['path']

		directories = os.path.dirname(path)
		src = directory + path
		pic = cv2.imread(src)
		pic = cv2.resize(pic, (height, width))
		assert pic.shape == (299, 299, 3)
		x[index] = pic

	x = np.uint8(x)
	np.save(filename, x)

createSet(train_CSV, TRAIN_DIR, 'train.npy', 299, 299)
createSet(test_CSV, TEST_DIR, 'test.npy', 299, 299)


"""Linear SDE models as transitions."""

from typing import Callable, Optional

import numpy as np
import scipy.integrate
import scipy.linalg

from probnum import randvars
from probnum.randprocs.markov.continuous import _sde
from probnum.typing import FloatLike, IntLike
from probnum.utils.linalg import tril_to_positive_tril

class LinearSDE(_sde.SDE):
    """Linear stochastic differential equation (SDE),

    .. math:: d x(t) = [G(t) x(t) + v(t)] d t + L(t) x(t) d w(t).

    For Gaussian initial conditions, this solution is a Gaussian process.

    Parameters
    ----------
    drift_matrix_function :
        This is G = G(t). The evaluations of this function are called
        the drift matrix of the SDE.
        Returns np.ndarray with shape=(n, n)
    force_vector_function :
        This is v = v(t). Evaluations of this function are called
        the force(vector) of the SDE.
        Returns np.ndarray with shape=(n,)
    dispersion_matrix_function :
        This is L = L(t). Evaluations of this function are called
        the dispersion(matrix) of the SDE.
        Returns np.ndarray with shape=(n, s)
    mde_atol
        Absolute tolerance passed to the solver of the moment differential equations
        (MDEs). Optional. Default is 1e-6.
    mde_rtol
        Relative tolerance passed to the solver of the moment differential equations
        (MDEs). Optional. Default is 1e-6.
    mde_solver
        Method that is chosen in `scipy.integrate.solve_ivp`. Any string that is
        compatible with ``solve_ivp(..., method=mde_solve,...)`` works here.
        Usual candidates are ``[RK45, LSODA, Radau, BDF, RK23, DOP853]``.
        Optional. Default is LSODA.
    forward_implementation
        Implementation style for forward transitions.
    """

    def __init__(
        self,
        state_dimension: IntLike,
        wiener_process_dimension: IntLike,
        drift_matrix_function: Callable[[FloatLike], np.ndarray],
        force_vector_function: Callable[[FloatLike], np.ndarray],
        dispersion_matrix_function: Callable[[FloatLike], np.ndarray],
        mde_atol: Optional[FloatLike] = 1e-6,
        mde_rtol: Optional[FloatLike] = 1e-6,
        mde_solver: Optional[str] = "RK45",
        forward_implementation: Optional[str] = "classic",
    ):

        # Transform functions to be SDE-compatible and initialize super().
        def drift_function(t, x):
            return drift_matrix_function(t) @ x + force_vector_function(t)

        def drift_jacobian(t, x):
            return drift_matrix_function(t)

        def dispersion_function(t, x):
            return dispersion_matrix_function(t)

        super().__init__(
            state_dimension=state_dimension,
            wiener_process_dimension=wiener_process_dimension,
            drift_function=drift_function,
            drift_jacobian=drift_jacobian,
            dispersion_function=dispersion_function,
        )

        # Choose implementation for forward transitions
        choose_mde_forward_implementation = {
            "classic": self._solve_mde_forward_classic,
            "sqrt": self._solve_mde_forward_sqrt,
        }
        self._mde_forward_implementation = choose_mde_forward_implementation[
            forward_implementation
        ]

        # Once different smoothing algorithms are available,
        # replicate the scheme from NonlinearGaussian here, in which
        # the initialisation decides between, e.g., classic and sqrt implementations.

        # Store remaining functions and attributes
        self._drift_matrix_function = drift_matrix_function
        self._force_vector_function = force_vector_function
        self._dispersion_matrix_function = dispersion_matrix_function
        self._mde_atol = mde_atol
        self._mde_rtol = mde_rtol
        self._mde_solver = mde_solver
        self._forward_implementation_string = forward_implementation

    def drift_matrix_function(self, t):
        return self._drift_matrix_function(t)

    def force_vector_function(self, t):
        return self._force_vector_function(t)

    def dispersion_matrix_function(self, t):
        return self._dispersion_matrix_function(t)

    @property
    def mde_atol(self):
        return self._mde_atol

    @property
    def mde_rtol(self):
        return self._mde_rtol

    @property
    def mde_solver(self):
        return self._mde_solver

    @property
    def forward_implementation(self):
        return self._forward_implementation_string

    def forward_rv(
        self,
        rv,
        t,
        dt=None,
        _compute_gain=False,
        _diffusion=1.0,
        **kwargs,
    ):
        if dt is None:
            raise ValueError(
                "Continuous-time transitions require a time-increment ``dt``."
            )

        return self._mde_forward_implementation(rv, t, dt, _diffusion=_diffusion)

    def backward_rv(
        self,
        rv_obtained,
        rv,
        rv_forwarded=None,
        gain=None,
        t=None,
        dt=None,
        _diffusion=1.0,
        **kwargs,
    ):
        if dt is None:
            raise ValueError(
                "Continuous-time transitions require a time-increment ``dt``."
            )

        # Ignore rv_forwarded
        return self._solve_mde_backward(
            rv_obtained=rv_obtained,
            rv=rv,
            t=t,
            dt=dt,
            _diffusion=_diffusion,
        )

    # Forward and backward implementation(s)

    def _solve_mde_forward_classic(self, rv, t, dt, _diffusion=1.0):
        """Solve forward moment differential equations (MDEs)."""
        dim = rv.mean.shape[0]
        mde, y0 = self._setup_vectorized_mde_forward_classic(
            rv,
            _diffusion=_diffusion,
        )

        sol, new_mean, new_cov = self._solve_mde_forward(mde, y0, t, dt, dim)

        # Useful for backward transitions
        # Aka continuous time smoothing.
        sol_mean = lambda t: sol.sol(t)[:dim]
        sol_cov = lambda t: sol.sol(t)[dim:].reshape((dim, dim))

        return randvars.Normal(mean=new_mean, cov=new_cov), {
            "sol": sol,
            "sol_mean": sol_mean,
            "sol_cov": sol_cov,
        }

    def _solve_mde_forward_sqrt(self, rv, t, dt, _diffusion=1.0):
        """Solve forward moment differential equations (MDEs) using a square-root
        implementation."""
        dim = rv.mean.shape[0]
        mde, y0 = self._setup_vectorized_mde_forward_sqrt(
            rv,
            _diffusion=_diffusion,
        )

        sol, new_mean, new_cov_cholesky = self._solve_mde_forward(mde, y0, t, dt, dim)
        new_cov = new_cov_cholesky @ new_cov_cholesky.T

        # Useful for backward transitions
        # Aka continuous time smoothing.
        sol_mean = lambda t: sol.sol(t)[:dim]
        sol_cov_cholesky = lambda t: sol.sol(t)[dim:].reshape((dim, dim))
        sol_cov = (
            lambda t: sol.sol(t)[dim:].reshape((dim, dim))
            @ sol.sol(t)[dim:].reshape((dim, dim)).T
        )

        return randvars.Normal(
            mean=new_mean, cov=new_cov, cov_cholesky=new_cov_cholesky
        ), {
            "sol": sol,
            "sol_mean": sol_mean,
            "sol_cov_cholesky": sol_cov_cholesky,
            "sol_cov": sol_cov,
        }

    def _solve_mde_forward(self, mde, y0, t, dt, dim):
        """Solve forward moment differential equations (MDEs)."""
        # Dense output for lambda-expression
        sol = scipy.integrate.solve_ivp(
            mde,
            (t, t + dt),
            y0,
            method=self._mde_solver,
            atol=self._mde_atol,
            rtol=self._mde_rtol,
            dense_output=True,
        )
        y_end = sol.y[:, -1]
        new_mean = y_end[:dim]
        # If forward_sqrt is used, new_cov_or_cov_cholesky is a Cholesky factor of
        # the covariance
        # If forward_classic is used, new_cov_or_cov_cholesky is the covariance
        new_cov_or_cov_cholesky = y_end[dim:].reshape((dim, dim))

        return sol, new_mean, new_cov_or_cov_cholesky

    def _solve_mde_backward(self, rv_obtained, rv, t, dt, _diffusion=1.0):
        """Solve backward moment differential equations (MDEs)."""
        _, mde_forward_info = self._mde_forward_implementation(
            rv, t, dt, _diffusion=_diffusion
        )

        mde_forward_sol_mean = mde_forward_info["sol_mean"]
        mde_forward_sol_cov = mde_forward_info["sol_cov"]

        mde, y0 = self._setup_vectorized_mde_backward(
            rv_obtained,
            _diffusion=_diffusion,
        )
        # Use forward solution for mean and covariance in scipy's ivp
        # Dense output for lambda-expression
        sol = scipy.integrate.solve_ivp(
            mde,
            (t + dt, t),
            y0,
            method=self._mde_solver,
            atol=self._mde_atol,
            rtol=self._mde_rtol,
            args=(mde_forward_sol_mean, mde_forward_sol_cov),
            dense_output=True,
        )
        dim = rv.mean.shape[0]
        y_end = sol.y[:, -1]
        new_mean = y_end[:dim]
        new_cov = y_end[dim:].reshape((dim, dim))

        # Useful for backward transitions
        # Aka continuous time smoothing.
        sol_mean = lambda t: sol.sol(t)[:dim]
        sol_cov = lambda t: sol.sol(t)[dim:].reshape((dim, dim))

        return randvars.Normal(mean=new_mean, cov=new_cov), {
            "sol": sol,
            "sol_mean": sol_mean,
            "sol_cov": sol_cov,
        }

    def _setup_vectorized_mde_forward_classic(self, initrv, _diffusion=1.0):
        """Set up forward moment differential equations (MDEs).

        Compute an ODE vector field that represents the MDEs and is compatible with
        scipy.solve_ivp.
        """
        dim = len(initrv.mean)

        def f(t, y):
            # Undo vectorization
            mean, cov_flat = y[:dim], y[dim:]
            cov = cov_flat.reshape((dim, dim))

            # Apply iteration
            G = self.drift_matrix_function(t)
            u = self.force_vector_function(t)
            L = self.dispersion_matrix_function(t)
            new_mean = G @ mean + u
            new_cov = G @ cov + cov @ G.T + _diffusion * L @ L.T

            # Vectorize outcome
            new_cov_flat = new_cov.flatten()
            y_new = np.hstack((new_mean, new_cov_flat))
            return y_new

        initcov_flat = initrv.cov.flatten()
        y0 = np.hstack((initrv.mean, initcov_flat))

        return f, y0

    def _setup_vectorized_mde_forward_sqrt(self, initrv, _diffusion=1.0):
        r"""Set up forward moment differential equations (MDEs) using a square-root
        implementation. (https://ieeexplore.ieee.org/document/4045974)

        The covariance :math:`P(t)` obeys the Riccati equation

        .. math::
            \dot P(t) = G(t)P(t) + P(t)G^\top(t) + L(t)L^\top(t).

        Let :math:`S(t)` be a square-root of :math:`P(t)`, :math:`P(t)`
        positive definite, then

        .. math::
            P(t) = S(t)S^\top(t)

        and we get the Riccati-Equation

        .. math::
            \dot P(t) = G(t)S(t)S^\top(t) + 1/2 \cdot L(t)L^\top(t)S^{-\top}S^\top
                        + S(t)S^\top(t)G^\top(t) + 1/2 \cdot S(t)S^{-1}(t)L(t)L^\top(t).

        One solution can be found by the square-root :math:`\dot S(t)`

        .. math::
            \dot S(t) = G(t)S(t) + (A + 1/2 \cdot L(t)L^\top(t))S^{-\top}

        where :math:`A` is an arbitrary symmetric matrix.
        :math:`A` can be chosen to make S lower-triangular which can be achieved by

        .. math::
            M(t) = S^{-1}(t)\dot S(t) + \dot S(t)^\top S^{-\top}

        and

        .. math::
            M(t) = \bar G(t) + \bar G^\top(t) + \bar L(t) \bar L^\top(t)

        and

        .. math::
            \bar G(t) = S^{-1}(t)G(t)S(t),
            \bar L(t) = S^{-1}L(t)

        and

        .. math::
            \dot S(t) = S(t)[M(t)]_{\mathrm{lt}}

        where :math:`\mathrm{lt}` denotes the lower-triangular operator defined by

        .. math::
            [M(t)]{_{\mathrm{lt}}}_{ij} =
                \begin{cases}
                    0 & i < j\\
                    1/2 m(t)_{ij} & i=j\\
                    m(t)_{ij} & i > j
                \end{cases}.

        Compute an ODE vector field that represents the MDEs and is
        compatible with scipy.solve_ivp.
        """
        dim = len(initrv.mean)

        def f(t, y):
            # Undo vectorization
            mean, cov_cholesky_flat = y[:dim], y[dim:]
            cov_cholesky = cov_cholesky_flat.reshape((dim, dim))

            # Apply iteration
            G = self.drift_matrix_function(t)
            u = self.force_vector_function(t)
            L = self.dispersion_matrix_function(t)

            new_mean = G @ mean + u
            G_bar = scipy.linalg.solve_triangular(
                cov_cholesky, G @ cov_cholesky, lower=True
            )
            L_bar = np.sqrt(_diffusion) * scipy.linalg.solve_triangular(
                cov_cholesky, L, lower=True
            )
            M = G_bar + G_bar.T + L_bar @ L_bar.T

            new_cov_cholesky = tril_to_positive_tril(
                cov_cholesky @ (np.tril(M, -1) + 1 / 2 * np.diag(np.diag(M)))
            )

            # Vectorize outcome
            new_cov_cholesky_flat = new_cov_cholesky.flatten()
            y_new = np.hstack((new_mean, new_cov_cholesky_flat))
            return y_new

        initcov_cholesky_flat = initrv.cov_cholesky.flatten()
        y0 = np.hstack((initrv.mean, initcov_cholesky_flat))

        return f, y0

    def _setup_vectorized_mde_backward(self, finalrv_obtained, _diffusion=1.0):
        """Set up backward moment differential equations (MDEs).

        Compute an ODE vector field that represents the MDEs and is compatible with
        scipy.solve_ivp.
        """
        dim = len(finalrv_obtained.mean)

        def f(t, y, mde_forward_sol_mean, mde_forward_sol_cov):
            # Undo vectorization
            mean, cov_flat = y[:dim], y[dim:]
            cov = cov_flat.reshape((dim, dim))

            # Apply iteration
            G = self.drift_matrix_function(t)
            u = self.force_vector_function(t)
            L = self.dispersion_matrix_function(t)

            mde_forward_sol_cov_mat = mde_forward_sol_cov(t)
            mde_forward_sol_mean_vec = mde_forward_sol_mean(t)

            LL = _diffusion * L @ L.T
            LL_inv_cov = np.linalg.solve(mde_forward_sol_cov_mat, LL.T).T

            new_mean = G @ mean + LL_inv_cov @ (mean - mde_forward_sol_mean_vec) + u
            new_cov = (G + LL_inv_cov) @ cov + cov @ (G + LL_inv_cov).T - LL

            new_cov_flat = new_cov.flatten()
            y_new = np.hstack((new_mean, new_cov_flat))
            return y_new

        finalcov_flat = finalrv_obtained.cov.flatten()
        y0 = np.hstack((finalrv_obtained.mean, finalcov_flat))

        return f, y0


import os, json, random
from pathlib import Path

list_txt = open(os.path.abspath("./source/list.json"), "w+")

image = os.listdir(os.path.abspath("./source/image/"))
video = os.listdir(os.path.abspath("./source/video/"))
gif = os.listdir(os.path.abspath("./source/gif/"))

json_inner = {
    "images": image,
    "videos": video,
    "gifs": gif
}

def fold(path):
    if os.path.isfile(os.path.abspath(path)):
        print("add " + path +"+")
        os.system("git add " + path)
    else:
        for i in os.listdir(os.path.abspath(path)):
            if os.path.isfile(os.path.abspath(path)):
                fold(path)
            else:
                print("add " + path+"/"+i+"-")
                os.system("git add " + path+"/"+i)

os.system("cd C:/Users/ivans/OneDrive/Документы/GitHub/PRG")
fold("C:/Users/ivans/OneDrive/Документы/GitHub/PRG")

os.system(f"git commit -m \""+str(random.random()).split(".")[-1][::-1]+"\"")
os.system(f"git pull")
os.system(f"git push")

json.dump(json_inner, list_txt)


# -*- coding: utf-8 -*-
"""
Fetch rainfall data from the HIRDS website.
"""

import re
from typing import List, NamedTuple, Optional
from io import StringIO

import requests
from requests.structures import CaseInsensitiveDict
import pandas as pd

def get_site_url_key(site_id: str, idf: bool) -> str:
    """
    Get the unique URL key of the requested rainfall site from the HIRDS website.

    Parameters
    ----------
    site_id : str
        HIRDS rainfall site ID.
    idf : bool
        Set to False for rainfall depth data, and True for rainfall intensity data.

    Returns
    -------
    str
        Unique URL key of the requested rainfall site.
    """
    url = "https://api.niwa.co.nz/hirds/report"
    headers = CaseInsensitiveDict()
    headers["Accept"] = "application/json, text/plain, */*"
    headers["Accept-Language"] = "en-GB,en-US;q=0.9,en;q=0.8"
    headers["Connection"] = "keep-alive"
    headers["Content-Type"] = "application/json"
    headers["Origin"] = "https://hirds.niwa.co.nz"
    headers["Referer"] = "https://hirds.niwa.co.nz/"
    headers["Sec-Fetch-Dest"] = "empty"
    headers["Sec-Fetch-Mode"] = "cors"
    headers["Sec-Fetch-Site"] = "same-site"
    headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)\
        Chrome/96.0.4664.110 Safari/537.36"
    headers["sec-ch-ua"] = '"" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96""'
    headers["sec-ch-ua-mobile"] = "?0"
    headers["sec-ch-ua-platform"] = '""Windows""'
    # Convert idf parameter to lowercase string
    idf = str(idf).lower()
    data = f'{{"site_id":"{site_id}","idf":{idf}}}'
    # Make a POST request
    resp = requests.post(url, headers=headers, data=data)
    # Convert the response to a DataFrame
    rainfall_results = pd.read_json(resp.text)
    # Get the unique URL key of the requested rainfall site
    site_url = rainfall_results["url"][0]
    pattern = re.compile(r"(?<=/asset/)\w*(?=/)")
    site_url_key = re.findall(pattern, site_url)[0]
    return site_url_key

def get_data_from_hirds(site_id: str, idf: bool) -> str:
    """
    Fetch rainfall data for the requested rainfall site from the HIRDS website.

    Parameters
    ----------
    site_id : str
        HIRDS rainfall site ID.
    idf : bool
        Set to False for rainfall depth data, and True for rainfall intensity data.

    Returns
    -------
    str
        Rainfall data for the requested site as a string.
    """
    # Get the unique URL key of the requested rainfall site
    site_url_key = get_site_url_key(site_id, idf)

    url = rf"https://api.niwa.co.nz/hirds/report/{site_url_key}/export"
    headers = CaseInsensitiveDict()
    headers["Accept"] = "application/json, text/plain, */*"
    headers["Accept-Language"] = "en-GB,en-US;q=0.9,en;q=0.8"
    headers["Connection"] = "keep-alive"
    headers["Origin"] = "https://hirds.niwa.co.nz"
    headers["Referer"] = "https://hirds.niwa.co.nz/"
    headers["Sec-Fetch-Dest"] = "empty"
    headers["Sec-Fetch-Mode"] = "cors"
    headers["Sec-Fetch-Site"] = "same-site"
    headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)\
        Chrome/96.0.4664.110 Safari/537.36"
    headers["sec-ch-ua"] = '"" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96""'
    headers["sec-ch-ua-mobile"] = "?0"
    headers["sec-ch-ua-platform"] = '""Windows""'
    # Send HTTP GET request to the specified URL with headers
    response = requests.get(url, headers=headers)
    # Return the response content as a text string
    rainfall_data = response.text
    return rainfall_data

class BlockStructure(NamedTuple):
    """
    Represents the layout structure of fetched rainfall data.

    Attributes
    ----------
    skip_rows : int
        Number of lines to skip at the start of the fetched rainfall site_data.
    rcp : Optional[float]
        There are four different representative concentration pathways (RCPs), and abbreviated as RCP2.6, RCP4.5,
        RCP6.0 and RCP8.5, in order of increasing radiative forcing by greenhouse gases, or nan for historical data.
    time_period : Optional[str]
        Rainfall estimates for two future time periods (e.g. 2031-2050 or 2081-2100) for four RCPs, or None for
        historical data.
    category : str
        Historical data, Historical Standard Error or Projections (i.e. hist, hist_stderr or proj).
    """
    skip_rows: int
    rcp: Optional[float]
    time_period: Optional[str]
    category: str

def get_layout_structure_of_data(site_data: str) -> List[BlockStructure]:
    """
    Get the layout structure of the fetched rainfall data.

    Parameters
    ----------
    site_data : str
        Fetched rainfall data text string from the HIRDS website for the requested rainfall site.

    Returns
    -------
    List[BlockStructure]
        List of BlockStructure named tuples representing the layout structure of the fetched rainfall data.
    """
    layout_structure = []
    # Read the site_data text string line by line with a for loop
    for index, line in enumerate(StringIO(site_data)):
        # Get lines that contain "(mm) ::" for depth data or "(mm/hr) ::" for intensity data
        if "(mm) ::" in line or "(mm/hr) ::" in line:
            # Add the row number to skip_rows list
            skip_rows = index + 1
            # Add the obtained rcp and time_period values to list
            rcp_result = re.search(r"(\d*\.\d*)", line)
            period_result = re.search(r"(\d{4}-\d{4})", line)
            if rcp_result is not None or period_result is not None:
                # Extract the rcp value from the line
                rcp = float(rcp_result[0])
                # Extract the time_period value from the line
                time_period = period_result[0]
            else:
                # When there are no rcp and time_period values (i.e. for historical data)
                # Add nan or None to the list depending on the data type
                rcp = float("nan")
                time_period = None
            # Assign category to list based on the content of the line
            if "standard error" in line:
                category = "hist_stderr"
            elif "Historical Data" in line:
                category = "hist"
            else:
                category = "proj"
            # Create a BlockStructure named tuple and append it to the layout_structure list
            layout_structure.append(BlockStructure(skip_rows, rcp, time_period, category))
    return layout_structure

def convert_to_tabular_data(
        site_data: str, site_id: str, block_structure: BlockStructure) -> pd.DataFrame:
    """
    Convert the fetched rainfall data for the requested site into a Pandas DataFrame.

    Parameters
    ----------
    site_data : str
        Fetched rainfall data text string from the HIRDS website for the requested rainfall site.
    site_id : str
        HIRDS rainfall site ID.
    block_structure : BlockStructure
        The layout structure of the fetched rainfall data, containing skip rows, RCP, time period, and category.

    Returns
    -------
    pd.DataFrame
        Rainfall data for the requested site in tabular format.
    """
    # Extract values from the block_structure tuple
    skip_rows, rcp, time_period, category = block_structure
    # Read the site_data text string into a DataFrame
    rainfall_data = pd.read_csv(StringIO(site_data), skiprows=skip_rows, nrows=12)
    # Insert additional columns to the DataFrame
    rainfall_data.insert(0, "site_id", site_id)
    rainfall_data.insert(1, "category", category)
    rainfall_data.insert(2, "rcp", rcp)
    rainfall_data.insert(3, "time_period", time_period)
    # Convert column names to lowercase
    rainfall_data.columns = rainfall_data.columns.str.lower()
    return rainfall_data


import pygame as pg
import sys
import time
import os
from pygame.locals import *

size = 400
line_color = "black"
gap = size // 3

# prepare paths

fldr = os.path.split(os.path.abspath(__file__))[0]
img_dir = os.path.join(fldr, "images")

def load_image(name, size):
    file_loc = os.path.join(img_dir, name)

    img = pg.image.load(file_loc)
    # cur_width = img.get_width()
    # cur_height = img.get_height()

    img = pg.transform.scale(img, (size, size))

    return img

def draw_grid(surface):
    width = surface.get_width()
    gap = width // 3
    surface.fill("white")
    for i in range(gap, width, gap):
        pg.draw.line(surface, line_color, (0, i), (width, i), 2)
        pg.draw.line(surface, line_color, (i, 0), (i, width), 2)
    pg.display.flip()

pg.init()
screen = pg.display.set_mode((size, size))
pg.display.set_caption("My tic tac toe")
pg.mouse.set_visible(True)

x_img = load_image("x.png", gap)
o_img = load_image("o.png", gap)

bg = pg.Surface(screen.get_size())
bg = bg.convert()
bg.fill((0, 0, 200))

if pg.font:
    font_style = pg.font.Font(None, 50)
    text = font_style.render("Welcome to Tic Tac Toe", True, "white", "black")
    text_rect = text.get_rect(centerx=bg.get_width() / 2, y=bg.get_height() / 3)
    bg.blit(text, text_rect)

screen.blit(bg, (0, 0))
pg.display.flip()

time.sleep(2)

# quran = load_image("quran.jpg")
# screen.fill((0,0,0))
# screen.blit(quran, (0, 0))
# pg.display.flip()
# time.sleep(2)

draw_grid(screen)
time.sleep(3)

keep_playing = True

while keep_playing:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            keep_playing = False
        elif event.type == pg.MOUSEBUTTONDOWN:
            gap = screen.get_width() // 3
            x, y = pg.mouse.get_pos()
            x_block = x // gap
            y_block = y // gap

            print("In mouse event")

            x_loc = (x_block ) * gap
            y_loc = (y_block) * gap
            screen.blit(x_img, (x_loc, y_loc))
            pg.display.update()


import pygame
import random

SCREEN_W   = 800
SCREEN_H   = 600
SCREEN_TOP = 40

UP    = 1
DOWN  = 2
LEFT  = 3
RIGHT = 4

BLACK      = [0,0,0]
WHITE      = [255,255,255]
RED        = [255,0,0]
GREEN      = [0,255,0]
ORANGE     = [255,165,0]
BACKGROUND = [220,220,220]

FPS = 30
PLAYER_SPEED = 90.0/FPS
ENEMY_SPEED  = 30.0/FPS
BULLET_SPEED = 150.0/FPS

class Level():
    def __init__(self, player1, player2):
        self.pos_block = list()
        self.num_block = random.randint(50,100)
        while len(self.pos_block) < self.num_block:
            pos = (20*(random.randint(0, SCREEN_W-20)//20)+10, 20*(random.randint(40, SCREEN_H-20)//20)+10)
            while pos in self.pos_enemy or pos in self.pos_block \
            or (abs(pos[0]-player1.rect.centerx)<20 and abs(pos[1]-player1.rect.centery)<20) \
            or (abs(pos[0]-player2.rect.centerx)<20 and abs(pos[1]-player2.rect.centery)<20):
                pos = (20*(random.randint(0, SCREEN_W-20)//20)+10, 20*(random.randint(40, SCREEN_H-20)//20)+10)
            self.pos_block.append(pos)

class Block(pygame.sprite.Sprite):
    def __init__(self,pos):
        super(Block, self).__init__()
        self.max_life = 4
        self.life = random.randint(self.max_life//2, self.max_life)
        self.image = pygame.Surface((20,20))
        self.image.fill(BLACK)
        self.image.set_alpha(255*self.life/self.max_life)
        self.rect = self.image.get_rect(center=(pos[0],pos[1]))

    def hit(self):
        self.life -= 1
        if self.life == 0: self.kill()
        else:              self.image.set_alpha(255*self.life/self.max_life)

class Player(pygame.sprite.Sprite):
    def __init__(self,pos,color):
        super(Player, self).__init__()
        self.max_life = 10
        self.life = self.max_life
        self.image = pygame.Surface([20,20])
        self.image.fill(color)
        self.image.set_alpha(255*self.life/self.max_life)
        self.rect = self.image.get_rect(center=pos)
        self.speed = PLAYER_SPEED
        self.direction = None

    def update(self, events, blocks):
        enable = True
        if events[3].is_set(): self.direction = UP
        elif events[4].is_set(): self.direction = DOWN
        elif events[1].is_set(): self.direction = LEFT
        elif events[2].is_set(): self.direction = RIGHT

        if self.direction == UP:
            for block in blocks:
                if abs(self.rect.centerx-block.rect.centerx) < 20 and abs((self.rect.centery-5)-block.rect.centery) < 20: enable = False
            if enable: self.rect.move_ip(0, -self.speed)

        elif self.direction == DOWN:
            for block in blocks:
                if abs(self.rect.centerx-block.rect.centerx) < 20 and abs((self.rect.centery+5)-block.rect.centery) < 20: enable = False
            if enable: self.rect.move_ip(0, self.speed)

        elif self.direction == LEFT:
            for block in blocks:
                if abs((self.rect.centerx-5)-block.rect.centerx) < 20 and abs(self.rect.centery-block.rect.centery) < 20: enable = False
            if enable: self.rect.move_ip(-self.speed, 0)

        elif self.direction == RIGHT:
            for block in blocks:
                if abs((self.rect.centerx+5)-block.rect.centerx) < 20 and abs(self.rect.centery-block.rect.centery) < 20: enable = False
            if enable: self.rect.move_ip(self.speed, 0)

        if self.rect.left < 0:
            self.rect.left = 0
        elif self.rect.right > SCREEN_W:
            self.rect.right = SCREEN_W
        if self.rect.top <= SCREEN_TOP:
            self.rect.top = SCREEN_TOP
        elif self.rect.bottom >= SCREEN_H:
            self.rect.bottom = SCREEN_H

    def hit(self):
        self.life -= 1
        if self.life == 0: self.kill()
        else:              self.image.set_alpha(255*self.life/self.max_life)

class Bullet(pygame.sprite.Sprite):
    def __init__(self, pos, direction):
        super(Bullet, self).__init__()
        self.image = pygame.Surface((10,10))
        self.image.fill(ORANGE)
        self.rect = self.image.get_rect(center=(pos[0],pos[1]))
        self.direction = direction
        self.speed = BULLET_SPEED

    def update(self):
        if self.direction == LEFT:  self.rect.move_ip(-self.speed,0)
        if self.direction == RIGHT: self.rect.move_ip(self.speed,0)
        if self.direction == UP:    self.rect.move_ip(0,-self.speed)
        if self.direction == DOWN:  self.rect.move_ip(0,self.speed)

        if self.rect.left < 0:           self.kill()
        if self.rect.right > SCREEN_W:   self.kill()
        if self.rect.top <= SCREEN_TOP:  self.kill()
        if self.rect.bottom >= SCREEN_H: self.kill()

def game(events1,events2):
    pygame.init()
    pygame.font.init()
    screen = pygame.display.set_mode((SCREEN_W, SCREEN_H))
    pygame.display.set_caption('Tanks Battle PVP version')
    clock = pygame.time.Clock()
    init_level = True
    starting   = True
    running    = True
    ending     = True
    winner     = 0

    big_font   = pygame.font.SysFont('Verdana', 80)
    small_font = pygame.font.SysFont('Verdana', 30)

    bg_img = pygame.image.load('pngtree-camouflage-block-texture-background-image_910986.png').convert()
    bg_img = pygame.transform.scale(bg_img, (SCREEN_W, SCREEN_H))
    bg_img.set_alpha(100)

    player1         = Player((50,50),GREEN)
    player2         = Player((SCREEN_W-50,SCREEN_H-50),RED)
    level           = Level(player1,player2)
    blocks          = pygame.sprite.Group()
    player1_bullets = pygame.sprite.Group()
    player2_bullets = pygame.sprite.Group()
    all_item        = pygame.sprite.Group()
    all_item.add(player1)
    all_item.add(player2)

    bullet_counter1 = 15
    bullet_counter2 = 15

    while starting:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

        if events1[5].is_set() or events2[5].is_set():
            starting = False

        text = big_font.render(f'Tank Battle PVP Version', False, (0, 0, 0))
        text2 = small_font.render(f'Press bottom to continue', False, (0, 0, 0))
        screen.fill(WHITE)
        screen.blit(bg_img, (0, 0))
        screen.blit(text, ((SCREEN_W-text.get_width())/2, (SCREEN_H-text.get_height())/2))
        screen.blit(text2, ((SCREEN_W-text2.get_width())/2, (SCREEN_H-text2.get_height())/2+100))
        pygame.display.flip()
        clock.tick(FPS)

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

        if init_level:
            for i in range(level.num_block):
                new_block = Block(level.pos_block[i])
                blocks.add(new_block)
                all_item.add(new_block)
            init_level = False

        screen.fill(WHITE)

        if events1[5].is_set() and bullet_counter1 == 0 and player1.direction != None:
            new_bullets = Bullet([player1.rect.centerx,player1.rect.centery], player1.direction)
            player1_bullets.add(new_bullets)
            all_item.add(new_bullets)
            bullet_counter1 = 1

        if bullet_counter1 != 0:
            bullet_counter1 += 1
            bullet_counter1 %= 30

        if events2[5].is_set() and bullet_counter2 == 0 and player2.direction != None:
            new_bullets = Bullet([player2.rect.centerx,player2.rect.centery], player2.direction)
            player2_bullets.add(new_bullets)
            all_item.add(new_bullets)
            bullet_counter2 = 1

        if bullet_counter2 != 0:
            bullet_counter2 += 1
            bullet_counter2 %= 30

        player1.update(events1,blocks)
        player2.update(events2,blocks)

        for bullet in player1_bullets:
            if bullet.rect.colliderect(player2.rect):
                bullet.kill()
                player2.hit()
                if player2.life == 0:
                    winner = 1
                    running = False

            for block in blocks:
                if bullet.rect.colliderect(block.rect):
                    bullet.kill()
                    block.hit()

            if bullet.alive():
                bullet.update()

        for bullet in player2_bullets:
            if bullet.rect.colliderect(player1.rect):
                bullet.kill()
                player1.hit()
                if player1.life == 0:
                    winner = 2
                    running = False

            for block in blocks:
                if bullet.rect.colliderect(block.rect):
                    bullet.kill()
                    block.hit()

            if bullet.alive():
                bullet.update()

        for item in all_item:
            screen.blit(item.image, item.rect)

        text = small_font.render(f'P1 HP: {player1.life}     P2 HP: {player2.life}', False, (0, 0, 0))
        screen.blit(text, (50,0))
        pygame.draw.line(screen, BLACK, (0,SCREEN_TOP-2), (SCREEN_W, SCREEN_TOP-2), 5)
        pygame.display.flip()
        clock.tick(FPS)

    while ending:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

        if events1[5].is_set() or events2[5].is_set():
            ending = False

        text = big_font.render(f'Player{winner} Win!!', False, (0, 0, 0))
        text2 = small_font.render(f'Press bottom to continue', False, (0, 0, 0))
        screen.fill(WHITE)
        screen.blit(bg_img, (0, 0))
        screen.blit(text, ((SCREEN_W-text.get_width())/2, (SCREEN_H-text.get_height())/2))
        screen.blit(text2, ((SCREEN_W-text2.get_width())/2, (SCREEN_H-text2.get_height())/2+100))
        pygame.display.flip()
        clock.tick(FPS)