# -*- coding: utf-8 -*-
"""
This file defines the parameters for spectrogram computation.
"""
import sys
#import pandas as pd

AUDIO_DIR = "/content/drive/MyDrive/RP/DataSet/recordings" # the folder containing audio files to be converted to spectrograms
SPEC_DIR = "/content/drive/MyDrive/RP/DataSet/specs" # the folder where you want the spectrogram images to be saved
SD_SPEC_DIR = "/content/drive/MyDrive/RP/DataSet/detected_specs"
LABEL_PATH = "/content/drive/MyDrive/RP/DataSet/labels.csv" # the csv file with species labels

AUG_SPEC_DIR = "/content/drive/MyDrive/RP/DataSet/aug_specs"
AUG_SD_SPEC_DIR = "/content/drive/MyDrive/RP/DataSet/aug_detected_specs"

'''
bird_data = pd.read_csv(LABEL_PATH)
print(bird_data)
print(bird_data.columns.values.tolist())
df2 = bird_data.groupby(['class_label'])['class_label'].count()
print(df2)
'''

## Parameters of Signal
##
SAMPLE_RATE = 48000 # Hz
DURATION = 15 # If DURATION == None, the files are not cut
OFFSET = 0.0 # Start time if cutting

##
CHUNK_SEC = 3
CHUNK_OVERLAP = 0.25

##
FMIN = 150
FMAX = 15000

## Parameters of Spectrogram
##
WIN_LEN = 512
WIN_OVERLAP = 0.50
##
N_MELS = 64
N_WIN = 512

##
RANDOM_SEED = 42

##
SIG_DETECTION = None#'median_clipping'

##
##
AUGMENTATION = True
SAMPLE_DICT = {"Saxicola torquatus_African Stonechat":214,
                "Perdix perdix_Grey Partridge":196,
                "Sylvia nisoria_Barred Warbler":97,
                "Lanius collurio_Red-backed Shrike":93,
                "Anthus pratensis_Meadow Pipit":66,
                "Alauda arvensis_Eurasian Skylark":58,
                "Emberiza hortulana_Ortolan Bunting":43,
                "Saxicola rubetra_Whinchat":0,
                "Linaria cannabina_Common Linnet":0,
                "Passer montanus_Eurasian Tree Sparrow":0,
                "Sylvia curruca_Lesser Whitethroat":0,
                "Emberiza calandra_Corn Bunting":0,
                "Motacilla flava_Western Yellow Wagtail":0,
                "Emberiza citrinella_Yellowhammer":0,
                "Sylvia communis_Common Whitethroat":0}
TRANSFORMS = {
    "train": [{"name": "PitchShift"},{"name": "TimeStretch"},{"name": "GaussianNoise"}]#,
    # "valid": [{"name": "Normalize"}],
    # "test": [{"name": "Normalize"}]
}

SPEC_DICT = {"Saxicola torquatus_African Stonechat":522,
                "Perdix perdix_Grey Partridge":483,
                "Sylvia nisoria_Barred Warbler":220,
                "Lanius collurio_Red-backed Shrike":425,
                "Anthus pratensis_Meadow Pipit":261,
                "Alauda arvensis_Eurasian Skylark":0,
                "Emberiza hortulana_Ortolan Bunting":170,
                "Saxicola rubetra_Whinchat":0,
                "Linaria cannabina_Common Linnet":154,
                "Passer montanus_Eurasian Tree Sparrow":0,
                "Sylvia curruca_Lesser Whitethroat":0,
                "Emberiza calandra_Corn Bunting":0,
                "Motacilla flava_Western Yellow Wagtail":0,
                "Emberiza citrinella_Yellowhammer":0,
                "Sylvia communis_Common Whitethroat":0}

SPEC_TRANSFORMS = {
    "spectrogram_transforms":{"train": [{"name": "TimeFreqMasking","params":{"time_drop_width":10,
                 "time_stripes_num":80, "freq_drop_width":2,"freq_stripes_num":5}},
                                        {"name": "TimeFreqShifting","params":{"shift_max":200,"roll":True}}]}
}


#! /usr/bin/python3
# coding : utf-8

"""
Created on Thu Apr 1 13:10:33 2019

@author: wyatt

1.读取folder文件夹下的dac文件列表
2.读取每一个dac文件的内容，保存到矩阵
3.然后把矩阵保存到.dat文件
"""

import data_process.FileProcess as fp

def main():
    folder = r"G:\Group_Work\Wyatt_Experiment\NanoSheets\TW\tangbing_sample\ColdTemp"
    fp.loop_folder_dac2dat(folder)

if __name__ == '__main__':
    main()


import websocket, orjson, time, threading, math

priceLog = []

def on_message(ws, message):
    data = orjson.loads(message)
    btcPrice = False
    ethPrice = False
    # выбираю цены по btc и eth
    for i in range(len(data)):
        if data[i]['s'] == 'BTCUSDT':
            btcPrice = float(data[i]['p'])
            curTime = data[i]['E']
        if data[i]['s'] == 'ETHUSDT':
            ethPrice = float(data[i]['p'])
    if btcPrice and ethPrice:
        if len(priceLog) > 0 and curTime - priceLog[0]['time'] > 3600000:
            # выбираю цену, которая была час назад и удаляю лишние данные из priceLog
            while curTime - priceLog[0]['time'] > 3600000:
                btcPricePast = priceLog[0]['btc']
                ethPricePast = priceLog[0]['eth']
                priceLog.pop(0)
            # логика расчётов
            btcPriceDelta = (btcPrice - btcPricePast)/btcPricePast
            ethPriceDelta = (ethPrice - ethPricePast)/ethPricePast
            ethPriceDelta_real = ethPriceDelta - (btcPriceDelta * 1.192)

            if math.fabs(ethPriceDelta_real) > 0.01:
                #if time.time() - curTime * 1000 < 1500:        #тут можно проверять временные задержки (данные с биржи не старее 1.5 секунд)
                print('Цена ETH изменилась на 1%')
        # добавляю новые данные в priceLog
        priceLog.append(
            {
                'btc': btcPrice,
                'eth': ethPrice,
                'time': curTime
            }
        )

def on_error(ws, error):
    print(error)

def on_close(ws, close_status_code, close_msg):
    print("### closed ###")

def on_open(ws):
    print("Opened connection")

# сенеджер потоков обеспечивает переподключение к вебсокетам в случае возникновения любых ошибок
def ws_manager():
    while True:
        try:
            ws = websocket.WebSocketApp(
                "wss://fstream.binance.com/ws/!markPrice@arr@1s",
                on_open=on_open,
                on_message=on_message,
                on_error=on_error,
                on_close=on_close
            )
            ws.run_forever()
        except Exception as ex:
            print(ex)
            time.sleep(1)

ws_manager_thread = threading.Thread(target=ws_manager, daemon=True)
ws_manager_thread.start()
ws_manager_thread.join()


# Create Lists
fav_colors = ['red', 'blue', 'purple', 'seafoam']
cousin_ages = [31, 33, 25, 35, 22]
flipped_heads = [True, True, False, False, True]
fav_artists = ['Travis', 'Bazzi', 'Tinashe']

# Create Dictionaries
words_defs = {
    'blossom': 'the state of flowering',
    'flower': 'the blossom of a plant',
    'stem': 'the stalk that supports a lead, flower, or fruit'
}
movie_years = {
    'Hitch': 2005,
    'Over the Hedge': 2006,
    'Kung Fu Panda': 2008
}
cities_pop = {
    'Toronto': 2.93,
    'Sydney': 4.627,
    'Milan': 1.352
}
family_ages = {
    'Amy': 22,
    'Ida': 31,
    'Filly': 35,
    'Huoi': 33
}

# EXERCISE 1
# print(flipped_heads)

# print(fav_colors[0])

# print(sorted(cousin_ages))

# cousin_ages.append('0')
# print(cousin_ages)

# print(movie_years['Hitch'])

# EXERCISE 2
# print(fav_colors[-1])

# cities_pop['Paris'] = 2.141
# print(cities_pop)

# list.reverse(flipped_heads)
# print(flipped_heads)

# print(cities_pop['Milan'])

# for artist in fav_artists:
#     print("I think {} is great.".format(artist))

# EXERCISE 3
# print(fav_artists[0:2])

# for movie, year in movie_years.items():
#     print("{} came out in {}.".format(movie,year))

# print(list(reversed(sorted(cousin_ages))))

# movie_years['Beauty and the Beast'] = [1991, 2017]
# print(movie_years)

# EXERCISE 4
# under_thirty = list(filter(lambda number: number < 30, cousin_ages))
# print(under_thirty)

# cousin_ages.sort()
# print(cousin_ages[-1])

# print(flipped_heads.count(True))

# fav_artists.pop(1)
# print(fav_artists)

# cities_pop['Sydney']=4.900
# print(cities_pop)

# EXERCISE 5
# print(sum(cities_pop.values()))

# for names,ages in family_ages.items():
#     if ages > 30:
#         print("{} is old.".format(names))
#     else:
#         print("{} is young.".format(names))

# print(fav_colors[-2:])

# for ages in cousin_ages:
#     print(ages + 1)

# fav_colors.append('coral')
# fav_colors.append('lime green')
# print(fav_colors)

# EXERCISE 6
listed_movies = {
    1999: ['The Matrix', 'Star Wars: Episode 1', 'The Mummy'],
    2009: ['Avatar','Star Trek', 'District 9'],
    2019: ['How to Train Your Dragon 3', 'Toy Story 4', 'Star Wars: Episode 9']
}
phone_buttons = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    ['*', 0, '#']
]
country_info = [
    {
        'Country':'Peru',
        'Continent': 'South America',
        'Island': False
    },
    {
        'Country':'New Zealand',
        'Continent': 'Australia',
        'Island': True
    },
    {
        'Country': 'Chile',
        'Continent': 'South America',
        'Island': False
    }
]

# EXERCISE 7
# for i in range(20):
#     print("I will not skateboard in the halls")

# my_list = []
# for i in range(20):
#     my_list.append("I will not skateboard in the halls")
# print(my_list)

# one_to_fifty = []
# for i in range(1,51):
#     one_to_fifty.append(i)
# print(one_to_fifty)

# total = 0
# for num in one_to_fifty:
#     total += num
# print(total)

# triple_list = []
# for i in range(1,51):
#     triple_list.append(i)
#     triple_list.append(i)
#     triple_list.append(i)
# print(triple_list)

# for country in country_info:
#     if country["Island"] == False:
#         print(country["Country"])

# EXERCISE 8
# expense_list_a = [250, 7.95, 30.95, 16.50, 500, 1275]
# expense_list_b = [530, 72.95, 780.95, 1.50, 5.50, 2055]
# print(sum(expense_list_a))
# print(sum(expense_list_b))

# def expense_total(l):
#     return sum(l)

# print(expense_total(expense_list_a))
# print(expense_total(expense_list_b))


import numpy as np

class Perceptron:
    def __init__(self):
        self.__inputs = None
        self.__weights = None
        self.__output = None

    def __update_inputs(self, inputs):
        # reshaping inputs to add bias (adding a first fictitious value = 1)
        self.__inputs = np.eye(inputs.shape[0] + 1, inputs.shape[0], k=-1) @ inputs
        self.__inputs[0][0] = 1

    def update_weights_and_bias(self, weights, bias):
        # reshaping weights to add bias. Bias is the first weight
        self.__weights = np.eye(weights.shape[0] + 1, weights.shape[0], k=-1) @ weights
        self.__weights[0][0] = bias

    def get_result(self, inputs):
        if self.__weights is not None:
            self.__update_inputs(inputs)
            self.__output = self.__act_func(self.__weights.T @ self.__inputs)
        else:
            print("Not all required params are set")
        return self.__output

    @staticmethod
    def __act_func(value):
        if value > 0:
            return 1
        else:
            return 0

if __name__ == "__main__":
    my_perceptron = Perceptron()
    # representing simple boolean function AND
    my_perceptron.update_weights_and_bias(np.array([[1],
                                                    [1]]), -1.5)

    print(my_perceptron.get_result(np.array([[1],
                                             [1]])))


r"""We deal with the 196884-dimensional representation of the monster.

Here class ``MMSpaceCRT`` is an analogue of class ``MMSpace`` that
allows a limited set of operations on the real
``196884``-dimensional representation of the monster with vectors of
a small norm. Typical examples of such vectors are unit vectors, or
so called *axes*, as described in :cite:`Con85`.

"""
# References in the __docstr__ see file docs/source/references.bib

from __future__ import absolute_import, division, print_function
from __future__ import  unicode_literals

import sys
import os
import re
import numpy as np
from numbers import Integral
import warnings
from collections import defaultdict, OrderedDict
import math
from functools import partial

from mmgroup.structures.abstract_group import singleton
from mmgroup.structures.abstract_mm_rep_space import AbstractMmRepVector
from mmgroup.structures.abstract_mm_rep_space import AbstractMmRepSpace
from mmgroup.structures.mm_space_indices import tuple_to_sparse
from mmgroup.structures.mm_space_indices import numeric_index_to_sparse
from mmgroup.structures.mm_space_indices import sparse_from_indices
from mmgroup.structures.abstract_group import AbstractGroupWord
from mmgroup.structures.abstract_mm_group import AbstractMMGroupWord
from mmgroup.mm_space  import MMSpace, MMVector
from mmgroup.mm_space  import standard_mm_group
from mmgroup.structures.parse_atoms import AtomDict
from mmgroup.structures.parse_atoms import eval_atom_expression, ihex

from mmgroup.mm_op import mm_aux_mmv_size
from mmgroup.mm_op import mm_vector, mm_aux_random_mmv
from mmgroup.mm_op import mm_aux_zero_mmv, mm_aux_reduce_mmv
from mmgroup.mm_op import mm_aux_mmv_to_sparse
from mmgroup.mm_op import mm_aux_mmv_set_sparse
from mmgroup.mm_op import mm_crt_combine, mm_crt_check_v2
from mmgroup.mm_op import mm_crt_combine_bytes
from mmgroup.mm_op import mm_crt_check_g
from mmgroup.mm_op import mm_crt_norm_int32
from mmgroup.generators import mm_group_n_clear
from mmgroup.generators import mm_group_n_mul_word_scan
from mmgroup.generators import mm_group_n_to_word
from mmgroup.mm_op import mm_op_word
from mmgroup.mm_op import mm_op_compare

PRECISION = math.log(7 * 31 * 127 * 255) / math.log(2.0) - 4

######################################################################
# Auxiliary class vsparse representing a sparse vector
######################################################################

ERR_CRT_TYPE = "Connot construct MMVectorCRT object from type '%s'"

class vsparse:
    def __init__(self, *data):
        self.d = defaultdict(int)
        if len(data) == 0 or not data[0]:
            return
        scalar = 1
        if isinstance(data[0], vsparse):
            self.d.update(data[0].d)
            return
        if isinstance(data[0], Integral):
            scalar, data = data[0],  data[1:]
        if scalar == 0 or len(data) == 0:
            return
        if isinstance(data[0], str):
            if len(data[0]) != 1 or not data[0] in "ABCTXZYIDE0":
                err = "Illegal tag '%s' in tuple for class MMSpaceCRT"
                raise ValueError(err %  data[0])
        else:
            raise TypeError(ERR_CRT_TYPE % type(data[0]))
        for t in tuple_to_sparse(255, *data):
            scalar1, t =  t & 0xff, t & 0xffffff00
            scalar1 = scalar1 if scalar1 < 128 else scalar1 - 255
            self.d[t] += scalar * scalar1

    def __imul__(self, other):
        assert isinstance(other, Integral)
        if other == 0:
            self.d.clear()
        else:
            for tag in self.d.keys():
                 self.d[tag] *= other
        return self

    def __mul__(self, other):
        return vsparse(self).__imul__(other)

    __rmul__ = __mul__

    def __neg__(self):
        return self.__mul__(-1)

    def __pos__(self):
        return self

    def __iadd__(self, other):
        if other == 0:
            return
        assert isinstance(other, vsparse)
        for tag, value in other.d.items():
            self.d[tag] += value
        return self

    def __add__(self, other):
        return vsparse(self).__iadd__(other)

    def __isub__(self, other):
        if other == 0:
            return
        assert isinstance(other, vsparse)
        for tag, value in other.d.items():
            self.d[tag] -= value
        return self

    def __sub__(self, other):
        return vsparse(self).__isub__(other)

    def reduce(self):
        for tag, value in self.d.items():
            if value == 0:
                del self.d[tag]
        return self

    def norm(self):
        norm = 0
        self.reduce()
        for tag, value in self.d.items():
            factor = 1
            if tag & 0xE000000 == 0x2000000:
                i0, i1 = (tag >> 14) & 0x7ff, (tag >> 8) & 0x3f
                factor += i0 != i1
            norm += factor * value * value
        return norm

    def sparse_array(self, p, shift = 0):
        assert p & 1 and p < 256
        self.reduce()
        a = np.zeros(len(self.d), dtype = np.uint32)
        for i, (tag, value) in enumerate(self.d.items()):
            a[i] = tag + (value << shift) % p
        return a

######################################################################
# Convert string to instance of class vsparse
######################################################################

FRAME = re.compile(r"^([A-Za-z_])+\<([0-9]+;)?(.+)\>$")

def vsparse_from_str(s):
    string = s
    m = FRAME.match(s)
    if m:
        _, p_str, string = m[1], m[2], m[3]
        if p_str:
            err = "Vector is defined module an integer only"
            raise ValueError(err)
    f = AtomDict(vsparse)
    return eval_atom_expression(string, f)

######################################################################
# Auxiliary functions
######################################################################

def _err_tag(*args, **kwds):
    err = "Bad entry in monster group element"
    raise ValueError(err)

def _iter_group(g):
    start, data, len_ = 0, g.mmdata, len(g.mmdata)
    while start < len_:
        tag = data[start] & 0x70000000
        if  tag >= 0x50000000:
            yield  data[start : start+1], True
            start += 1
        else:
            end_ = start
            while end_ < len_ and data[end_] & 0x70000000 < 0x50000000:
               end_ += 1
            yield data[start : end_], False
            start = end_

def compress_data(data):
    out = np.zeros(196884, dtype = np.int32)
    out[0:24] = data[0:768:33]
    k = 24
    for i in range(1,24):
        out[k:k+i] = data[32*i:32*i+i]
        out[276+k:276+k+i] = data[768+32*i:768+32*i+i]
        out[552+k:552+k+i] = data[1536+32*i:1536+32*i+i]
        k += i
    assert k == 300
    out[852:49428] = data[2304:50880]
    t = data[50880:247488].reshape((3*2048,32))[:,:24]
    out[49428:196884] = t.ravel()
    assert len(out) == 196884
    return out

######################################################################
# If check_MMVectorCRT is True then we check a vector of type
# MMVectorCRT for overflow or underflow after each operation
######################################################################

check_MMVectorCRT = True

######################################################################
# Modelling a vector of the 196884-dimensional rep of the monster
######################################################################

MODULUS = 7*31*125*255
MAX_CRT_NORM = MODULUS**2 // 4
ERR_OVERFLOW = "Overflow in class MMVectorCRT"
ERR_UNDERFLOW = "Underflow in class MMVectorCRT"
ERR_UNDERFLOW_G = "Underflow at group operation in class MMVectorCRT"

class MMVectorCRT(AbstractMmRepVector):
    """Models a vector in a space of type ``MMSpaceCRT``.

    Such a vector should be constructed by calling an instance ``V``
    of class ``MMSpaceCRT`` which models a real representation of
    the monster group. Calculations in this space are exact in
    fixed-point arithmetic with a precision of about %.2f bits.

    ValueError is raised in case of overflow or underflow.

    The functionality of this class is a subset of the functionality
    of class ``MMVector``. See class ``MMSpaceCRT`` for details.

    A vector may also be reduced modulo ``p = 7, 31, 127, 255`` with
    the modulo operator ``%%``. Then the result is a vector in the
    vector space ``MMSpace(p)``.

    :var space:
        This attribute contains the space to which the vector
        belongs. That space is an instance of class |MMSpaceCRT|.

    .. warning::
       The constructor of this class is not for public use! You
       may call an instance ``V`` of class  |MMSpaceCRT| for
       constructing vectors in the real representation space
       ``V`` of the monster group.
    """ % PRECISION
    p = MODULUS
    MAX_CRT_NORM = MAX_CRT_NORM

    def __init__(self, shift, tag = 0, i0 = None, i1 = None):
        self.shift = shift
        self.factor = 1.0 / (1 << shift)
        if not 3 <= self.shift <= 21:
            raise ValueError("Bad shift factor for class MMVectorCRT")
        self.data = OrderedDict()
        self.data_int = np.zeros(247488, dtype = np.int32)
        self.expanded = False
        d = vsparse()
        if isinstance(tag, MMVectorCRT):
            for p in (7, 31, 127, 255):
                self.data[p] = tag.data[p].copy()
            self.shl(self.shift - tag.shift)
            return
        elif isinstance(tag, Integral) and not tag:
            for p in (7, 31, 127, 255):
                self.data[p] = MMVector(p)
            return
        elif isinstance(tag, str) and len(tag) == 1:
            d += vsparse(tag, i0, i1)
        elif isinstance(tag, str) and tag == "Axis":
            g = None
            if isinstance(i0, AbstractMMGroupWord):
               i0, g = MMSpace._mm_element_to_axis(i0)
               i1 = None
            for p in (7, 31, 127, 255):
                self.data[p]  = MMVector(p, tag, i0, i1) << self.shift
            if g is not None:
                self *= g
            return
        elif isinstance(tag, str):
            d += vsparse_from_str(tag)
        elif isinstance(tag, list):
            for x in tag:
                if isinstance(x,tuple):
                    d += vsparse(*x)
                elif isinstance(x, str):
                    d += vsparse_from_str(x)
        else:
            err = "Connot construct MMVectorCRT object from type '%s'"
            raise ValueError(err % type(tag))
        for p in (7, 31, 127, 255):
            self.data[p] = v = MMVector(p)
            ind = d.sparse_array(p, shift)
            mm_aux_mmv_set_sparse(p, v.data, ind, len(ind))
        if check_MMVectorCRT:
            self.expand()
            if self._inorm > MAX_CRT_NORM:
                raise ValueError(ERR_OVERFLOW)

    def expand(self):
        """Expand data of vector ``v`` with CRT

        In the array ``v.data_int`` the ``i``-th entry is computed
        form the ``i``-th entries of the vector modulo 7, 31, 127,
        and 255.
        """
        if not self.expanded:
            v2 = mm_crt_combine(self.data[7].data,
                self.data[31].data, self.data[127].data,
                self.data[255].data, self.data_int)
            self._v2 = v2 - self.shift if v2 < 24 else 24
            self._inorm = mm_crt_norm_int32(self.data_int)
            self.expanded = True

    def shl(self, sh):
        assert isinstance(sh, Integral)
        if sh == 0:
            return self
        if sh > 0:
            if check_MMVectorCRT:
                self.expand()
                if sh > 24 or self._inorm * 4**sh > MAX_CRT_NORM:
                    raise ValueError(ERR_OVERFLOW)
                self.data_int <<= sh
                self._inorm *= 4**sh
            for d in self.data.values:
                d <<= sh
            self._v2 += sh
        elif sh < 0:
            nsh = -sh
            if check_MMVectorCRT:
                self.expand()
                if nsh > self.v2:
                    raise ValueError(ERR_UNDERFLOW)
                self.data_int >>= nsh
                self._inorm *= 4**sh
            for d in self.data.values:
                d >>= nsh
            self._v2 += sh
        return self

    def check(self):
        """Check if the vector is correct

        Raise ValueError if the vector is erroneous.
        """
        return True

    def __ilshift__(self, other):
        return self.shl(other)

    def __lshift__(self, other):
        return self.copy().shl(other)

    def __irshift__(self, other):
        return self.shl(-other)

    def __lrhift__(self, other):
        return self.copy().shil(-other)

    def __mod__(self, p):
        """Return the vector modulo ``p``.

        ``p``  must be in (7, 31, 127, 255). Actually, we divide
        the vector by the *scaling factor* before reducing it
        modulo ``p``.

        This method is mainly for testing.
        """
        if  p in (7, 31, 127, 255):
            return MMVector(p, self.data[p]) >> self.shift
        elif p in (3, 15):
            v0 = self % 255
            return MMVector(p, v0)
        elif isinstance(p, Integral):
            err = "Cannot reduce MMVectorCRT object modulo %d"
            raise ValueError(err % p)
        else:
            err = "Modulus for reducing MMVectorCRT object must be int"
            raise TypeError(err)

    @property
    def v2(self):
        """Return the ``2``-adic value of the vector.

        If "a, b" are odd integers and ``k`` is an integer then
        the  ``2``-adic value  ``a * 2**k / b`` is ``k``.

        The  ``2``-adic value of a vector is the minimum of the
         ``2``-adic values of its entries, ignoring zero entries.

        The function raises ZeroDivisionError if ``v ==  0``
        """
        v2 = self._v2
        if self._v2 >= 24:
            err = "The zero vector has infinite 2-adic value"
            raise ZeroDivisionError(err)
        return self._v2

    @property
    def inorm(self):
        """Return a scaled norm of the vector as an integer

        For a vector ``v`` we have

            ``v.fnorm() = v.inorm() * v.factor**2``.

        Where ``v.fnorm()`` is the real norm of ``v``.
        """
        self.expand()
        return self._inorm

    def norm(self):
        """Return norm of vector as a floating point number.

        The norm of a vector in the representation of the monster
        is the squared sum of is entries. Here the squares of all
        entries with index ``("A", i0, i1)`` must be doubled in
        case ``i0 != i1``.

        The returned norm is exact.
        """
        return self.inorm * self.factor**2

######################################################################
# class MMSpace
######################################################################

@singleton
class MMSpaceCRT(AbstractMmRepSpace):
    """Models a ``196884``-dimensional representation of the monster group

    This class models a real representation of the monster group
    with fixed-point arithmetic. Calculations are done by combining
    vectors modulo ``p = 7, 31, 127, 255`` with Chinese remaindering.
    This way we achieve about %.2f bit precision.

    The construction of a vector in this space and the computation
    with such vectors works in the same way as in class |MMSpace|.
    But there are som limitations:

      * Vectors may be constructed as in class |MMSpace|, but
        the arguments of the constructor may be tuples only.
        Here randomized scalars are illegal.

      * The only operations allowed for vectors are copying,
        multiplication with a group element, and testing for
        equality. Vector addition and scalar multiplication
        are illegal.

      * A vector may be reduced modulo one of the primes
        ``p = 7, 31, 127, 255`` using the modulo operator ``%%``.
        The result is a vector in the space ``MMSpace(p)``.

      * Changing entries of a vector via item assignment is
        illegal.

      * Entries and subarrays of a vector may be obtained as
        in class |MMSpace|. Here subarrays are returned as
        integers or numpy  arrays with ``dtype = np.int32``.
        On output, each number is multiplied with ``2**k``
        in order to obtain an integer. Here ``k`` is the
        argument given in the constructor by parameter ``shift``.

        You may multiply such an integer value with the attribute
        ``factor`` of the space for obtaining an exact floating
        point value. But floating point data may lead to imprecise
        results when processing the with a computer algebra system.

    :var shift:
        If shift is ``k`` then a vector ``v`` can be represented if
        all entries of ``v`` are integer multiples of ``2**(-k)``.
        The absolute value of an entry may be at most
        ``3513772 / 2**k``. In case ``shift == 20`` (default)
        this is about  ``3.35``.

    :var group:
        Contains the group operating on the vector
        space by right multiplication.
        That group must be an instance of class |MMGroup|.

    Caution!

        The norm of an input vector must not exceed
        ``1.234 * 10**13 * 4.0 ** (-k)``. Otherwise *overflow*
        occurs. In case ``k = 20`` (default) this quantity is about
        ``11.229``. Here ``k`` is given by parameter ``shift``.

        The norm of a vector is the sum of the squares of its
        entries, where for all entries with index ``("A", i0, i1)`` ,
        ``i0 != i1``, the doubled square of the entry must be
        taken instead.

    Caution!

        When multiplying a vector ``v`` with a generator of the group
        with tag ``t`` or ``l``, and ``v`` has an entry that is not
        an integer multiple of ``8 * 2**(-k)``, then a
        *precision error* may occur. We raise ``ValueError`` in
        case of a precision error.

    """ % PRECISION
    vector_type = MMVectorCRT
    _max_norm = (7*31*125*255)**2 // 4

    def __init__(self):
        """Create a 196884-dimensional representation of the monster

        All calculations are done modulo the odd number p
        """
        super(MMSpaceCRT, self).__init__()

    #######################################################################
    # Conversion to a list of tuples
    #######################################################################

    def _not_supported(self, *args, **kwds):
        err = "Method not supported in class MMSpaceCRT"
        raise NotImplementedError(err)

    as_tuples = _not_supported

    #######################################################################
    # Creating vectors
    #######################################################################

    def zero(self, shift):
        """Return the zero vector"""
        err = "Cannot create zero vector in space of class MMSpaceCRT"
        return MMVectorCRT(shift)

    def copy_vector(self, v1):
        assert v1.space == self
        v = MMVectorCRT(v1.shift, 0)
        for p in (7, 31, 127, 255):
            np.copyto(v.data[p].data, v1.data[p].data)
        if v1.expanded:
            np.copyto(v.data_int, v1.data_int)
            v._v2 = v1._v2
            v._inorm = v1._inorm
        v.expanded = v1.expanded
        return v

    def __call__(self, *args):
       return self.from_tuples(*args)

    def set_rand_uniform(self, *args, **kwds):
        err = "Cannot create random vector in space of class MMSpaceCRT"
        raise NotImplementedError(err)

    parse = _not_supported

    #######################################################################
    # Obtaining and setting components via sparse vectors
    #######################################################################

    def getitems_sparse(self, v, a_sparse):
        raise NotImplementedError(err)

    def additems_sparse(self, *args, **kwds):
        err = "Sparse representation not supported in class MMSpaceCRT"
        raise NotImplementedError(err)

    setitems_sparse = additems_sparse

    #######################################################################
    # Conversion from and to to sparse representation
    #######################################################################

    as_sparse = additems_sparse

    #######################################################################
    # Vector operations
    #######################################################################

    def iadd(self, v1, v2):
        err = "Vector addition not supported in class MMSpaceCRT"
        raise NotImplementedError(err)

    def imul_scalar(self, v1, a):
        err = "Scalar multiplication not supported in class MMSpaceCRT"
        raise NotImplementedError(err)

    #######################################################################
    # Group operation
    #######################################################################

    def _imul_word(self, v1, g_word, buf):
        if len(g_word):
            vectors = list(v1.data.values())
            if check_MMVectorCRT:
                if mm_crt_check_g(g_word[0], *[v.data for v in vectors]):
                    print("MMVectorCRT: shift = %d, v2 = %d, tag = %s" %
                        (v1.shift, v1.v2, ((g_word[0] >> 28) & 7)))
                    raise ValueError(ERR_UNDERFLOW_G)
            for v in vectors:
                mm_op_word(v.p, v.data, g_word, len(g_word), 1, buf)

    def imul_group_word(self, v1, g):
        """Return product v1 * g of vector v1 and group word g.

        v1 is replaced by v1 * g.

        This method is called for elements v1 of the space
        'self' and for elements g of the group 'self.group' only.
        """
        assert isinstance(g, AbstractGroupWord) and g.group.is_mmgroup
        a = g.mmdata
        nn = np.zeros(5, dtype = np.uint32)
        nnw = np.zeros(5, dtype = np.uint32)
        buf = np.zeros(mm_aux_mmv_size(255), dtype = np.uint64)
        while len(a):
            mm_group_n_clear(nn)
            i = mm_group_n_mul_word_scan(nn, a, len(a))
            length =  mm_group_n_to_word(nn, nnw)
            self._imul_word(v1, nnw[:length], buf)
            a = a[i:]
            if len(a):
                self._imul_word(v1, a[:1], buf)
                a = a[1:]
        del buf
        v1.expanded = False
        return v1

    vector_mul_exp = _not_supported

    #######################################################################
    # Checking equality
    #######################################################################

    def equal_vectors(self, v1, v2):
        """Return True iff vectors v1 and v2 are equal

        This method is called for elements v1 and v2 of the space
        'self' only.
        """
        v1.expand()
        v2.expand()
        v1_2, v2_2 = v1._v2, v2._v2
        if v1_2 == v2_2 == 24:
             return True   # then v1 = v1 = zero
        if v1_2 != v2_2:
             return False
        data1, data2 = v1.data_int, v2.data_int
        if v1.shift > v2.shift:
             data1 = data1 >> (v1.shift - v2.shift)
        if v2.shift > v1.shift:
             data2 = data2 >> (v2.shift - v1.shift)
        return (data1 == data2).all()

    #######################################################################
    # Conversion from and to byte format
    #######################################################################

    as_bytes =  _not_supported

    from_bytes = _not_supported

    #######################################################################
    #  Checking and reducing a vector
    #######################################################################

    def check(self, v1):
        return True

    def reduce(self, v1):
        return v1

    #######################################################################
    # getitem and setitem
    #######################################################################

    def vector_get_item(self, v, item):
        assert v.space == self
        if not isinstance(item, tuple):
            item = (item,)
        shape, a_sparse = sparse_from_indices(255, *item)
        d = {}
        for p in (7, 31, 127, 255):
            vdata = v.data[p]
            asp = a_sparse.copy()
            vdata.space.getitems_sparse(vdata, asp)
            d[p] = (asp & p).astype(np.uint8)
        l = len(d[7])
        a = np.zeros(l, dtype = np.int32)
        mm_crt_combine_bytes(d[7], d[31], d[127], d[255], l, a)
        af = np.array(a * v.factor,  dtype = float)
        return af.reshape(shape) if len(shape) else float(af)

    def vector_set_item(*args, **kwd):
        err = "Item assigment not supported in space of type MMSpaceCRT"
        raise NotImplementedError(err)

    #######################################################################
    # Formatting a vector
    #######################################################################

    def str_vector(self, v1):
        return "<vector in space of type MMSpaceCRT>"

    #######################################################################
    # Properties
    #######################################################################

    @property
    def factor(self):
        """Factor of type ``float`` for vector entries.

        When reading entries from a vector then integers or arrays
        of integers are returned. Here each entry must be multiplied
        with this ``factor`` to obtain an exact floating point value
        of that entry.
        """
        return self._factor

    @property
    def max_norm(self):
        """Maximum feasible norm of an input vector.

        The norm of a vector is the sum of the squares of its
        entries, where for all entries with index ``("A", i0, i1)`` ,
        ``i0 != i1``, the doubled square of the entry must be
        taken instead.
        """
        return (self._max_norm * self._factor)**2

StdMMSpaceCRT = MMSpaceCRT()
MMVectorCRT.space = StdMMSpaceCRT

def MMV_CRT(shift):
    return partial(MMVectorCRT, shift)


with open('input.txt', 'r') as file:
    inputs = file.read()

inputs = inputs.strip().split('\n')

groups_of_three = list(map(lambda *x: x, *([iter(inputs)] * 3)))

total = 0
for group in groups_of_three:
    intersect = list(set(group[0]) & set(group[1]) & set(group[2]))[0]
    total += ord(intersect.lower()) - 96 if intersect.islower() else (26 + ord(intersect.lower()) - 96)

print(total)


import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('datosgauss.txt')

minimo = data[:,0].min()
maximo = data[:,0].max()

i_x = data[data[:,0]==minimo, 1]
i_y = data[data[:,0]==minimo, 2]

f_x = data[data[:,0]==maximo, 1]
f_y = data[data[:,0]==maximo, 2]

plt.plot(i_x, i_y, label='inicial')
plt.plot(f_x, f_y, '--', label='final')
plt.legend()
plt.xlabel("x")
plt.ylabel("y")
plt.title('Grafica de Gauss')
plt.savefig('gauss.png')
plt.show()


import sys
input=sys.stdin.readline
a = int(input())
coin = [25,10,5,1]
for i in range(a):
    k = int(input())
    ex1= [ ]
    for j in range(4):
        ex1.append(int(k/coin[j]))
        k = k%coin[j]

    print(*ex1)

백준/Bronze/2720. 세탁소 사장 동혁/세탁소 사장 동혁.py

세탁소 사장 동혁.py


'''
ICG PROTOCOLS - single compartment cell
Written by William Podlaski and Christopher Currin. Last modified 19.08.2016
'''

import numpy as np
from nrnutils import Mechanism, Section, alias

diam_100um2 = np.sqrt(100/np.pi)

class ICGCell(object):
    def __init__(self, ion_type, current_type, L=diam_100um2, diam=diam_100um2, Ra=150, g_pas=0.00003334):
        self.ion_type = ion_type
        self.current_type = current_type
        leak = Mechanism('pas', e=-65, g=g_pas)
        self.soma = Section(L, diam, Ra=Ra, mechanisms=[leak])

    def insert_mechanism(self, mech):
        """
        Insert density mechanism into cell
        :param mech:
        """
        if isinstance(mech, basestring):
            mech = Mechanism(mech)
        mech.insert_into(self.soma)
        self.setRevVar()
        self.setConc()
    insert_distributed_channel = insert_mechanism

    def insert_synapse(self, suffix, **parameters):
        """

        :param suffix:
        :param parameters: passed as e=-70, g=0.0001
        :return:
        """
        self.soma.add_synapses(suffix.lower(), suffix, **parameters)
        return getattr(self.soma, suffix.lower())
    insert_single_channel = insert_synapse

    def setRevVar(self):
        self.eRev = {
            'kv': 'ek', 'nav': 'ena', 'cav': 'eca', 'kca': 'ek', 'ih': 'eh'
        }[self.ion_type]
        tmp_eRev = {
            'kv': -86.7, 'nav': 50.0, 'cav': 135.0, 'kca': -86.7, 'ih': -45.0
        }[self.ion_type]
        setattr(self.soma, self.eRev, tmp_eRev)

    def setConc(self):
        self.iConc = {
            'kv': 'ki', 'nav': 'nai', 'cav': 'cai', 'kca': 'ki', 'ih': None
        }[self.ion_type]
        self.oConc = {
            'kv': 'ko', 'nav': 'nao', 'cav': 'cao', 'kca': 'ko', 'ih': None
        }[self.ion_type]
        tmp_iConc = {
            'kv': 85.0, 'nav': 21.0, 'cav': 8.1929e-5, 'kca': 85.0, 'ih': None
        }[self.ion_type]
        tmp_oConc = {
            'kv': 3.3152396, 'nav': 136.3753955, 'cav': 2.0, 'kca': 3.3152396, 'ih': None
        }[self.ion_type]
        if self.ion_type != 'ih':
            setattr(self.soma, self.iConc, tmp_iConc)
            setattr(self.soma, self.iConc, tmp_oConc)


import numpy as np
from typing import Tuple
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont

from definitions import ROOT_DIR

# ==========================================================
#                        ROCKSAMPLE
# ==========================================================

def create_circular_mask(h: int, w: int, center: Tuple[int, int] = None, radius: int = None):
    if center is None: # use the middle of the image
        center = (int(w/2), int(h/2))
    if radius is None: # use the smallest distance between the center and image walls
        radius = min(center[0], center[1], w-center[0], h-center[1])

    Y, X = np.ogrid[:h, :w]
    dist_from_center = np.sqrt((X - center[0])**2 + (Y-center[1])**2)

    mask = dist_from_center <= radius
    return mask

def create_rocksample_agent(h: int, w: int, thickness: int = 3, radius: int = None):
    grid = create_circular_mask(h, w, radius=radius)
    grid_inner = create_circular_mask(h, w, radius=radius - thickness)
    return np.bitwise_xor(grid, grid_inner)

def generate_rock_agent_rgb(size: int, agent_color: np.ndarray) -> np.ndarray:
    agent_mask = create_rocksample_agent(size, size, radius=size // 3)
    rgb = np.repeat(agent_mask[..., np.newaxis], 3, axis=-1)
    rgb = np.ones_like(rgb) * 255
    rgb[agent_mask.astype(bool)] = agent_color
    return rgb

def create_rectangle(h: int, w: int, thickness: int = 2, length: int = None, width: int = None):
    grid = np.zeros((h, w))
    if length is None:
        length = h - 2
    if width is None:
        width = w - 2

    assert h > length and w > width
    h_space = (h - length) // 2
    w_space = (w - width) // 2

    # East/West
    w_range_pos = np.arange(thickness) + w_space
    w_range = np.concatenate([w_range_pos, -(w_range_pos + 1)])
    grid[h_space:-h_space, w_range] = 1

    # North/South
    h_range_pos = np.arange(thickness) + h_space
    h_range = np.concatenate([h_range_pos, -(h_range_pos + 1)])
    grid[h_range, w_space:-w_space] = 1
    return grid

def generate_rock_rgb(size: int, rock_color: np.ndarray, weight: float = None,
                      background_color: np.ndarray = None):
    if weight is not None and background_color is None:
        w_p = int(weight * 155) + 30 if weight > 0 else 0
        background_color = np.array([255 - w_p, 255 - w_p, 255])
    l = size - size // 4
    rock_mask = create_rectangle(size, size, thickness=5, length=l, width=l)
    rgb = np.repeat(rock_mask[..., np.newaxis], 3, axis=-1)
    if weight is not None:
        rgb[:, :] = background_color
    else:
        rgb = np.ones_like(rgb) * 255
    rgb[rock_mask.astype(bool)] = rock_color
    return rgb

def generate_label(h: int, w: int, str_label: str,
                   font_size: int = 12):
    """
    Generate an h x w x 3 RGB label with str_label in the centre printed.
    :param h: height
    :param w: width
    :param str_label: string to label
    :param color: color for font
    :param font_size: what size font do we use
    :return:
    """
    # Start with an all white label
    label = Image.new('RGB', (w, h), (255, 255, 255))
    d_actual = ImageDraw.Draw(label)
    # font = ImageFont.truetype("FreeMono.ttf", font_size)
    font_path = Path(ROOT_DIR, "scripts", "FreeMono.ttf")
    font = ImageFont.truetype(str(font_path), font_size)

    img_to_guide = Image.new('RGB', (w, h), (255, 255, 255))
    d = ImageDraw.Draw(img_to_guide)
    d.text((0, 0), str_label, (0, 0, 0), font=font)

    text_w, text_h = d.textsize(str_label, font)
    offset_x, offset_y = font.getoffset(str_label)
    text_w += offset_x
    text_h += offset_y

    pos = ((w - text_w) // 2, (h - text_h) // 2)

    d_actual.text(pos, str_label, fill=(0, 0, 0), font=font)
    return np.array(label)

def rocksample_arr_to_viz(arr: np.ndarray, scale: int = 10, grid_lines: bool = True,
                          background_weights: np.ndarray = None, greedy_actions: np.ndarray = None) -> np.ndarray:
    """
    Make a pixel representation of rock sample state/array
    :param arr: array representation of the environment
    :param scale: How large do we scale up?
    :param grid_lines: Do we show grid lines?
    :param background_weights: What are the weights for each grid?
    :param greedy_actions: Do we show optimal actions for each position?
    :return: array to plot
    """
    space_color = np.array([255, 255, 255], dtype=np.uint8)
    rock_color = np.array([255, 167, 0], dtype=np.uint8)
    goal_color = np.array([0, 150, 0])
    agent_color = np.array([0, 0, 0], dtype=np.uint8)
    grid_color = None

    size = arr.shape[0] * scale

    if grid_lines:
        size += arr.shape[0] + 1
        grid_color = np.array([150, 150, 150], dtype=np.uint8)

    final_viz_array = np.zeros((size, size, 3), dtype=np.uint8)

    if grid_lines:
        final_viz_array[::(scale + 1)] = grid_color
        final_viz_array[:, ::(scale + 1)] = grid_color

    for y, row in enumerate(arr):
        for x, val in enumerate(row):
            if val == 1:
                # AGENT
                to_fill = generate_rock_agent_rgb(scale, agent_color)
            elif val == 2:
                # ROCK
                to_fill = generate_rock_rgb(scale, rock_color, weight=background_weights[y, x] if background_weights is not None else None)
            elif val == 3:
                # AGENT + ROCK
                background = generate_rock_rgb(scale, rock_color, weight=background_weights[y, x] if background_weights is not None else None)
                agent = generate_rock_agent_rgb(scale, agent_color)
                background[(agent != 255).astype(bool)] = agent[(agent != 255).astype(bool)]
                to_fill = background
            elif val == 4:
                to_fill = np.zeros((scale, scale, 3))
                to_fill[:, :] = np.copy(goal_color)
            else:
                to_fill = np.zeros((scale, scale, 3))
                to_fill[:, :] = np.copy(space_color)

            if greedy_actions is not None and y < greedy_actions.shape[0] and x < greedy_actions.shape[1]:
                action_str = greedy_actions[y, x]
                label = generate_label(scale, scale, action_str)
                to_fill[(label != 255).astype(bool)] = label[(label != 255).astype(bool)]

            if grid_lines:
                final_viz_array[y * (scale + 1) + 1:(y + 1) * (scale + 1),
                x * (scale + 1) + 1:(x + 1) * (scale + 1)] = to_fill
            else:
                final_viz_array[y * scale:(y + 1) * scale,
                x * scale:(x + 1) * scale] = to_fill

    return final_viz_array

def generate_greedy_action_array(env, agent):
    """
    NOTE: for ROCKSAMPLE only.
    :return:
    """
    all_pos_states = env.sample_all_states()
    obses = []
    for state in all_pos_states:
        obses.append(env.get_obs(state))

    obses = np.stack(obses)
    qs = agent.Qs(obses, agent.network_params)
    actions = np.argmax(qs, axis=1)

    arr = np.zeros((env.size, env.size - 1), dtype=np.int16)
    for act, state in zip(actions, all_pos_states):
        pos = state[:2]
        arr[pos[0], pos[1]] = int(act)

    return arr


from flask import Flask, request, jsonify
from flask_cors import CORS

from constants import *
from subreddit_score import *

app = Flask(__name__)
CORS(app)

"""
Obtain data and transform into valid format for Plotly.js.

    Input:
        - subreddit:
        - days:
        - stat:

    Output:
        - JSON
"""
@app.route('/generate/<subreddit>/<days>', methods = ['GET'])
def generate(subreddit, days):

    print('Generating data and plot... [subreddit = %s] [days = %s]' % (subreddit, days))

    # Obtain raw data using pushshift.io
    data = generate_data(str(subreddit), int(days))

    # Transform data for plotting
    score_means = transform_data(subreddit, data, COLUMN_NAMES[3])
    score_means.reverse() # reverse data for day of the week

    num_comments_means = transform_data(subreddit, data, COLUMN_NAMES[1])
    num_comments_means.reverse() # reverse data for day of the week

    return jsonify(
        status = 'SUCCESS',
        scores = score_means,
        comments = num_comments_means,
        subreddit = subreddit,
        days = days)

    # return jsonify(
    #     status = 'ERROR',
    #     message = 'Incorrect value for [stat]. Must be one of [SCORE, COMMENT].',
    #     subreddit = subreddit,
    #     days = days
    # )

"""
Start server
"""
if __name__ == '__main__':
    app.run(threaded=True, host='0.0.0.0')


import streamlit as st
from PIL import Image
import dill
from src.pipeline.predict_pipeline import PredictPipeline
from src.pipeline.preprocess_pipeline import data_transform

def predict(testimg):
    try:
        predictor = PredictPipeline()
        preds = predictor.predict(testimg)
        pred_age = int(preds)
        st.success(f'Predicted age: {pred_age}')
        return pred_age
    except:
        st.title(' ')

# model = joblib.load('xgbpipe.joblib')
st.title("Upload the photo and then click 'Predict the age' ")

try:
    trigger = st.button('Predict the age', on_click=predict(file))
except:
    file = st.file_uploader(" ")
    trigger = st.button('Predict the age', on_click=predict(file))

try:
    image = Image.open(file)
    st.image(image, use_column_width=True)
except:
    st.title(' ')


import brainpy_datasets as bp_data
import matplotlib.pyplot as plt
import numpy as np

import brainpy as bp
import brainpy.math as bm

# data
ds = bp_data.cognitive.RatePerceptualDecisionMaking(
  dt=20.,
  t_fixation=lambda: np.random.choice((50, 100, 200, 400)),
  t_stimulus=lambda: np.random.choice((100, 200, 400, 800)),
  num_trial=64 * 100
)
loader = bp_data.cognitive.TaskLoader(ds,
                                      max_seq_len=100,
                                      batch_size=64,
                                      data_first_axis='T')

# EI RNN model
class EI_RNN(bp.DynamicalSystem):
  def __init__(
      self, num_input, num_hidden, num_output, dt,
      e_ratio=0.8, sigma_rec=0., seed=None,
      w_ir=bp.init.KaimingUniform(scale=1.),
      w_rr=bp.init.KaimingUniform(scale=1.),
      w_ro=bp.init.KaimingUniform(scale=1.)
  ):
    super().__init__()

    # parameters
    self.tau = 100
    self.num_input = num_input
    self.num_hidden = num_hidden
    self.num_output = num_output
    self.e_size = int(num_hidden * e_ratio)
    self.i_size = num_hidden - self.e_size
    self.alpha = dt / self.tau
    self.sigma_rec = (2 * self.alpha) ** 0.5 * sigma_rec  # Recurrent noise
    self.rng = bm.random.RandomState(seed=seed)

    # hidden mask
    mask = np.tile([1] * self.e_size + [-1] * self.i_size, (num_hidden, 1))
    np.fill_diagonal(mask, 0)
    self.mask = bm.asarray(mask, dtype=bm.float_)

    # input weight
    self.w_ir = bm.TrainVar(bp.init.parameter(w_ir, (num_input, num_hidden)))

    # recurrent weight
    bound = 1 / num_hidden ** 0.5
    self.w_rr = bm.TrainVar(bp.init.parameter(w_rr, (num_hidden, num_hidden)))
    self.w_rr[:, :self.e_size] /= (self.e_size / self.i_size)
    self.b_rr = bm.TrainVar(self.rng.uniform(-bound, bound, num_hidden))

    # readout weight
    bound = 1 / self.e_size ** 0.5
    self.w_ro = bm.TrainVar(bp.init.parameter(w_ro, (self.e_size, num_output)))
    self.b_ro = bm.TrainVar(self.rng.uniform(-bound, bound, num_output))

  def reset_state(self, batch_size):
    self.h = bm.Variable(bm.zeros((batch_size, self.num_hidden)), batch_axis=0)
    self.o = bm.Variable(bm.zeros((batch_size, self.num_output)), batch_axis=0)

  def cell(self, x, h):
    ins = x @ self.w_ir + h @ (bm.abs(self.w_rr) * self.mask) + self.b_rr
    state = h * (1 - self.alpha) + ins * self.alpha
    state += self.sigma_rec * self.rng.randn(self.num_hidden)
    return bm.relu(state)

  def readout(self, h):
    return h @ self.w_ro + self.b_ro

  def update(self, x):
    self.h.value = self.cell(x, self.h)
    self.o.value = self.readout(self.h[:, :self.e_size])
    return self.h.value, self.o.value

  def predict(self, xs):
    self.h[:] = 0.
    return bm.for_loop(self.update, xs)

  def loss(self, xs, ys):
    hs, os = self.predict(xs)
    l = bp.losses.cross_entropy_loss(os.reshape((-1, os.shape[-1])), ys.flatten())
    acc = bm.mean(bm.argmax(os, axis=-1) == ys)
    return l, acc

# Instantiate the network and print information
hidden_size = 50
net = EI_RNN(num_input=len(ds.input_features),
             num_hidden=hidden_size,
             num_output=len(ds.output_features),
             dt=ds.dt,
             sigma_rec=0.15)

# Adam optimizer
opt = bp.optim.Adam(lr=0.001, train_vars=net.train_vars().unique())

# gradient function
grad_f = bm.grad(net.loss,
                 grad_vars=net.train_vars().unique(),
                 return_value=True,
                 has_aux=True)

# training function
@bm.jit
def train(xs, ys):
  grads, loss, acc = grad_f(xs, ys)
  opt.update(grads)
  return loss, acc

# training
for epoch_i in range(30):
  losses = []
  accs = []
  for x, y in loader:
    net.reset_state(x.shape[1])
    l, a = train(x, y)
    losses.append(l)
    accs.append(a)
  print(f'Epoch {epoch_i}, loss {np.mean(losses)}, acc {np.mean(accs)}')

# testing
ds.t_fixation = 500.  # set the fixed time duration for fixation and stimulus
ds.t_stimulus = 500.
x, y = zip(*[ds[i] for i in range(50)])  # get 50 trials
x = np.asarray(x).transpose(1, 0, 2)
y = np.asarray(y).transpose(1, 0)
net.reset_state(x.shape[1])
rnn_activity, action_pred = net.predict(x)
rnn_activity = bm.as_numpy(rnn_activity)
choice = np.argmax(bm.as_numpy(action_pred[-1]), axis=1)
correct = choice == y[-1]
print('Average performance', np.mean(correct))

# plot activity
trial = 0
plt.figure(figsize=(8, 6))
_ = plt.plot(rnn_activity[:, trial, :net.e_size], color='blue', label='Excitatory')
_ = plt.plot(rnn_activity[:, trial, net.e_size:], color='red', label='Inhibitory')
plt.xlabel('Time step')
plt.ylabel('Activity')
plt.show()


#!/usr/bin/env python

"""Display a subsection of input, starting with a matching pattern.

With no input file, or when file is -, read standard input.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import click
import gzip
import bz2
import re
import signal
import sys

@click.command()
@click.argument('pattern')
@click.argument('filenames', nargs=-1,
                type=click.Path(exists=True, dir_okay=False))
@click.option('-F', '--fixed-string',
                    default=False, is_flag=True,
                    help='fixed string match (default is regex)')
@click.option('-i', '--ignore-case',
                    default=False, is_flag=True,
                    help='ignore case distinctions')
@click.option('-m', '--max-count',
                    default=1,
                    help='start after number of matches.')
@click.option('-n', '--line-number',
                    default=False, is_flag=True,
                    help='print line number with output lines.')
@click.option('-x', '--exclude-match',
                    default=False, is_flag=True,
                    help='exclude matching line.')
def main(pattern, filenames, fixed_string, ignore_case,
         exclude_match, line_number, max_count):
    """Display input starting with a line matching PATTERN."""
    global exit_code
    exit_code = 1
    is_match = pattern_matcher(pattern, fixed_string, ignore_case)
    lines = text_file_input(filenames)
    if line_number:
        lines = (':'.join((str(i), l)) for i, l in enumerate(lines, 1))
    selected_lines = fromthis(is_match, lines, exclude_match, max_count)
    for line in selected_lines:
        print(line)
    return exit_code

def fromthis(is_match, input_stream, exclude_match, max_count):
    """Display input starting with a matching pattern."""
    global exit_code
    for line in input_stream:
        if is_match(line, max_count):
            exit_code = 0
            if not exclude_match:
                yield line
            break
    for line in input_stream:
        yield line

def count_matches(match_function):
    """Return True only if the required number of matches is reached."""
    def counter(line, max_count, persistent={'count': 0}):
        result = match_function(line)
        if result:
            persistent['count'] += 1
        return True if persistent['count'] == max_count else False
    return counter

def pattern_matcher(pattern, fixed_string, ignore_case):
    """Return a custom function for pattern matching."""
    regex_flags = re.IGNORECASE if ignore_case else 0

    @count_matches
    def match_regex(line, regex=re.compile(pattern, regex_flags)):
        return bool(regex.search(line))

    @count_matches
    def match_fixed_string_ignore_case(line, pattern=pattern):
        return pattern.upper() in line.upper()

    @count_matches
    def match_fixed_string_match_case(line, pattern=pattern):
        return pattern in line

    if not fixed_string:
        match_function = match_regex
    elif ignore_case:
        match_function = match_fixed_string_ignore_case
    else:
        match_function = match_fixed_string_match_case
    return match_function

def text_file_input(filenames):
    """Generate input from files or standard input."""
    if filenames:
        file_handles = open_files(filenames)
    else:
        file_handles = (iter(sys.stdin.readline, ''), )
    for file_handle in file_handles:
        for line in file_handle:
            yield line.rstrip()

def open_files(filenames):
    """Return a filehandle for each provided filename."""
    for filename in filenames:
        file_open = file_opener(filename)
        with file_open(filename, 'rt') as filehandle:
            yield filehandle

def file_opener(filename):
    """Return a file open function based on file extension."""
    if filename.endswith('.gz'):
        return gzip.open
    if filename.endswith('.bz2'):
        return bz2.open
    else:
        return open

if __name__ == '__main__':
    signal.signal(signal.SIGPIPE, signal.SIG_DFL)  # IOError: Broken pipe
    signal.signal(signal.SIGINT, signal.SIG_DFL)  # KeyboardInterrupt: Ctrl-C
    sys.exit(main())


# -*- coding:utf-8 -*-
import hashlib
from scrapy.utils.python import to_bytes
import os, shutil

targetPath = 'target'
dirPath = 'content'
for parent, dirnames, filename in os.walk(dirPath):
    for name in filename:
        img = os.path.join(parent, name)
        images = hashlib.sha1(to_bytes(img)).hexdigest() + '.jpg'
        print(img, images)
        shutil.copy(img, targetPath + '/' + images)


from dcext.oanda.api import OandaAPI, CANDLESV3, TOKEN
from datetime import datetime, timedelta, timezone
import pandas as pd
from pymongo import MongoClient
from pymongo.errors import DuplicateKeyError
from itertools import product
import logging
import json

EXCHANGE = "oanda"

def get_dt(date, tz=None):
    if isinstance(date, int):
        return get_dt(str(date), tz)
    elif isinstance(date, str):
        return datetime.strptime(date.replace("-", ""), "%Y%m%d").replace(tzinfo=tz)
    elif isinstance(date, datetime):
        return date.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=tz)
    else:
        raise TypeError("Not supported type: %s" % type(date))

class API(OandaAPI):

    def bar(self, instrument, granularity, start, end):
        if isinstance(start, datetime):
            start = start.timestamp()
        if isinstance(end, datetime):
            end = end.timestamp()
        query = {
            "granularity": granularity,
            "from": start,
            "to": end
        }
        content = self.get(CANDLESV3, query, instrument=instrument)
        data = json.loads(content)

        result = []
        for bar in data["candles"]:
            result.append(self.generate(bar))
        return  result

    MAPPER = {"o": "open", "h": "high", "c": "close", "l": "low"}

    def generate(self, bar):
        doc = bar.copy()
        mid = doc.pop("mid")
        for o, t in self.MAPPER.items():
            doc[t] = float(mid[o])
        return doc

class MongodbStorage(object):

    INSTRUMENT = "_i"
    START = "_s"
    END = "_e"
    DATE = "_d"
    COUNT = "_c"
    FILL = "_f"
    MODIFY = "_m"

    def __init__(self, host=None, db="OANDA_M1", log="log.oanda", tz=0):
        self.client = MongoClient(host)
        self.db = self.client[db]
        ldb, lcol = log.split(".", 1)
        self.log = self.client[ldb][lcol]
        self.tz = timezone(timedelta(hours=tz))
        self.init_log_collection()

    def ensure_table(self, instrument):
        collection = self.get_collection(instrument)
        info = collection.index_information()
        unique = info.get("datetime_1", {}).get("unique", False)
        if not unique:
            logging.warning("ensure table | %s | index datetime not unique", instrument)
            self.drop_dups(collection)
            collection.create_index("datetime", unique=1, background=True)
            logging.warning("ensure table | %s | unique index datetime created", instrument)
        else:
            logging.warning("ensure table | %s | unique index datetime exists", instrument)
        if "date_1" not in info:
            collection.create_index("date", background=True)
            logging.warning("ensure table | %s | index date created", instrument)

    @staticmethod
    def drop_dups(collection):
        dts = set()
        cursor = collection.find(None, ["datetime"])
        _id = None
        while True:
            try:
                doc = next(cursor)
                _id = doc["_id"]
                dt = doc["datetime"]
            except StopIteration:
                break
            except KeyError:
                continue
            except:
                cursor = collection.find({"_id": {"$gte": _id}}, ["datetime"])
            else:
                if dt in dts:
                    collection.delete_one({"_id": _id})
                    logging.warning("drop dups | %s | %s" % (collection, dt))
                else:
                    dts.add(dt)

    def init_log_collection(self):
        self.log.create_index([
            (self.INSTRUMENT, 1),
            (self.DATE, 1)
        ], unique=True, background=True)

    def create(self, instrument, date):
        dt = get_dt(date, self.tz)
        filters = {
            self.INSTRUMENT: instrument,
            self.DATE: date,
        }
        doc = {
            self.START: dt,
            self.END: dt+timedelta(days=1),
            self.COUNT: 0,
            self.FILL: 0,
            self.MODIFY: datetime.now()
        }
        doc.update(filters)
        return self.log.update_one(filters, {"$setOnInsert": doc}, upsert=True).upserted_id

    def fill(self, instrument, date, count, fill):
        filters = {
            self.INSTRUMENT: instrument,
            self.DATE: date
        }
        doc = {self.COUNT: count, self.FILL: fill, self.MODIFY: datetime.now()}
        self.log.update_one(filters, {"$set": doc})

    def find(self, instruments=None, start=None, end=None, filled=False):
        filters = {}
        if instruments:
            filters[self.INSTRUMENT] = {"$in": instruments}
        if start:
            filters[self.DATE] = {"$gte": start}
        if end:
            filters.setdefault(self.DATE, {})["$lte"] = end
        if filled:
            filters[self.FILL] = {"$gte": 0}
        elif filled is not None:
            filters[self.FILL] = 0
        print(filters)
        cursor = self.log.find(filters, [self.INSTRUMENT, self.DATE, self.START, self.END])
        for doc in list(cursor):
            yield doc[self.INSTRUMENT], doc[self.DATE], doc[self.START].replace(tzinfo=self.tz), doc[self.END].replace(tzinfo=self.tz)

    def time(self, date):
        if isinstance(date, int):
            return self.time(str(date))
        elif isinstance(date, str):
            return datetime.strptime(date.replace("-", ""), "%Y%m%d").replace(tzinfo=self.tz)
        elif isinstance(datetime, date):
            return date.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=self.tz)

    def write(self, instrument, data):
        count = 0
        collection = self.get_collection(instrument)
        for bar in data:
            doc = self.vnpy_format(bar, instrument)
            count += self.append(collection, doc)
        return count

    def get_collection(self, instrumet):
        return self.db["%s:%s" % (instrumet, EXCHANGE)]

    @staticmethod
    def append(collection, bar):
        try:
            collection.insert_one(bar)
        except DuplicateKeyError:
            return 0
        else:
            return 1

    def vnpy_format(self, bar, symbol):
        bar.pop("complete", None)
        bar["symbol"] = symbol
        bar["exchange"] = EXCHANGE
        bar["vtSymbol"] = "%s:%s" % (symbol, EXCHANGE)
        dt = datetime.strptime(bar.pop("time").split(".")[0], "%Y-%m-%dT%H:%M:%S").replace(tzinfo=self.tz)
        bar["datetime"] = dt
        bar["date"] = dt.strftime("%Y%m%d")
        bar["time"] = dt.strftime("%H:%M:%S.000000")
        bar["rawData"] = None
        bar["openInterest"] = 0
        return bar

    def get_last_date(self):
        doc = self.log.find_one(sort=[(self.DATE, -1)])
        if doc:
            return doc[self.DATE]
        else:
            return None

class Framework(object):

    def __init__(self, api, storage, ltz=8):
        assert isinstance(api, API)
        assert isinstance(storage, MongodbStorage)
        self.api = api
        self.storage = storage
        self.ltz = timezone(timedelta(hours=ltz))

    def create(self, instruments, start, end):
        dates = pd.date_range(get_dt(start), get_dt(end)).map(lambda t: t.year*10000+t.month*100+t.day)
        for i, d in product(instruments, dates):
            r = self.storage.create(i, int(d))
            logging.warning("create log | %s | %s | %s", i, d, r)

    def ensure(self, instruments):
        for i in instruments:
            self.storage.ensure_table(i)

    def publish(self, instruments=None, start=None, end=None, filled=False, redo=3):
        logging.warning("publish cycle start| %s | %s | %s | %s | %s", instruments, start, end, filled, redo)
        now = datetime.now(self.ltz)
        missions = list(self.storage.find(instruments, start, end, filled))
        total = len(missions)
        accomplish = 0
        for i, d, s, e in missions:
            if e >= now:
                logging.warning("publish | %s | %s | end: %s is future", i, d, e)
                accomplish += 1
                continue
            accomplish += self.download(i, d, s, e)
        logging.warning("publish cycle done | total: %s | accomplished: %s", total, accomplish)
        if redo:
            if accomplish < total:
                self.publish(instruments, start, end, False, redo-1)

    def download(self, instrument, date, start, end):
        try:
            data = self.api.bar(instrument, "M1", start, end)
            count = len(data)
        except Exception as e:
            logging.error("req bar | %s | %s | %s", instrument, date, e)
            return 0

        try:
            if count:
                fill = self.storage.write(instrument, data)
            else:
                fill = -1
        except Exception as e:
            logging.error("write bar | %s | %s | %s", instrument, date, e)
            return 0

        try:
            self.storage.fill(instrument, date, count, fill)
        except Exception as e:
            logging.error("fill log | %s | %s | %s", instrument, date, e)
        else:
            logging.warning("download bar | %s | %s | fill: %s, count: %s", instrument, date, fill, count)
            return 1

    def last_date(self):
        return self.storage.get_last_date()

def init_from_config(filename):
    with open(filename) as f:
        conf = json.load(f)
    api = API(conf.get("token", None))
    storage = MongodbStorage(**conf.get("storage", {}))
    fw = Framework(api, storage, conf.get("ltz", 8))
    return fw

def run(command, instruments, start, end, filled=False, redo=3, config_file="oanda_m1.json"):
    fw = init_from_config(config_file)
    if command == "create":
        assert isinstance(instruments, list)
        assert isinstance(start, int)
        assert isinstance(end, int)
        fw.create(instruments, start, end)
    elif command == "publish":
        fw.publish((instruments, start, end, filled, redo))

import click

@click.command()
@click.option("-i", "--instruments", default="")
@click.option("-s", "--start", default=None, type=click.INT)
@click.option("-e", "--end", default=None, type=click.INT)
@click.option("-f", "--filled", default=False, is_flag=True)
@click.option("-r", "--redo", default=3, type=click.INT)
@click.option('-n', "--filename", default="oanda_m1.json", type=click.STRING)
@click.argument("command", nargs=-1)
def command(command, instruments, start, end, filled=False, redo=3, filename="oanda_m1.json"):
    with open(filename) as f:
        conf = json.load(f)
    api = API(conf.get("token", None))
    storage = MongodbStorage(**conf.get("storage", {}))
    fw = Framework(api, storage, conf.get("ltz", 8))
    if instruments:
        instruments = instruments.split(",")
    else:
        instruments = conf["instruments"]

    if len(command) == 0:
        command = ["publish"]
    for cmd in command:
        if cmd == "create":
            if not start:
                start = fw.last_date() or conf.get("start", None)
            if not end:
                end = conf.get("end", int(datetime.now().strftime("%Y%m%d")))
            assert isinstance(start, int)
            assert isinstance(end, int)
            fw.create(instruments, start, end)
        elif cmd == "publish":
            fw.publish(instruments, start, end, filled, redo)
        elif cmd == "ensure":
            fw.ensure(instruments)

if __name__ == '__main__':
    command()


print('1부터 9까지의 숫자를 입력하세요.')
i = 10
while i <13 :
	number = int(input('구구단 : '))
	if number<10 :
		for i in range(1,10) :
			print(number,'x',i, '=',number*i)
	elif number >= 10 :
		print('안됩니다. 돌아가세요. 1부터 9까지만 가르쳐줄거에요.다시 입력해요.')

		'''
print('2부터 9까지의 숫자를 입력하세요. 알파벳 a를 입력하시면 자동으로 프로그램이 종료됩니다.')

while True:
	number = int(input('구구단 : '))
	for i in range(1,10):
		print(number,'x',i, '=', number*i
'''


from __future__ import absolute_import
from django.db import models
from .utils import ugettext_lazy_compact as _
# from localflavor.in_ import models as india_models

class ClientIndustry(models.Model):
    name =models.CharField(
        max_length=50,
        blank=False,
        unique=True,
        null=False)

    slug= models.SlugField(
        max_length=50,
        blank=False,
        null=False,
        unique=True,
        db_index=True)

    def __unicode__(self):
        return self.name

class Client(models.Model):

    name =models.CharField(
        verbose_name='Client name',
        max_length=200,
        blank=False,
        unique=True,
        null=False
    )

    industry = models.ForeignKey(
        'ClientIndustry',
        blank=True,
        null=True,
        on_delete=models.SET_NULL,)

    mailing_street = models.CharField(
        max_length=100,
        blank=True,
        null=True,
        verbose_name=_('Mailing Street'))

    mailing_street2 = models.CharField(
        max_length=100,
        blank=True,
        null=True,
        verbose_name=_('Mailing Street 2'))

    mailing_city = models.CharField(
        max_length=100,
        blank=True,
        null=True,
        verbose_name=_('Mailing City'))

    # mailing_state = india_models.INStateField(
    #     blank=True,
    #     null=True,
    #     verbose_name=_('Mailing State'))

    mailing_zip = models.CharField(
        max_length=10,
        blank=True,
        null=True,)

    website = models.URLField(
        max_length=200,
        blank=True,
        null=True,)

    class Meta:
        verbose_name = _('client')
        verbose_name_plural = _('clients')

    def __unicode__(self):
        return self.name


class Solution:
    # @param A : integer
    # @return a list of integers
    def getRow(self, A):
        row=[[0,1,0]]
        for k in range(1,A+1):
            temp = []
            temp.append(0)
            for j in range(len(row[k-1])-1):
                temp.append(row[k-1][j]+row[k-1][j+1])
            temp.append(0)
            row.append(temp)
        return row[A][1:-1]


from src.gen_function import analiseDeProximidade
import argparse

def main():
    ap = argparse.ArgumentParser()

    ap.add_argument('-lista', '--listaDeStrings',
                    default=['abacate', 'pera', 'uva', 'banana', 'maçã','repolho', 'uva', 'feijão', 'arroz']
                    ,
                    help='Lista de strings')

    args = vars(ap.parse_args())

    listaDeStrings = args['listaDeStrings']

    '''
    chamada de função que faz a analise de proximidade
    '''
    analise = analiseDeProximidade(listaDeStrings,'uva',2)

    print(analise)

if __name__ == '__main__':
    main()


# Andrew Janedy Final Project

# Main menu displaying the three functions of this program
def menu():
    print()
    print("Please select from the following menu:")
    print()
    print("1: Calculate Compound Interest")
    print("2: Reverse List")
    print("3: Draw Some Triangles")
    print()
    selection = input("Please select the number of the function you would like to perform: ")

    return selection

# Recursive interest calculator menu / called upon original selection
# Takes 3 inputs as the function called from here has 3 parameters
def interest_calculator_menu():

    print()
    amount = input("How much is the initial investment?  Use only numbers and decimals: ")
    print("What is the interest rate?  Use only numbers and decimals.")
    print("Example, for a 5% interest rate, type .05 or 0.05.")
    interest_rate = input("For a 50% interest rate, type .5 or 0.5: ")
    years = input("How many years will this be invested?  Use only whole numbers: ")

    return recursive_interest_calculator(amount, interest_rate, years)

# Recursive interest calculator / takes 3 parameters
# Calculates interest and continues upon itself until years remaining = 0
def recursive_interest_calculator(amount, interest_rate, years):

    principle = float(amount)
    rate_of_interest = float(interest_rate)
    years_to_calculate = int(years)
    new_principle = principle
    new_principle += principle * rate_of_interest

    if years == 0:
        return amount
    else:
        return recursive_interest_calculator(new_principle, rate_of_interest, years_to_calculate - 1)

# Reverses the order of a list created from a .txt file using recursion
def reverse_list(list):
    if list == []:
        return None
    else:
        print(list[-1])
        reverse_list(list[:-1])

# Recursive triangle artist menu, asks user how big to draw the triangle
# Calls draw_triangle function with size parameter
def draw_triangles_menu():
    print()
    print("How big would you like the triangle to be?")
    triangle_size = input("Please enter a whole number: ")
    print()
    draw_triangles(triangle_size)

# Recursive triangle artist function
# Parameter is the size of the desired triangle
# Calls upon itself until triangle size = 0
def draw_triangles(triangle_size):
    if int(triangle_size) == 0:
        return None
    else:
        print("#" * int(triangle_size))
        return draw_triangles(int(triangle_size) - 1)

# Main function, calls menu which then returns the selection
# Basic check statements determines desired function
def main():
    selection = menu()

    if int(selection) == 1:
        result = interest_calculator_menu()
        new_result = round(result, 2)
        print(new_result)

    if int(selection) == 2:
        data_list = []
        data = open("Numbers.txt", 'r')
        data_lines = data.readlines()
        for line in data_lines:
            data_list.append(line)
        reverse_list(data_list)

    if int(selection) == 3:
        draw_triangles_menu()

main()


import gc
import glob
import re

import torch.optim
import torch.optim as optim
import torch.nn.functional as F
from torch.optim.lr_scheduler import CosineAnnealingWarmRestarts, CosineAnnealingLR, OneCycleLR

import xgboost as xgb
import optuna

from loader import *
from model import *
from utilities import *

class Learner(object):

	def __init__(
			self,
			data_root=None, model_root=None,
			model_type=PawSwinTransformerLarge4Patch12Win22k384, patience=3, pretrained=True, fine_tune=False,
			img_size=384,
			n_folds=10,
			batch_size=1,
			epochs=30,
			embed_size=128,
			hidden_size=64,
			lr=1e-5,
			max_lr=1e-3,
			min_lr=1e-7,
			weight_decay=1e-6,

	):
		if data_root is None:
			os.makedirs('data', exist_ok=True)
			self.data_root = 'data'
		else:
			self.data_root = data_root

		if model_root is None:
			os.makedirs('models', exist_ok=True)
			self.model_root = 'models'
		else:
			self.model_root = model_root

		self.activation = {}  # to store intermediate layers' activations if needed

		self.model_type = model_type
		self.patience = patience
		self.pretrained = pretrained
		self.fine_tune = fine_tune
		self.device = get_default_device()

		self.img_size = img_size
		self.n_folds = n_folds
		self.batch_size = batch_size
		self.epochs = epochs
		self.embed_size = embed_size
		self.hidden_size = hidden_size
		self.lr = lr
		self.max_lr = max_lr
		self.min_lr = min_lr
		self.weight_decay = weight_decay

	def _assert_model_type_is_supported(self):
		assert issubclass(self.model_type, PawVisionTransformerTiny16Patch384), \
			f'{self.model_type.__name__} is not supported'

	def train_one_epoch(self, train_loader, model, loss_func, optimizer, epoch, scheduler=None):
		metric_monitor = MetricMonitor()
		model.train()
		stream = tqdm(train_loader)

		with torch.enable_grad():

			for i, (images, dense, target) in enumerate(stream, start=1):
				images = images.to(self.device, non_blocking=True)
				dense = dense.to(self.device, non_blocking=True)
				target = target.to(self.device, non_blocking=True).float().view(-1, 1)

				# output = model(images, dense)
				output = model(images)  # TODO: should control whether the model with image only or not

				loss = loss_func(output, target)

				metric_monitor.update('Loss', loss.item())
				metric_monitor.update('RMSE', rmse_from_classifier_output(output, target))
				loss.backward()
				optimizer.step()

				if scheduler is not None:
					scheduler.step()

				optimizer.zero_grad()
				stream.set_description(f"Epoch: {epoch:02}. Train. {metric_monitor}")

		return

	def validate(self, val_loader, model, loss_func, epoch):
		metric_monitor = MetricMonitor()
		# WKNOTE: is a kind of switch for some specific layers/parts of the model that behave differently during
		#   training and inference (evaluating) time, e.g. Dropouts Layers, BatchNorm Layers, etc.
		model.eval()
		stream = tqdm(val_loader)
		final_targets = []
		final_outputs = []

		with torch.no_grad():
			for i, (images, dense, target) in enumerate(stream, start=1):
				images = images.to(self.device, non_blocking=True)
				dense = dense.to(self.device, non_blocking=True)
				target = target.to(self.device, non_blocking=True).float().view(-1, 1)
				# output = model(images, dense)
				output = model(images)  # TODO: should control whether the model with image only or not
				loss = loss_func(output, target)
				metric_monitor.update('Loss', loss.item())
				metric_monitor.update('RMSE', rmse_from_classifier_output(output, target))
				stream.set_description(f"Epoch: {epoch:02}. Valid. {metric_monitor}")

				targets = (target.detach().cpu().numpy() * 100).tolist()
				# WKNOTE: because we are using class for reg
				outputs = (torch.sigmoid(output).detach().cpu().numpy() * 100).tolist()

				final_targets.extend(targets)
				final_outputs.extend(outputs)
		return final_outputs, final_targets

	def get_activate_for_model_hook(self, name):
		def hook(model, input, output):
			self.activation[name] = output.detach().cpu().numpy()
		return hook

	def extract_intermediate_outputs_and_targets(self, model, data_loader):
		device = get_default_device()
		new_x = None
		y = None
		preds = None

		model.eval()

		with torch.no_grad():
			for (images, dense, target) in tqdm(data_loader, desc=f'Training with XGB. '):
				images = images.to(device, non_blocking=True)
				dense = dense.to(device, non_blocking=True)
				# batch_preds = torch.sigmoid(model(images, dense)).detach().cpu().numpy() * 100
				batch_preds = torch.sigmoid(model(images)).detach().cpu().numpy() * 100
				batch_embed = self.activation['swin_head']
				batch_x = np.concatenate([batch_embed, dense.detach().cpu().numpy()], axis=1)
				if preds is None:
					preds = batch_preds
					new_x = batch_x
					y = target.view(-1, 1).detach().cpu().numpy()
				else:
					preds = np.vstack((preds, batch_preds))
					new_x = np.vstack((new_x, batch_x))
					y = np.vstack((y, target.view(-1, 1).detach().cpu().numpy()))

		return new_x, y * 100, preds

	def perform_training(self, resume=False):
		preprocessor = PawPreprocessor(
			root_dir=data_root, train=True, n_folds=self.n_folds, model_dir=model_root, image_size=self.img_size)
		for fold in range(self.n_folds):

			train_loader = preprocessor.get_dataloader(
				fold=fold, for_validation=False, transform=get_albumentation_transform_for_training(self.img_size),
				batch_size=self.batch_size
			)
			val_loader = preprocessor.get_dataloader(
				fold=fold, for_validation=True, transform=get_albumentation_transform_for_validation(self.img_size),
				batch_size=self.batch_size
			)

			# model = PawClassifier(img_size, img_size, 3, len(preprocessor.features), embed_size, hidden_size)
			model = self.model_type(
				3, len(preprocessor.features), self.embed_size, self.hidden_size,
				pretrained=self.pretrained, fine_tune=self.fine_tune)

			epoch_start = 1

			# Training and Validation Loop
			best_rmse = np.inf
			best_epoch = np.inf
			best_model_path = None
			if resume:
				model_paths = glob.glob(model_root + os.path.sep + f'{str(model)}_*.pth.tar')
				model_paths = [p for p in model_paths if f'_fold{fold + 1}' in p]
				if len(model_paths) != 0:
					model_path = model_paths[0]
					model.load_state_dict(torch.load(model_path))  # always load the 0-th
					epoch_start = self._get_epoch_number_from_model_name(model_path)
					print(f'resume training from epoch {epoch_start} for fold {fold + 1}')
					best_model_path = model_path
					best_epoch = epoch_start
					best_rmse = self._get_rmse_from_model_name(model_path)

			model.to(self.device)
			loss_func = nn.BCEWithLogitsLoss()
			optimizer = torch.optim.AdamW(model.parameters(), lr=self.lr, weight_decay=self.weight_decay)
			scheduler = OneCycleLR(
				optimizer,
				max_lr=3e-4,
				steps_per_epoch=len(train_loader),
				epochs=self.epochs,
			)
			# scheduler = CosineAnnealingWarmRestarts(
			# 	optimizer,
			# 	T_0=100,
			# 	eta_min=self.min_lr,
			# 	last_epoch=-1
			# )

			epochs_with_no_improvement = 0
			fine_tune_with_no_augmentation = False

			for epoch in range(epoch_start, self.epochs + 1):

				if epochs_with_no_improvement >= self.patience:
					if fine_tune_with_no_augmentation:
						print(f'No improvement with no augmentation for {self.patience} epochs, early stop')
						break
					else:
						print(f'No improvement with AUGMENTATION for {self.patience} epochs, switch to NON-AUG training')
						epochs_with_no_improvement = 0
						fine_tune_with_no_augmentation = True
						train_loader = preprocessor.get_dataloader(
							fold=fold, for_validation=False,
							transform=get_albumentation_transform_for_validation(self.img_size),
							batch_size=self.batch_size)

				self.train_one_epoch(train_loader, model, loss_func, optimizer, epoch, scheduler)

				predictions, valid_targets = self.validate(val_loader, model, loss_func, epoch)
				rmse = round(mean_squared_error(valid_targets, predictions, squared=False), 5)
				if rmse <= best_rmse:
					epochs_with_no_improvement = 0
					best_rmse = rmse
					best_epoch = epoch
					if best_model_path is not None:
						os.remove(best_model_path)
					best_model_path = os.path.join(
						model_root,
						f"{str(model)}_fold{fold + 1}_epoch{epoch}_{rmse}-rmse.pth.tar")
					torch.save(model.state_dict(), best_model_path)
				else:
					epochs_with_no_improvement += 1

			print(f'The best RMSE: {best_rmse} for fold {fold + 1} was achieved on epoch: {best_epoch}.')
			print(f'The Best saved model is: {best_model_path}')
			print(''.join(['#'] * 50))
			del model
			gc.collect()
			torch.cuda.empty_cache()
		return

	@staticmethod
	def _get_fold_index_from_model_name(model_path) -> int:
		fold_info = [f for f in model_path.split('_') if f.startswith('fold')][0]
		pattern = re.compile(r'\d+')
		result = pattern.search(fold_info)
		fold = int(result.group()) - 1
		return fold

	@staticmethod
	def _get_epoch_number_from_model_name(model_path) -> int:
		fold_info = [f for f in model_path.split('_') if f.startswith('epoch')][0]
		pattern = re.compile(r'\d+')
		result = pattern.search(fold_info)
		epoch = int(result.group())
		return epoch

	@staticmethod
	def _get_rmse_from_model_name(model_path) -> float:
		basename = os.path.basename(model_path)
		rmse_txt = basename.split('-rmse')[0].split('_')[-1]
		rmse = float(rmse_txt)
		return rmse

	def train_and_fine_tune_xgb_model(self):
		seed_everything()
		device = get_default_device()
		preprocessor = PawPreprocessor(
			root_dir=self.data_root, train=True, n_folds=self.n_folds, model_dir=self.model_root,
			image_size=self.img_size)
		test_preprocessor = PawPreprocessor(root_dir=self.data_root, train=False, image_size=self.img_size)

		preds = None
		model = self.model_type(
			3, len(preprocessor.features), self.embed_size, self.hidden_size,
			pretrained=self.pretrained, fine_tune=self.fine_tune)
		all_models_checkpoints = glob.glob(model_root + os.path.sep + f'{str(model)}_*.pth.tar')

		for model_path in all_models_checkpoints:

			fold = self._get_fold_index_from_model_name(model_path)

			train_loader = preprocessor.get_dataloader(
				fold=fold, for_validation=False,
				transform=get_albumentation_transform_for_training(self.img_size), batch_size=self.batch_size)
			val_loader = preprocessor.get_dataloader(
				fold=fold, for_validation=True,
				transform=get_albumentation_transform_for_validation(self.img_size), batch_size=self.batch_size)

			model = self.model_type(
				3, len(preprocessor.features), self.embed_size, self.hidden_size, pretrained=self.pretrained,
				fine_tune=self.fine_tune)
			# WKNOTE: get activation from an intermediate layer
			model.model.head.register_forward_hook(self.get_activate_for_model_hook('swin_head'))
			model.load_state_dict(torch.load(model_path))
			model = model.to(device)

			xgb_train_x, xgb_train_y, train_preds = self.extract_intermediate_outputs_and_targets(model, train_loader)
			xgb_val_x, xgb_val_y, val_preds = self.extract_intermediate_outputs_and_targets(model, val_loader)

			def loss_func(trial: optuna.trial.Trial):
				params = {
					'n_estimators': trial.suggest_int('n_estimators', 10, 1000),  # default = 100
					'booster': trial.suggest_categorical('booster', ['gbtree', 'gblinear', 'dart']),
					# default = 'gbtree'
					'gamma': trial.suggest_uniform('gamma', 0, 100),  # default = 0
					'max_depth': trial.suggest_int('max_depth', 1, 11),
					# default = 6, the deeper, the easier to overfit
					'learning_rate': trial.suggest_uniform('learning_rate', 0, 1),  # default = 0.3
					'min_child_weight': trial.suggest_uniform('min_child_weight', 0.1, 100),  # default = 1
					'max_delta_step': trial.suggest_int('max_delta_step', 0, 11),  # default = 0
					'reg_lambda': trial.suggest_uniform('reg_lambda', 0, 1),
					'reg_alpha': trial.suggest_uniform('reg_alpha', 0, 1),
				}

				xgb_model = xgb.XGBRegressor(random_state=RANDOM_SEED, **params)
				xgb_model.fit(xgb_train_x, xgb_train_y)
				xgb_val_preds = xgb_model.predict(xgb_val_x)
				xgb_val_preds = prediction_validity_check(xgb_val_preds)

				rmse_val = round(mean_squared_error(xgb_val_y, xgb_val_preds, squared=False), 5)

				return rmse_val

			model_name = os.path.basename(model_path)

			study_db_path = os.path.join(self.model_root, f'{model_name}.db')
			study = optuna.create_study(
				direction='minimize', study_name=model_name,
				storage=f'sqlite:///{study_db_path}', load_if_exists=True)
			study.optimize(loss_func, n_trials=200)
			best_params = study.best_params
			print(f'the best model params are found on Trial #{study.best_trial.number}')
			print(best_params)

			xgb_model = xgb.XGBRegressor(random_state=RANDOM_SEED, **best_params)
			xgb_model.fit(xgb_train_x, xgb_train_y)
			xgb_train_preds = xgb_model.predict(xgb_train_x)
			xgb_val_preds = xgb_model.predict(xgb_val_x)

			rmse_train = round(mean_squared_error(xgb_train_y, xgb_train_preds, squared=False), 5)
			rmse_val = round(mean_squared_error(xgb_val_y, xgb_val_preds, squared=False), 5)

			print(f'train rmse: {rmse_train}, val rmse: {rmse_val}')

			model_name = os.path.basename(model_path)
			model_path = os.path.join(
				model_root, f"XGB-{rmse_val:.5f}_{model_name}.json")
			xgb_model.save_model(model_path)

			test_loader = test_preprocessor.get_dataloader(batch_size=self.batch_size)

			xgb_test_x, xgb_test_y, test_preds = self.extract_intermediate_outputs_and_targets(model, test_loader)
			xgb_test_preds = xgb_model.predict(xgb_test_x)
			xgb_test_preds = prediction_validity_check(xgb_test_preds)

			if preds is None:
				preds = xgb_test_preds
			else:
				preds += xgb_test_preds

		preds /= (len(all_models_checkpoints))

		return preds

if __name__ == '__main__':
	learning_params = dict(
		img_size=224,
		n_folds=10,
		batch_size=4,
		patience=3,
		model_type=PawSwinTransformerLarge4Patch7Win22k224,
		pretrained=True,
		fine_tune=True,
		epochs=20,
		embed_size=128,
		hidden_size=64,
		lr=1e-5,
		max_lr=3e-2,
		min_lr=1e-7,
		weight_decay=1e-6,
	)

	learner = Learner(
		data_root=data_root, model_root=model_root, **learning_params
	)
	learner.perform_training(resume=True)
	learner.train_and_fine_tune_xgb_model()


#!/usr/bin/python3
'__autor__'=='__Ivanoel__'

print('## Calculo para energia elétrica ##\n')

consumo = float(input("Informe a quantidade de KWH consumido?\n "))
print("Que tipo de Instalação  Elétrica é?\nR para Residencial; I para Industrial; C para Comercios?")
opcao = str(input("Qual foi sua opção? \n"))

if( opcao == 'R' and consumo < 500):
    paga = 0.40
  #  print('Você pagará de energia residencial? ',(paga * consumo))
else:
    paga = 0.65
  #  print('Você pagará de energia rsidencial? ',(paga * consumo ))

    if( opcao == 'I' and consumo < 1000):
        paga = 0.55
    #    print('Você pagará de energia Industrial? ',(paga * consumo))
    else:
        paga = 0.65
     #   print('Você pagará de energia Idustrial? ',(paga * consumo))

        if( opcao == 'C' and consumo < 5000):

            paga = 0.55

     #       print('Você pagará de energia Comercial? ',(paga * consumo))
        else:
            paga = 0.60
      #      print('Você pagará de energia Comercial? ',(paga * consumo))

print('Você pagará no consumo de energia R$%6.2f' %(paga*consumo))


import os
import pandas as pd
import numpy as np
import pickle
from imblearn.over_sampling import SMOTE
from sklearn.preprocessing import LabelEncoder
from collections import Counter
import typing as typing
from sklearn.impute import KNNImputer
from thyroid.utils.exception import customException
from thyroid.logging import logger
from thyroid.entity.config_entity import DataTransformationConfig
from thyroid.components.clustering import DataClustering
from pathlib import Path
from thyroid.config.configuration import ConfigurationManager

class DataTransformation:
    def __init__(self, config: DataTransformationConfig):
        self.config = config

    def convert_to_numerics(self,df):
        df[self.config.numerical_columns] = df[self.config.numerical_columns].apply(pd.to_numeric, errors='coerce')
        return(df)

    def case_normalization(self):
        """
        Method: this method will normalize all values to small case, will convert 'y' to 't' and 'n' to 'f'
        , it will replace '?' with nan and will convert columns to numerics
        Input: dataframe
        Outputs: normalized dataframe
        """

        file_path = self.config.merged_file
        df = pd.read_csv(os.path.join(self.config.validation_dir,file_path))

        # Convert all object columns to lowercase
        df = df.applymap(lambda x: x.lower() if isinstance(x, str) else x)

        # Replace '?' with NaN
        df.replace('?', np.nan, inplace=True)

        # Replace 'y' with 't' and 'n' with 'f'
        df.replace({'y': 't', 'n': 'f'}, inplace=True)

        for column in self.config.categorical_columns:
            df[column].replace({'0': 'f', '1': 't'}, inplace=True)

        # convert "age","TSH","T3","TT4","T4U","FTI" to numeric
        df = self.convert_to_numerics(df)

        return(df)

    def impute_column(self,column):
        imputer = KNNImputer(n_neighbors=3, weights="uniform", missing_values=np.nan)
        imputed_values = imputer.fit_transform(column.values.reshape(-1, 1))

        return np.round(imputed_values, 2)

    def handle_missing_values(self, df):
        """
        Method: This method will eliminate columns with more than 30% missing values, handle missing values and drop unnecessary columns
        Inputs: data frame with missing values
        Output: data frame with no missing values and useless columns
        """
        # Drop columns with more missing values than the threshold
        logger.info("Drop columns with more missing values than the threshold = {self.config.threshold}")
        column_threshold = int(self.config.threshold * len(df))
        df = df.dropna(axis=1, thresh=column_threshold)

        # Impute missing values
        logger.info("Impute missing values in numerical column")
        columns_to_impute = self.config.numerical_columns[1:]
        for column in columns_to_impute:
            df[column] = self.impute_column(df[column])

        # Mode Impute missing values for remaining categorical columns
        logger.info("Impute other columns missing values using mode")
        mode_columns = ['hypopituitary', 'I131_treatment', 'psych','sex']
        for column in mode_columns:
            mode_value = df[column].mode()[0]  # Get the mode value of the column
            df[column].fillna(mode_value, inplace=True)

        # median_imputation for age column
        logger.info("Impute age column using median")
        median_value = df['age'].median()  # Calculate the median value of the age column
        df['age'].fillna(median_value, inplace=True)

        # drop the measured columns
        df.drop(columns=self.config.drop_columns, inplace=True)

        return (df)

    def calculate_age_iqr(self,df):
        """_summary_

        Args:
            df (_type_): _description_

        Returns:
            _type_: _description_
        """

        logger.info("removing outliers from age column")
        age = self.config.numerical_columns[0]
        # Calculate IQR for the 'age' column
        Q1 = df[age].quantile(0.25)
        Q3 = df[age].quantile(0.75)
        IQR = Q3 - Q1

        # Define the upper and lower bounds for outlier removal
        lower_bound = Q1 - 1.5 * IQR
        upper_bound = Q3 + 1.5 * IQR

        # Remove outliers from the 'age' column
        df = df[(df['age'] >= lower_bound) & (df['age'] <= upper_bound)]

        return (df)

    def calculate_z_score(self,df):
        # Define a threshold for identifying outliers (e.g., Z-score > 2)
        logger.info("removing outliers using z-score")
        # Iterate over the columns and remove outliers
        non_outliers_data_frame = df.copy()  # Create a copy to store non-outliers
        for column in self.config.numerical_columns[1:]:
            z_scores = np.abs((non_outliers_data_frame[column] - non_outliers_data_frame[column].mean()) / non_outliers_data_frame[column].std())
            non_outliers_data_frame = non_outliers_data_frame[(z_scores <= self.config.z_threshold)]

        return non_outliers_data_frame

    def no_duplicates_and_binary_convert(self, df):
        # map sex column values to 0 and 1
        logger.info("converting sex column -> 'f' to 0 and 'm' to 1")
        sex = self.config.categorical_columns_to_convert[0]
        df[sex] = df[sex].map({'f': 0, 'm': 1})

        # map all othere categorical function to 0 and 1
        logger.info("mapping 'f' to 0 and 't' to 1")
        for column in self.config.categorical_columns_to_convert[1:]:
            if len(df[column].unique()) == 2:
                df[column] = df[column].map({'f': 0, 't': 1})

        # remove duplicate rows
        df = df.drop_duplicates()
        logger.info("duplicate rows removed")

        return(df)

    def outlier_removal(self,df):
        """_summary_
        Method:
            remove outliers from numerical columns
        Args:
            df (_type_): _description_
        """
        logger.info("Outliers removal started")
        # compute IQR on age column to remove outliers
        df = self.calculate_age_iqr(df)
        logger.info("IQR on age column completed")
        # compute Z-score on remaining numerical columns to remove outliers
        df = self.calculate_z_score(df)
        logger.info("Z-score on remaining numerical columns")
        df = self.no_duplicates_and_binary_convert(df)
        logger.info("remvoed duplicates and converted to binary")

        return df

    def imbalance_handling(self, df):
        """_summary_

        Args:
            df (_type_): imbalanced data frame

        Returns:
            _type_: dataframe with balanced data points
        """
        X = df.drop(columns=['class'])
        y = df['class']

        prev_count = Counter(y)
        logger.info("Class distribution before SMOTE: {prev_count}")

        smote = SMOTE(sampling_strategy='auto', random_state=42)

        X_resampled, y_resampled = smote.fit_resample(X, y)

        resampled_count = Counter(y_resampled)
        logger.info("Class distribution after SMOTE: {resampled_count})} ")

        resampled_data = pd.concat([X_resampled, y_resampled], axis=1)

        return resampled_data

    def labelencoding_and_save(self, df):
        encode = LabelEncoder().fit(df['class'])

        df['class'] = encode.transform(df['class'])

        # we will save the encoder as pickle to use when we do the prediction. We will need to decode the predcited values
        # back to original
        with open(os.path.join(self.config.encoding_dir, self.config.encoder_file), 'wb') as file:
            pickle.dump(encode, file)
        logger.info("label encoding successfull")

        # save the data
        df.to_csv(os.path.join(self.config.data_dir, self.config.data_file), index=False)
        logger.info(f"file saved as {self.config.data_file}")

        return df

    def get_clustered_data(self,df):
        config = ConfigurationManager()
        data_clustering_config = config.get_data_clustering_config()
        data_clustering = DataClustering(df,config=data_clustering_config)
        optimal_clusters = data_clustering.plot_knee()
        data_clustering.create_clusters(optimal_clusters)