from typing import Any, List

from fastapi import APIRouter, HTTPException, Depends

from apps.auth.model import User
from apps.bank.cruds import invoice
from apps.bank.schemas.invoice import InvoiceUpdate, InvoiceView, InvoiceCreate, InvoiceViewFull
from core.security import current_user_is_banker, get_current_user

router = APIRouter(prefix='/invoices', tags=['Invoices'])

@router.get('/', response_model=List[InvoiceViewFull],
            dependencies=[Depends(current_user_is_banker)])
async def list_invoices(skip: int = 0, limit: int = 100) -> Any:
    results = await invoice.get_list(skip=skip, limit=limit)
    return results

@router.get('/my', response_model=List[InvoiceViewFull])
async def list_my_invoices(user: User = Depends(get_current_user),
                           skip: int = 0, limit: int = 100) -> Any:
    results = await invoice.get_list(user=user, skip=skip, limit=limit)
    return results

@router.get('/{obj_id}', response_model=InvoiceViewFull)
async def get_invoice(obj_id: int, user: User = Depends(get_current_user)) -> Any:
    result = await invoice.get(id=obj_id, user=user)
    if not result:
        raise HTTPException(status_code=404, detail='Invoice not found!')
    return result

@router.post('/create', response_model=InvoiceView, status_code=201)
async def create_invoice(item: InvoiceCreate, user: User = Depends(get_current_user)) -> Any:
    result = await invoice.create_invoice(obj_in=item, user=user)
    return result

@router.put('/{obj_id}', response_model=InvoiceView)
async def update_invoice(obj_id: int, item: InvoiceUpdate,
                         user: User = Depends(get_current_user)) -> Any:
    obj_db = await invoice.get(id=obj_id, user=user)
    if not obj_db:
        raise HTTPException(status_code=404, detail='Invoice not found!')
    result = await invoice.update_invoice(obj_db=obj_db, obj_in=item, user=user)
    return result

@router.delete('/{obj_id}')
async def delete_invoice(obj_id: int, user: User = Depends(get_current_user)) -> Any:
    result = await invoice.remove(id=obj_id, user=user)
    return result


import pandas as pd
# import requests
import sys
import collections

# import urllib.request
import json

# url = 'http://loterias.caixa.gov.br/wps/portal/loterias/landing/lotofacil/!ut/p/a1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOLNDH0MPAzcDbz8vTxNDRy9_Y2NQ13CDA0sTIEKIoEKnN0dPUzMfQwMDEwsjAw8XZw8XMwtfQ0MPM2I02-AAzgaENIfrh-FqsQ9wBmoxN_FydLAGAgNTKEK8DkRrACPGwpyQyMMMj0VAcySpRM!/dl5/d5/L2dBISEvZ0FBIS9nQSEh/pw/Z7_HGK818G0K85260Q5OIRSC42046/res/id=historicoHTML/c=cacheLevelPage/=/'
# url = 'https://servicebus2.caixa.gov.br/portaldeloterias/api/resultados?modalidade=Lotofácil'
# url = sys.argv[1]
file = sys.argv[1] #'resultados.json'
# r = requests.get(url)

# r.text
# r.text = r.text

r = open(file, encoding="utf8")
data = json.load(r)
# data = json.load(urllib.request.urlopen(url))

r_text = data['html'].replace('\\r\\n', '')
r_text = r_text.replace('"\r\n}','')
r_text = r_text.replace('{\r\n  "html:','')
r_text

df = pd.read_html(r_text)
type(df)
type(df[0])
df1 = df
df = df[0].copy()

df = df[df['Bola1'] == df['Bola1']]

df.head()

nr_pop = list(range(1,26))
nr_par = []
nr_impar = []
nr_primo = []
for n in nr_pop:
    if n % 2 == 0:
        nr_par.append(n)
    elif n % 2 == 1:
        nr_impar.append(n)
    for i in range(2,n):
        if n % i == 0:
            break
        elif n not in nr_primo:
            nr_primo.append(n)

comb = []
v_cont = []
for n in nr_pop:
    v_cont.append([n, 0])
'''v01 = 0
v02 = 0
v03 = 0
v04 = 0
v05 = 0
v06 = 0
v07 = 0
v08 = 0
v09 = 0
v10 = 0
v11 = 0
v12 = 0
v13 = 0
v14 = 0
v15 = 0
v16 = 0
v17 = 0
v18 = 0
v19 = 0
v20 = 0
v21 = 0
v22 = 0
v23 = 0
v24 = 0
v25 = 0'''

cols = ['Bola1', 'Bola2', 'Bola3', 'Bola4', 'Bola5', 'Bola6', 'Bola7', 'Bola8', 'Bola9', 'Bola10', 'Bola11', 'Bola12', 'Bola13', 'Bola14', 'Bola15']

for idx, row in df.iterrows():
    v_par = 0
    v_impar = 0
    v_primo = 0
    for c in cols:
        if row[c] in nr_par:
            v_par += 1
        elif row[c] in nr_impar:
            v_impar += 1
        if row[c] in nr_primo:
            v_primo += 1
        for n in nr_pop:
            if row[c] == n:
                v_cont[n-1][1] += 1
        '''if row[c] == 1:
            v01 += 1
        elif row[c] == 2:
            v02 += 1
        elif row[c] == 3:
            v03 += 1
        elif row[c] == 4:
            v04 += 1
        elif row[c] == 5:
            v05 += 1
        elif row[c] == 6:
            v06 += 1
        elif row[c] == 7:
            v07 += 1
        elif row[c] == 8:
            v08 += 1
        elif row[c] == 9:
            v09 += 1
        elif row[c] == 10:
            v10 += 1
        elif row[c] == 11:
            v11 += 1
        elif row[c] == 12:
            v12 += 1
        elif row[c] == 13:
            v13 += 1
        elif row[c] == 14:
            v14 += 1
        elif row[c] == 15:
            v15 += 1
        elif row[c] == 16:
            v16 += 1
        elif row[c] == 17:
            v17 += 1
        elif row[c] == 18:
            v18 += 1
        elif row[c] == 19:
            v19 += 1
        elif row[c] == 20:
            v20 += 1
        elif row[c] == 21:
            v21 += 1
        elif row[c] == 22:
            v22 += 1
        elif row[c] == 23:
            v23 += 1
        elif row[c] == 24:
            v24 += 1
        elif row[c] == 25:
            v25 += 1'''
    comb.append(str(v_par) + 'p-' + str(v_impar) + 'i-' + str(v_primo) + 'np')

freq_nr = v_cont
freq_nr.sort(key=lambda tup: tup[1])

counter_comb = collections.Counter(comb)
resultado = pd.DataFrame(counter_comb.items(), columns=['combination','frequency'])
resultado['p_freq'] = resultado.frequency / resultado.frequency.sum()
resultado.sort_values('p_freq', inplace=True)

print('''
O número mais frequente é: {}
O número menos frequente é: {}
A combinação mais frequente é {}, com a frequência de {}%.
'''.format(freq_nr[-1][0], freq_nr[0][0], resultado['combination'].values[-1], int(resultado['p_freq'].values[-1]*100*100)/100)
)


# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
# Import libraries
import matplotlib.pyplot as plt
import matplotlib.animation as animate
import matplotlib.lines as mlines
import agentframework
import csv

# Request input from user for number of heroes and enemies
print("Welcome to the hero agent game. \nPlease set the number of hero and agents by inputting into the console below...")
num_heroes = int(input("Set the number of heroes: "))
num_enemies = int(input("Set the number of enemies: "))

# Declare variables
heroes = []
enemies =[]
winners = []
environment = []
rowlist = []
# Declare figure for plot
fig = plt.figure(num= 1, figsize=(7, 5))
carry_on = True

# DECLARE FUNCTIONS

# Keeps animation running as long as carry_on = true
def gen_function():
    global carry_on
    a = 0
    while carry_on:
        yield a
        a = a + 1

# Writes end environment to file end_environment.txt
def end_game():
    print('End game function called')
    with open('end_environment.txt', 'w') as e:
        e.write("END ENVIRONMENT: \n")
        for row in environment:
            e.write(" ".join(str(value) for value in row) +"\n")
        e.write("DOCUMENT END \n")
        e.close()

    with open('stores_record.txt', 'a') as s:
        s.write("GAME STARTS with {} heroes and {} enemies: \n".format(num_heroes, num_enemies))
        for hero in heroes:
            s.write("Hero {} finishes with a total store of {}. \n".format(hero.identity, hero.store))
        s.write("GAME ENDS \n")
        s.close()

# Sets updates to figure per iteration
def update(frame_number):

    # Creates figures and axes:
    fig.clear() # Clears figure so that updated markers and environment can be applied at each iteration
    axes = plt.gca() # Points to axis
    # Sets ranges of axes:
    axes.set_xlim([0,300])
    axes.set_ylim([0,300])
    # Adds environment and colour scale key:
    plt.imshow(environment)
    plt.colorbar(ax = axes, orientation= 'horizontal', extend = 'min', spacing = 'proportional', shrink = 0.5).set_label('Environment density')

    # Plots and actions for heroes:

    for hero in heroes: # Loops through heroes
        print(hero) # Heroes prints location and store to console
        global carry_on # Access to stopping condition variable

        # Plots heroes according to status:

        if hero.store >= 3000: # First hero to reach store of 3000 wins and is plotted as winner
            winners.append(hero)
            plt.scatter(winners[0].x, winners[0].y, marker="D", c= "Orange")
            plt.text((winners[0].x + 25), (winners[0].y - 1), "{} is the winner!".format(winners[0].identity), fontsize=8, color='White', backgroundcolor='Black')
            print("We have a winner! Hero {} wins with a store of {}".format(winners[0].identity, winners[0].store) + "\n Remaining heroes:" )
            carry_on = False
            end_game()

        elif hero.store >= 2500: # Fast heroes plotted
            plt.scatter(hero.x, hero.y, c= 'Purple', label='Fast')
            plt.text((hero.x + 8), (hero.y - 1), str(hero.identity), fontsize=8, color='White')
        elif hero.store >= 1000: # Medium heroes plotted
            plt.scatter(hero.x, hero.y, c= 'Pink', label= 'Average')
            plt.text((hero.x + 8), (hero.y - 1), str(hero.identity), fontsize=8, color='White')
        elif hero.store < 1000: # Fast heroes plotted
            plt.scatter(hero.x, hero.y, c= 'Grey', label= 'Slow')
            plt.text((hero.x + 8), (hero.y - 1), str(hero.identity), fontsize=8, color='White')

        # Actions for heroes (movement and sharing and eating of environment)
        hero.move()
        hero.eat()
        hero.share_with_neighbours()

        # Creates key for hero markers
        enemy = mlines.Line2D([], [], color='Black', marker='x', linestyle='None', label='Enemy')
        key_slow = mlines.Line2D([], [], color='Grey', marker='o', linestyle='None', label='Slow hero')
        key_medium = mlines.Line2D([], [], color='Pink', marker='o', linestyle='None', label='Average hero')
        key_fast = mlines.Line2D([], [], color='Purple', marker='o', linestyle='None', label='Fast hero')
        plt.legend(handles=[key_slow, key_medium, key_fast, enemy], bbox_to_anchor=(1,1), bbox_transform=plt.gcf().transFigure, title='Agent key')

    # Plots and actions for enemies
    for enemy in enemies:
        enemy.move()
        enemy.eat()
        plt.scatter(enemy.x, enemy.y, marker="x", c= 'Black')
        for hero in heroes:
            enemy.eat_neighbours(hero) # enemy eats neighbouring hereos' stores

# Creates environment array from data file
f = open('environment.txt', newline='')
reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)

for row in reader:
    for value in row:
        rowlist.append(int(value))
    environment.append(rowlist)
    rowlist = []
f.close()

# Creates heroes (as many as inputted into console by user) and adds them to hereos list
for identity in range(num_heroes):
    heroes.append(agentframework.Agent(environment, heroes, (identity + 1), enemies))

# Creates enemies (as many as inputted into console by user) and adds them to enemies list
for identity in range(num_enemies):
    enemies.append(agentframework.Agent(environment, heroes, (identity + 1), enemies))

# Animates plot
animation = animate.FuncAnimation(fig, update, interval=1, frames=gen_function, repeat=False,)


import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
from numpy.lib.shape_base import split
import math
import cmath

def get_MSI (matrix, f, tau, iterCnt):  #method of simple iterations
    n = np.size(f)
    B = np.diagflat([1] * n) - tau * matrix
    f = tau * f

    x = f
    for i in range (iterCnt):
        x = B.dot(x) + f

    return x

def get_MJ (matrix, f, iterCnt): # method of Jacobi
    u = f
    n = np.size(f)
    for cnt in range(iterCnt):
        uNext = np.zeros(n)
        for i in range (n):
            uNext[i] = (f[i] - matrix[i].dot(u)) / matrix[i][i]
        u = uNext

    return u

def get_MZ (matrix, f, iterCnt):
    u = f
    n = np.size(f)
    answ = np.zeros((n, iterCnt))
    for cnt in range(iterCnt):
        uNext = np.zeros(n)
        for i in range (n):
            uNext[i] = (f[i] -
                                matrix[i][:i - 1].dot(uNext[:i - 1]) -
                                                                        matrix[i][i + 1:].dot(u[i + 1:])) / matrix[i][i]
        u = uNext
        answ[:, cnt] = uNext

    return answ[:, -1]

eigenValuesFilename = "eigenValues15.dat"
#Вход задачи
N = 15  # число точек
h = 1 / (N - 1)
matrix = np.zeros (((N - 2) * (N - 2), N * N))

#построение матрицы
def elem (i, j):
            return N * j + i

for j in range (1, N - 1):
    for i in range (1, N - 1):
        eqID = (j - 1) * (N - 2) + i - 1  #equationID  - индекс строчки

        if (i > 1):
            matrix[eqID][elem(i - 1, j)] = -1

        if (j > 1):
            matrix[eqID][elem(i, j - 1)] = -1

        if (i < N - 2):
            matrix[eqID][elem(i + 1, j)] = -1

        if (j < N - 2):
            matrix[eqID][elem(i, j + 1)] = -1

        matrix[eqID][elem(i, j)] = 4

print ("half")

#Удаление пустых столбцов
zeroColumns = []
for i in range (N * N - 1, -1, -1):
    if i % N == 0 or \
            i < N or \
            i >= N * (N - 1) or \
            (i + 1) % N == 0:
        zeroColumns.append (i)

otherColumns = [x for x in range (N * N - 1) if x not in zeroColumns]
matrix = matrix[:, otherColumns]

print ("matrix is done")
# plt.spy(matrix)
# plt.show()

# поиск собственных чисел
# eigenValues = np.linalg.eigvals(matrix)
# print ("eighenvalues  are computed")
# with open (eigenValuesFilename, "w") as file:
#     for val in eigenValues:
#          file.write(str(val) + "\n")

minVal = 1e9
maxVal = -1e9
with open(eigenValuesFilename, 'r') as file:
    for line in file:
        val = cmath.polar(complex(line))[0]
        if (val > maxVal):
            maxVal = val
        elif val < minVal:
            minVal = val
# Формирование вектора правой части
v = np.zeros((N - 2) * (N - 2))
v[(N // 2) * (N - 2) + N // 2 - 1] = h**2
# v[N * N // 3 : N * N // 2] += h ** 2

#Решаем систему разными методами
# u = get_MSI(matrix, v, 2 / (maxVal + minVal), 100)
# u = get_MSI(matrix, v, 1, 100)
# print (matrix)
# u = get_MJ(matrix, v, 0)
u = get_MZ(matrix, v, 10)

# u = np.linalg.solve(matrix, v)

# Заполняем Матрицу Z для отображения
Z = np.zeros((N, N))
for (uVal, oldID) in zip(u, otherColumns):
    Z[oldID % N][oldID // N] = uVal

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

w = v = np.linspace(0, 1, N)
X, Y = np.meshgrid(w, v)

surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)
ax.zaxis.set_major_locator(LinearLocator(10))
# A StrMethodFormatter is used automatically
# ax.zaxis.set_major_formatter('{x:.02f}')
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.savefig("pic.png")
# plt.show()


#! /bin/python

import os
import sys
import json

import luigi
import nifty.tools as nt

import elf.skeleton.io as skelio
from elf.skeleton import skeletonize as skel_impl, get_method_names

import cluster_tools.utils.volume_utils as vu
import cluster_tools.utils.function_utils as fu
from cluster_tools.cluster_tasks import SlurmTask, LocalTask, LSFTask
from cluster_tools.utils.task_utils import DummyTask

#
# skeletonize tasks
#

class SkeletonizeBase(luigi.Task):
    """ Skeletonize base class
    """

    task_name = 'skeletonize'
    src_file = os.path.abspath(__file__)
    allow_retry = False

    # input and output volumes
    input_path = luigi.Parameter()
    input_key = luigi.Parameter()
    morphology_path = luigi.Parameter()
    morphology_key = luigi.Parameter()
    output_path = luigi.Parameter()
    output_key = luigi.Parameter()
    number_of_labels = luigi.IntParameter()
    resolution = luigi.ListParameter()
    size_threshold = luigi.IntParameter(default=None)
    method = luigi.Parameter(default='thinning')
    dependency = luigi.TaskParameter(default=DummyTask())

    methods = get_method_names()

    # expose skeletonization parameter if we support more parameter
    @staticmethod
    def default_task_config():
        # we use this to get also get the common default config
        config = LocalTask.default_task_config()
        config.update({'chunk_len': 1000, 'method_kwargs': {}})
        return config

    def requires(self):
        return self.dependency

    def _prepare_output(self, config):
        # make the blocking
        block_len = min(self.number_of_labels, config.get('chunk_len', 1000))
        block_list = vu.blocks_in_volume((self.number_of_labels,),
                                         (block_len,))
        n_jobs = min(len(block_list), self.max_jobs)
        # require output dataset
        with vu.file_reader(self.output_path) as f:
            f.require_dataset(self.output_key, shape=(self.number_of_labels,),
                              chunks=(1,), compression='gzip', dtype='uint64')
        # update the config
        config.update({'number_of_labels': self.number_of_labels,
                       'block_len': block_len})
        return config, n_jobs, block_list

    def run_impl(self):
        assert self.method in self.methods,\
            "Method %s is not supported, must be one of %s" % (self.method, str(self.methods))

        # TODO support roi
        # get the global config and init configs
        shebang, block_shape, _, _ = self.global_config_values()
        self.init(shebang)

        # load the skeletonize config
        # update the config with input and output paths and keys
        config = self.get_task_config()
        config.update({'input_path': self.input_path, 'input_key': self.input_key,
                       'morphology_path': self.morphology_path,
                       'morphology_key': self.morphology_key,
                       'output_path': self.output_path, 'output_key': self.output_key,
                       'resolution': self.resolution, 'size_threshold': self.size_threshold,
                       'method': self.method})
        config, n_jobs, block_list = self._prepare_output(config)

        # prime and run the jobs
        self.prepare_jobs(n_jobs, block_list, config)
        self.submit_jobs(n_jobs)

        # wait till jobs finish and check for job success
        self.wait_for_jobs()
        self.check_jobs(n_jobs)

class SkeletonizeLocal(SkeletonizeBase, LocalTask):
    """
    skeletonize on local machine
    """
    pass

class SkeletonizeSlurm(SkeletonizeBase, SlurmTask):
    """
    skeletonize on slurm cluster
    """
    pass

class SkeletonizeLSF(SkeletonizeBase, LSFTask):
    """
    skeletonize on lsf cluster
    """
    pass

#
# Implementation
#

# not parallelized for now
def _skeletonize_id_block(blocking, block_id, ds_in, ds_out,
                          sizes, bb_min, bb_max, resolution, size_threshold,
                          method):

    fu.log("start processing block %i" % block_id)
    block = blocking.getBlock(block_id)
    id_begin, id_end = block.begin[0], block.end[0]
    # we don't compute the skeleton for id 0, which is reserved for the ignore label
    id_begin = 1 if id_begin == 0 else id_begin

    # we increase the bounding box with a small halo, otherwise there
    # semms to be boundary inconsistencies
    halo = (2, 2, 2)
    shape = ds_in.shape

    # skeletonize ids in range and serialize skeletons
    for seg_id in range(id_begin, id_end):
        if size_threshold is not None:
            if sizes[seg_id] < size_threshold:
                continue
        bb = tuple(slice(max(int(mi - ha), 0),
                         min(int(ma + ha), sh)) for mi, ma, sh, ha in zip(bb_min[seg_id],
                                                                          bb_max[seg_id],
                                                                          shape, halo))
        fu.log("skeletonize id %i from bb %s" % (seg_id, str(bb)))
        obj = ds_in[bb] == seg_id

        # try to skeletonize the object, skip if any exception is thrown
        try:
            nodes, edges = skel_impl(obj, resolution=resolution, method=method)
        except Exception:
            continue

        offsets = [b.start * res for b, res in zip(bb, resolution)]
        skelio.write_n5(ds_out, seg_id, nodes, edges, offsets)
    fu.log_block_success(block_id)

def skeletonize(job_id, config_path):
    fu.log("start processing job %i" % job_id)
    fu.log("reading config from %s" % config_path)
    with open(config_path, 'r') as f:
        config = json.load(f)

    # read the input cofig
    input_path = config['input_path']
    input_key = config['input_key']
    morphology_path = config['morphology_path']
    morphology_key = config['morphology_key']
    output_path = config['output_path']
    output_key = config['output_key']
    size_threshold = config['size_threshold']
    resolution = config['resolution']
    method = config['method']

    # morphology feature-columns
    # 0    = label-id
    # 1    = pixel size
    # 2:5  = center of mass
    # 5:8  = min coordinate
    # 8:11 = max coordinate
    with vu.file_reader(morphology_path) as f:
        morpho = f[morphology_key][:]
        sizes = morpho[:, 1].astype('uint64')
        bb_min = morpho[:, 5:8].astype('uint64')
        bb_max = morpho[:, 8:11].astype('uint64') + 1

    block_list = config['block_list']
    block_len = config['block_len']
    n_labels = config['number_of_labels']
    blocking = nt.blocking([0], [n_labels], [block_len])

    # skeletonize this id block
    with vu.file_reader(input_path, 'r') as f_in, vu.file_reader(output_path) as f_out:
        ds_in = f_in[input_key]
        ds_out = f_out[output_key]
        for block_id in block_list:
            _skeletonize_id_block(blocking, block_id, ds_in, ds_out,
                                  sizes, bb_min, bb_max, resolution, size_threshold,
                                  method)

    # log success
    fu.log_job_success(job_id)

if __name__ == '__main__':
    path = sys.argv[1]
    assert os.path.exists(path), path
    job_id = int(os.path.split(path)[1].split('.')[0].split('_')[-1])
    skeletonize(job_id, path)


"""
ID: detrime1
LANG: PYTHON3
TASK: friday
"""

import sys,os.path
from collections import *
if os.path.exists('friday.in'):
    sys.stdin = open('friday.in', 'r')
    sys.stdout = open('friday.out', 'w')

def detri():
	n = int(input())
	weekDays = [0]*7
	monthDays = [31,28,31,30,31,30,31,31,30,31,30,31]
	day = 2
	for year in range(1900, 1900+n):
		if str(year)[2:]=="00":
			if not year%400: monthDays[1] = 29
		elif not year%4: monthDays[1] = 29
		else: monthDays[1] = 28
		for monthDay in monthDays:
			day += 12
			day %= 7
			weekDays[day] += 1
			day += monthDay-12
	print(*weekDays)

	# WORKING GARBAGE WACK BRUTE FORCE
	"""days = ["SAT","SUN","MON","TUE","WED","THU","FRI"]
				months = {
				"JAN":31,
				"FEB":(28,29),
				"MAR":31,
				"APR":30,
				"MAY":31,
				"JUN":30,
				"JUL":31,
				"AUG":31,
				"SEP":30,
				"OCT":31,
				"NOV":30,
				"DEC":31,
				}
				ans = {day:0 for day in days}
				n = int(input())
				day = 0
				for year in range(1900, 1900+n):
					leap = False
					if str(year)[2:]=="00":
						if not year%400:
							leap = True
					else:
						if not year%4:
							leap = True
					months["FEB"] = 29 if leap else 28
					for month in months:
						for i in range(1,months[month]+1):
							day = (day+1)%7
							if i==13:
								ans[days[day]] += 1
				check = []
				for val in ans.values():
					check.append(val)
				check[:] = check[6:]+check[:6]
				print(*check)"""

if __name__ == "__main__":
	detri()

Chapter 1/Friday The Thirteenth/friday.py


from flask import request
from flask_restx import Resource

from ..service.auth_service import Auth
from ..util.decorator import admin_token_required
from ..service.user_service import save_new_user, get_a_user
from ..util.dto import AuthDto

api = AuthDto.api
user_auth = AuthDto.user_auth
user_token = AuthDto.user_token

@api.route('/register')
class UserRegister(Resource):
    """
        User Register Resource
    """
    @api.doc('user register')
    @api.expect(user_auth, validate=True)
    def post(self):
        # get the post data
        data = request.json
        return save_new_user(data=data)

@api.route('/login')
class UserLogin(Resource):
    """
        User Login Resource
    """
    @api.doc('user login')
    @api.expect(user_auth, validate=True)
    def post(self):
        # get the post data
        post_data = request.json
        return Auth.login_user(data=post_data)

@api.route('/logout')
class LogoutAPI(Resource):
    """
    Logout Resource
    """
    @api.doc('logout a user')
    @api.doc(params={'Authorization': {'in': 'header', 'description': 'An authorization token'}})
    def post(self):
        """
        logout user
        """
        # get auth token
        auth_header = request.headers.get('Authorization')
        return Auth.logout_user(data=auth_header)

@api.route('/force-logout/<id>')
class ForceLogoutAPI(Resource):
    """
    Force Logout Resource
    """
    @api.doc('force logout a user')
    @api.doc(params={'Authorization': {'in': 'header', 'description': 'An authorization token'}})
    @api.response(404, 'User not found.')
    @admin_token_required
    def post(self, id):
        """
        force logout a user and blacklist all the tokens
        """
        user = user = get_a_user(id)
        if not user:
            api.abort(404)
        return Auth.force_logout_user(user)

@api.route('/expire-token')
class ExpireTokenAPI(Resource):
    """
    Expire Token Resource
    """
    @api.doc('expire a user token')
    @api.doc(params={'Authorization': {'in': 'header', 'description': 'An authorization token'}})
    @api.expect(user_token, validate=True)
    @admin_token_required
    def post(self):
        """
        expire a token passed in post body, admin only authorized
        """
        token = request.json['token']
        return Auth.expire_token(token)


import csv
import argparse
import os
import sys

import numpy as np
import torch
import torch.cuda
from PIL import Image
from torch.autograd import Variable
from torchvision.transforms import transforms

from my.yolov3.easy.net.load_net import load_net
from PIL import Image

image_size = (96, 96)

test_transformations = transforms.Compose([
    transforms.ToTensor()
])

def load_trained_net(model_path):
    print("Begin to load pre-trained net ... ", end="")
    net = load_net("resnet152")
    checkpoint = torch.load(model_path)
    net.load_state_dict(checkpoint['state_dict'])
    net.eval()
    print("Finished.")
    return net

def predict(net, ims: list):
    # Define transformations for the image
    transformation = test_transformations

    images_tensor_list = []
    for im in ims:
        w = max(im.size)  # 正方形的宽度
        im = im.crop((0, 0, w, w)).resize(image_size)  # 补成正方形再压缩
        image = np.asarray(im)
        image_tensor = transformation(image)
        images_tensor_list.append(image_tensor)

    images_tensor = torch.stack(images_tensor_list)

    if torch.cuda.is_available():
        images_tensor.cuda()

    # 将输入变为变量
    input = Variable(images_tensor)

    # 预测图像的类
    output = net(input)
    index = output.data.numpy().argmax(axis=1)
    return index + 1  # [0, C-1] -> [1, C]

if __name__ == '__main__':
    net = load_trained_net("model/model-87-8.477896466274615e-05.pth")

    image_paths = ["../data/images/0a0bf7bc-e0d7-4f20-abec-039136663d85.jpg",
            "../data/images/0a0c27d7-2e2a-4817-a715-8182cf07ec9b.jpg",
            "../data/images/0a00c2a3-a498-452a-ba88-6b9ef514e201.jpg",
            "../data/images/0a1a5d35-1b30-43ff-87bc-9acdab1567c1.jpg"]

    ims = []

    for image_path in image_paths:
        im = Image.open(image_path)
        ims.append(im)

    results = predict(net, ims)
    print(results)


houses = [[3,7],[1,9],[2,0],[5,15],[4,30]]
# houses = [[1,2],[2,3],[3,1],[4,20]]
d = {}
for i in houses:
    d[i[0]] = i[1]
SortedDict = {k: v for k, v in sorted(d.items(), key=lambda item: item[1])}
print(SortedDict)
fList = []
finalList = []
for j in SortedDict:
    fList.append(j)

print(fList)
finalList = fList[-2:]
finalList.sort()
print(finalList)

# d = {1:'a',3:'b',2:'c',5:'u'}
# print(d)
# print(d.items())


'''Process Restricted Friend Requests
https://leetcode.com/problems/process-restricted-friend-requests/

You are given an integer n indicating the number of people in a network. Each person is labeled from 0 to n - 1.

You are also given a 0-indexed 2D integer array restrictions, where restrictions[i] = [xi, yi] means that person xi
and person yi cannot become friends, either directly or indirectly through other people.

Initially, no one is friends with each other. You are given a list of friend requests as a 0-indexed 2D integer array
requests, where requests[j] = [uj, vj] is a friend request between person uj and person vj.

A friend request is successful if uj and vj can be friends. Each friend request is processed in the given order (i.e.,
requests[j] occurs before requests[j + 1]), and upon a successful request, uj and vj become direct friends for all
future friend requests.

Return a boolean array result, where each result[j] is true if the jth friend request is successful or false if it is
not.

Note: If uj and vj are already direct friends, the request is still successful.

Example 1:

Input: n = 3, restrictions = [[0,1]], requests = [[0,2],[2,1]]
Output: [true,false]
Explanation:
Request 0: Person 0 and person 2 can be friends, so they become direct friends.
Request 1: Person 2 and person 1 cannot be friends since person 0 and person 1 would be indirect friends (1--2--0).
'''

from collections import defaultdict, deque
class Solution:
    def friendRequests(self, n: int, restrictions: List[List[int]], requests: List[List[int]]) -> List[bool]:

        def bfs(node):
            q = deque([node])
            visited = set()
            visited.add(node)
            relations = set()
            while q:
                node = q.popleft()
                relations.add(node)
                for neighbor in friend_mapping[node]:
                    if neighbor not in visited:
                        visited.add(neighbor)
                        q.append(neighbor)
            return relations

        banned_mapping = defaultdict(set)
        for u, v in restrictions:
            banned_mapping[u].add(v)
            banned_mapping[v].add(u)

        result = [False] * len(requests)
        friend_mapping = defaultdict(set)
        for index, req in enumerate(requests):
            u, v = req[0], req[1]
            if v in banned_mapping[u]:
                result[index] = False
            else:
                set1 = bfs(u)
                set2 = bfs(v)
                banned = False
                for key in set1:
                    for bannedId in banned_mapping[key]:
                        if bannedId in set2:
                            result[index] = False
                            banned = True
                if not banned:
                    result[index] = True
                    friend_mapping[u].add(v)
                    friend_mapping[v].add(u)
        return result

Graph/Process Restricted Friend Requests.py


import boto3
import json
import os

dynamodb = boto3.resource('dynamodb')
client = boto3.client('dynamodb')
USERS_TABLE = dynamodb.Table(os.environ['USERS_TABLE'])

def delete_user_service(event, context):
    try:
        response = USERS_TABLE.update_item(
            Key={
                'userId': event['pathParameters']['id']
            },
            ConditionExpression='attribute_exists(userId)',
            UpdateExpression='SET active = :active',
            ExpressionAttributeValues={':active': False}
        )
        print('[GET RESPONSE]:', response)
        return {
            'statusCode': 200,
            'body': json.dumps('user deleted.')
        }
    except Exception as e:
        print("Error deleting user:")
        print(e)
        return {
            'statusCode': 400,
            'body': json.dumps('Error deleting the user')
        }


# -*- coding: utf-8 -*-
#------------------------------------------------------------
# pelisalacarta - XBMC Plugin
# Conector para blip.tv
# http://blog.tvalacarta.info/plugin-xbmc/pelisalacarta/
#------------------------------------------------------------
import re
import urllib

try:
    from core import scrapertools
    from core import logger
except:
    from Code.core import scrapertools
    from Code.core import logger

# Resuelve los videos de blip.tv que se usan en el embed
#
def geturl(bliptv_url,recurse=True):
    logger.info("[bliptv.py] bliptv_url="+bliptv_url)

    devuelve = ""

    if bliptv_url.startswith("http://blip.tv/play"):
        redirect = scrapertools.getLocationHeaderFromResponse(bliptv_url)
        logger.info("[bliptv.py] redirect="+redirect)

        patron='file\=(.*?)$'
        matches = re.compile(patron).findall(redirect)
        logger.info("[bliptv.py] matches1=%d" % len(matches))

        if len(matches)==0:
            patron='file\=([^\&]+)\&'
            matches = re.compile(patron).findall(redirect)
            logger.info("[bliptv.py] matches2=%d" % len(matches))

        if len(matches)>0:
            url = matches[0]
            logger.info("[bliptv.py] url="+url)
            url = urllib.unquote(url)
            logger.info("[bliptv.py] url="+url)

            data = scrapertools.cache_page(url)
            logger.info(data)
            patron = '<media\:content url\="([^"]+)" blip\:role="([^"]+)".*?type="([^"]+)"[^>]+>'
            matches = re.compile(patron).findall(data)
            scrapertools.printMatches(matches)

            for match in matches:
                logger.info("url="+str(match[0]))
                devuelve = match[0]

    return devuelve


import requests
import bs4
import urllib

def spider(max_pages):
    for page in range(1, max_pages + 1):
        query = urllib.parse.urlencode({'query':u'대선후보'})
        url = 'http://news.naver.com/main/search/search.nhn?query=' + '%B4%EB%BC%B1%C8%C4%BA%B8'
        source_code = requests.get(url)
        plain_text = source_code.text
        soup = bs4.BeautifulSoup(plain_text, 'html.parser')
        content = soup.find(id='search_div')
        for result in content.select('ul > li > div'):
            print('############# Title')
            print(result.a.text)
            print('############# Content')
            print(result.p.text)

spider(1)

C Team(Hong, Heo)/Crowaling/Practice 1.py


import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
# import snntorch
import pandas as pd
import tqdm
import argparse
from . import p_snu_layer

class SNN_Net(torch.nn.Module):
    def __init__(self, inputs_num = 4, hidden_num = 4, outputs_num = 3 ,l_tau = 0.8,num_time = 100, batch_size = 80 ,soft = False, rec = False, power = False, gpu = True):
        super().__init__()

        self.num_time = num_time
        self.batch_size = batch_size
        self.rec = rec
        self.power = power

        #parametr
        # self.neuron_0 = 4
        # self.neuron_1 = 24
        # self.neuron_2 = 24
        # self.neuron_3 = 4

        #my #hidden num = 24
        # self.l1 = p_snu_layer.P_SNU(inputs_num, hidden_num, l_tau = l_tau, soft = soft, gpu = gpu)
        # self.l2 = p_snu_layer.P_SNU(hidden_num, hidden_num, l_tau = l_tau, soft = soft, gpu = gpu)
        # self.l3 = p_snu_layer.P_SNU(hidden_num, hidden_num, l_tau = l_tau, soft = soft, gpu = gpu)
        # self.l4 = p_snu_layer.P_SNU(hidden_num, outputs_num, l_tau = l_tau, soft = soft, gpu = gpu)

        #my2 hidden num = 4
        self.l1 = p_snu_layer.P_SNU(inputs_num, hidden_num, l_tau = l_tau, soft = soft, gpu = gpu)
        self.l2 = p_snu_layer.P_SNU(hidden_num, hidden_num, l_tau = l_tau, soft = soft, gpu = gpu)
        self.l3 = p_snu_layer.P_SNU(hidden_num, outputs_num, l_tau = l_tau, soft = soft, gpu = gpu)

        # for 1 layer test
        # self.l4 = p_snu_layer.P_SNU(inputs_num, outputs_num, l_tau = l_tau, soft = soft, gpu = gpu)

    def reset_state(self):
        self.l1.reset_state()
        self.l2.reset_state()
        self.l3.reset_state()
        # self.l4.reset_state()

    def forward(self,x,y):
        # y = torch.tensor(y)
        losse = None
        accuracy = None
        sum_out = None #タイムステップ毎のスパイク数の累積をとる
        out_list = [] #各データ(120×4)のタイムステップ(100ms)における出力スパイクを時系列で挿入
        out_total_list = []
        membrane_out = torch.empty(100,3)
        mem1_out = torch.empty(100,4)
        mem2_out = torch.empty(100,4)
        spikes_ = torch.empty(100,4)

        self.reset_state()

        for time in range(self.num_time): #num_timeはデフォルトで100になる。
            # spike_encoded_neuron = x[time]
            # target_ = torch.reshape(y[time],(1,3))
            # spike_encoded_neuron = torch.reshape(x[time],(4,1))

            #4→4→3(network)
            spike_encoded_neuron = x[time]
            h1,mem1,u1 = self.l1(spike_encoded_neuron)
            h2,mem2,u2 = self.l2(h1)
            # out,mem = self.l3(h2)

            # 1 layer test 4→3(network)
            #膜電位と入出力スパイクの確認
            # out,thresh,spike = self.l4(spike_encoded_neuron)

            #normal
            # out = self.l4(spike_encoded_neuron)

            # sum_out = out if sum_out is None else sum_out + out
            mem1_out[time] = mem1
            mem2_out[time] = mem2
            # membrane_out[time] = out
            spikes_[time] = h1

        #出力を確認する
        # return sum_out,y

        #バッチ学習の場合
        # criterion = nn.CrossEntropyLoss()
        # losse = criterion(sum_out,y)

        #正解率
        # predicted_label = torch.argmax(sum_out)
        # accuracy = 1 if predicted_label == y else 0
        return spikes_,mem1_out,mem2_out
        return mem_out
        return sum_out,losse,accuracy


class Solution:
    def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
        dp = [[0] * (n+1) for _ in range(m+1)]
        counter=[[s.count("0"), s.count("1")] for s in strs]

        for zeroes, ones in counter:
            for i in range(m, zeroes-1, -1):
                for j in range(n, ones-1, -1):
                    dp[i][j] = max(dp[i][j], 1+dp[i-zeroes][j-ones])

        return dp[-1][-1]

Leetcode/May 2022/23)474. Ones and Zeroes.py


# 2SUM
# http://rosalind.info/problems/2sum/

from utilities import get_file, get_answer_file

def two_sum(num_array):
    minus_set = set(-i for i in num_array)
    for i, value in enumerate(num_array):
        if value in minus_set:
            try:
                j = num_array.index(-value, i+1)
            except ValueError:
                continue

            return (i+1, j+1)
    return (-1, -1)

with get_file() as file:
    k, n = map(int, file.readline().split())
    num_arrays = [list(map(int, line.split())) for line in file.readlines()]

with get_answer_file() as file:
    for num_array in num_arrays:
        p, q = two_sum(num_array)
        if p == -1:
            print(-1, file=file)
        else:
            print(p, q, file=file)


#  This file is part of RADAR.
#  Copyright (C) 2019 Cole Daubenspeck
#
#  RADAR is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  RADAR is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with RADAR.  If not, see <https://www.gnu.org/licenses/>.

import re

from typing import Tuple

# used if anything wants to sort/use numeric values instead of strings
VALUE_STANDARD_CONVERSIONS = {
    "unknown": 0,
    "low": 1,
    "medium": 2,
    "high": 3,
    "very-high": 4
}

# format
#   device_type_string
#       - list of services (if it might not appear in consistent ports) (THESE ARE REGULAR EXPRESSIONS!)
#       - list of ports that correspond to hosts of that type (prefered unless service tends to run on different ports)
HOST_TYPE = {
    "webserver": {
        "value": "high",
        "service_names": [
            "werkzeug",
            "httpd",
            "nginx",
            "apache"
        ],
        "ports": [
            80,
            443,
            3000,  # node webserver
            8000,
            8443
        ]
    },
    "database": {
        "value": "very-high",
        "service_names": [
        ],
        "ports": [
            1433,  # mssql
            3306,  # mysql
            6379,  # redis
            27017, # mongo

        ]
    },
    "fileserver": {
        "value": "high",
        "service_names": [
        ],
        "ports": [
            21,
            990
        ]
    },
    "mailserver": {
        "value": "medium",
        "service_names": [
        ],
        "ports": [
            25,  # smtp ports
            468,
            587,
            2525,
            110,  # pop3 ports
            993,
            143,  # imap ports
            995
        ]
    },
    "ics": {  # industrial control system
        "value": "very-high",
        "service_names": [
            "modbus"
        ],
        "ports": [
            502
        ]
    },
    "domain_controller": {
        "value": "very-high",
        "service_names": [
        ],
        "ports": [
            88  # kerberos
        ]
    }
}

def get_info(target: dict) -> Tuple[str, str]:
    """ For a given target, returns information about the priority and best-guess type of host

    arguments:
    target: a dictionary that conforms to RADAR target specifications

    returns:
    a tuple of strings (priority, type). First string is the value of the device (e.g. "high"), second is the type of device (e.g. "webserver").
    Multiple device types will be seperated with a semicolon (e.g. 'webserver;database').
    """
    services = target.get("services")

    if not services:  # no running services, we don't care
        return "unknown", "generic"

    device_value = "unknown"
    device_type = ""

    global HOST_TYPE
    global VALUE_STANDARD_CONVERSIONS
    # for every service on the target
    for service in services:
        port = int(service.get("port"))
        name = service.get("service")
        # check if any of the host types matches the target...
        for host_type, details in HOST_TYPE.items():
            # skip checking the type if it's already flagged (e.g. it has multiple services related to being a webserver)
            if host_type in device_type:
                continue
            type_value = details.get("value")
            # by seeing if the port is in one of the lists
            if port in details.get("ports"):
                device_value = device_value if VALUE_STANDARD_CONVERSIONS[type_value] < VALUE_STANDARD_CONVERSIONS[device_value] else type_value
                device_type += f";{host_type}"
            # or by seeing if any of the patterns matches
            else:
                for check_names in details.get("service_names", []):
                    if re.search(check_names, name):
                        device_value = device_value if VALUE_STANDARD_CONVERSIONS[type_value] < VALUE_STANDARD_CONVERSIONS[device_value] else type_value
                        device_type += f";{host_type}"
                        break

    return device_value, device_type[1:] or "unknown"


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('tapp', '0005_comment_end_i'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='essay',
            options={'permissions': (('view_essay', 'View essay'),)},
        ),
    ]


s = open('Day14.txt').read()

def twist(a, n):
    return a[:n][::-1] + a[n:]

def skip(a, k):
    return a[k:] + a[:k]

def knot(a, x):
    p = k = 0
    for i in x:
        a = skip(twist(skip(a, p), i), -p)
        p = (p + i + k) % len(a)
        k += 1
    return a

def densehash(a):
    return list(map(
        lambda x: reduce(int.__xor__, x),
        zip(*(a[i::16] for i in range(16)))
    ))

def knothash(x):
    a = list(range(256))
    suffix = [17, 31, 73, 47, 23]
    y = list(map(ord, x)) + suffix
    dh = densehash(knot(a, y * 64))
    return ''.join('{:02x}'.format(n) for n in dh)

def kh(x):
    i = lambda h: int(h, 16)
    b = '{:04b}'.format
    f = lambda h: b(i(h))
    return ''.join(map(f, knothash(x)))

def view(x):
    return kh(x)[:8].replace('1', '#').replace('0', '.')

arr = np.array(list(map(int, ''.join(map(kh, (s + '-' + i for i in map(str, range(128))))))))

print(arr.sum())

a = arr.reshape(128, -1)

seen = set()
available = set((i, j) for i in range(128) for j in range(128))
groups = defaultdict(set)

def path(i, j, g):
    if (i >= 0) and (j >= 0):
        seen.add((i, j))
        available.difference_update([(i, j)])
        if a[i, j] == 1:
            groups[g].add((i, j))
            moves = set([
                (i - 1, j), (i + 1, j),
                (i, j - 1), (i, j + 1)
            ])
            for i, j in (moves & available):
                path(i, j, g)

g = 0
while available:
    i, j = next(iter(available))
    path(i, j, g)
    g += 1

print(len(groups.keys()))

print(
    '\n'.join(map(view, (t + '-{}'.format(i) for i in range(8))))
)


import pandas as pd
import numpy as np
import io
import requests
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn import metrics
from beta_encoder import BetaEncoder
import category_encoders as ce
from utils import *
import csv
import xgboost as xgb

def run_ls_experiments():

    print("Loading Data")
    df = load_data()

    continuous = ['company_size', 'interested_desks']
    categorical = ['industry','location', 'lead_source']
    #columns:
    print("continuous columns: ",continuous)
    print("categorical columns: ",categorical)

    # plot increasing dimensionality va computation time
    sample_sizes = [2000,
    4000,
    6000,
    8000,
    10000,
    12000,
    14000,
    16000,
    18000,
    20000,
    22000,
    24000,
    26000,
    28000,
    30000,
    32000,
    34000,
    36000,
    38000,
    40000,
    42000,
    44000,
    46000,
    48000,
    50000]
    sample_sizes = [42000,
    44000,
    46000,
    48000,
    50000]

    results = [['model','Encoder','Accuracy','STD','Training Time','Sparsity','Dimensions','sample_size']]
    for sample_size in sample_sizes:

        print("")
        print("----------------------")
        print("Sample Size: ",sample_size)
        print("----------------------")

        if not sample_size < len(df):
            sample_size = len(df)
        sample = df.sample(sample_size)

        X = sample[continuous+categorical]
        y = sample[['converted']]

        successes = y.sum()[0]
        alpha_prior = float(successes / len(y))

        model = xgb.XGBClassifier(n_jobs=4) #[GradientBoostingClassifier(max_depth=8, n_estimators=64)]

        #BetaEncoder (mean)
        print("Beta Encoder (mean) Results:")
        acc, std, time, sparsity, dimensions = cv_lead_scoring_classification(model, X, y, continuous, categorical, encoder=BetaEncoder(alpha=alpha_prior, beta=1-alpha_prior))
        results.append([type(model), 'BetaEncoder (m)', acc, std, time, sparsity, dimensions,sample_size])

        #OneHotEncoder
        print("OneHotEncoder Results:")
        acc, std, time, sparsity, dimensions = cv_lead_scoring_classification(model, X, y, continuous, categorical, encoder=OneHotEncoder(handle_unknown='ignore', sparse=False))
        results.append([type(model), 'OneHotEncoder', acc, std, time, sparsity, dimensions,sample_size])

    file = 'lead_scoring_experiments_comp_time_2.csv'
    with open(file, "w") as output:
        writer = csv.writer(output, lineterminator='\n')
        writer.writerows(results)
    try:
        upload_file(file)
    except:
        print("File Not Uploaded")

def load_data():
    df = pd.read_csv('lead_scoring_1mil.csv')
    df = df.fillna('null')
    industries = df.industry.str.split(',', n=-1, expand=True)
    df['industry'] = industries[0]
    #training_df['sector'] = industries[1]

    return df

if __name__ == '__main__':
    run_ls_experiments()


from sklearn.datasets import load_boston

from  sklearn.model_selection import train_test_split           # model_selection模型选择过程中各种数据分割的类与函数

from sklearn.preprocessing import StandardScaler

from sklearn.linear_model import LinearRegression, SGDRegressor, LogisticRegression           # 线性回归
# externals是外部的、外部扩展的意思
from sklearn.externals import joblib             # 模型的保存和提取  ==》  https://blog.csdn.net/YZXnuaa/article/details/80694372

from sklearn.metrics import mean_squared_error, classification_report    # 均方误差的评估
import pandas as pd
import numpy as np

# 通过线性回归预测房价
# （数据量小的时候，可以用linearRegression，也就是最小二乘法之正规方程，可以直接求得误差最小点，但是计算量特别大，数据多时不推荐使用）
# 这个也就是高中学的线性方程的求解k和b的值类似的方法
def mylinear():
    # 获取数据
    lb = load_boston()
    # 特征数值化

    # 分割数据集
    x_train, x_test, y_train, y_test = train_test_split(lb.data, lb.target, test_size=0.25)

    # 归一化处理
    """因为特征值和目标值对应的数组维度不一样， 所以无法用同一个转换器进行归一化，而是分别创造一个转换器"""
    std_x = StandardScaler()
    x_train = std_x.fit_transform(x_train)
    x_test = std_x.transform(x_test)
    # 对于目标的
    std_y = StandardScaler()
    y_train = std_y.fit_transform(y_train.reshape(-1, 1))
    y_test = std_y.transform(y_test.reshape(-1, 1))

    # 建立模型
    lr = LinearRegression()
    lr.fit(x_train, y_train)
    print(lr.coef_)

    # 模型保存，可以在别的地方直接加载出来这次保存好的模型用来继续学习或者预测使用
    joblib.dump(lr, './通过joblib保存线性回归模型.pkl')

    # 预测结果评估
    y_predict = lr.predict(x_test)
    # 线性回归模型的好坏通过均方误差来评测
    print("产生的均方误差为：", mean_squared_error(y_test, y_predict))
    # 将数据变回归一化之前的大小
    y_predict_orign = std_y.inverse_transform(y_predict)
    print(y_predict, "==================", y_predict_orign)    # ==》产生的均方误差为： 0.42782165417388635

# 用梯度下降来预测房价,在数据量大的时候推荐使用这个
def sgd_regression():
    # 下面这些是直接从上面的函数中copy下来的
    # 获取数据
    lb = load_boston()
    # 特征数值化
    # 分割数据集
    x_train, x_test, y_train, y_test = train_test_split(lb.data, lb.target, test_size=0.25)
    # 归一化处理
    """因为特征值和目标值对应的数组维度不一样， 所以无法用同一个转换器进行归一化，而是分别创造一个转换器"""
    std_x = StandardScaler()
    x_train = std_x.fit_transform(x_train)
    x_test = std_x.transform(x_test)
    # 对于目标的
    std_y = StandardScaler()
    y_train = std_y.fit_transform(y_train.reshape(-1, 1))
    y_test = std_y.transform(y_test.reshape(-1, 1))

    # 建立模型(基于随机梯度下降法估计参数的SGDRegressor)
    sgd = SGDRegressor()

    # 预测并评估
    sgd.fit(x_train, y_train)
    y_predict = sgd.predict(x_test)
    print(mean_squared_error(y_test, y_predict))  # ==》产生的均方误差为： 0.21901470588593194

# 岭回归：也就是逻辑回归
# 只能用于解决二分类问题，属于判别模型（与前面所学的生成模型相区别，生成模型是需要从历史中总结出某些概率）
# 但我感觉对于判别模型和生成模型还是不太对

# 岭回归解觉二分类问题的思路 ==》 https://blog.csdn.net/weixin_39445556/article/details/83930186
# 先生成回归曲线，再从y轴找一个合理的值设置为阈值（临界值），求解的y值大于这个阈值的为一个结果，y小于这个阈值是另一个结果
# 阈值的选择可以根据二分类中的一类占总数量的比例
def LingHuiGui():
    # 对于癌症的预测
    # 读取数据
    column = ['Sample code number','Clump Thickness', 'Uniformity of Cell Size','Uniformity of Cell Shape','Marginal Adhesion', 'Single Epithelial Cell Size','Bare Nuclei','Bland Chromatin','Normal Nucleoli','Mitoses','Class']
    data = pd.read_csv(r"E:\chrome下载的东西\breast-cancer-wisconsin.data", names=column)   # 这个指定标签名的方式一定要学一学
    print(data)

    # 缺失值处理
    data = data.replace("?", np.nan)
    data = data.dropna()

    # 数据分离                                            # data中的column是前面第89行的那个标签名列表
    x_train, x_test, y_train, y_test = train_test_split(data[column[1:10]], data[column[10]], test_size=0.25)

    # 数据标准化（归一化）
    std_x = StandardScaler()
    x_train = std_x.fit_transform(x_train)
    x_test = std_x.transform(x_test)

    # 这里用的逻辑回归是个二分类方法，是解决二分类问题的
    # std_y = StandardScaler()
    # y_train = std_y.fit_transform(y_train)
    # y_test = std_y.transform(y_test)

    # 建立模型
    lg = LogisticRegression(C=1.0)  # c为正则化参数（正则化用于解决过拟合问题，可以减少模型的复杂度）
    lg.fit(x_train, y_train)
    y_predict = lg.predict(x_test)

    # 误差评估
    print("精确率：", lg.score(x_test, y_test))
    # 将结果为2、4两个数值换为名字良性和恶性
    print("召回率", classification_report(y_test, y_predict, labels=[2, 4], target_names=["良性", "恶性"]))

print("加油！")
# mylinear()
# sgd_regression()
LingHuiGui()

机器学习/19_线性回归.py


from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register"),
    path("newListing", views.newForm, name="new"),
    path("createListing", views.createNewListing, name="create"),
    path("<str:title>Listing", views.listing_view, name="listing"),
    path("Watchlist", views.watchlist, name="watchlist"),
    path("AddToWatchlist/<str:title>", views.add_to_watchlist, name="add"),
    path("RemoveFromWatchlist/<str:title>", views.remove_from_watchlist, name="remove"),
    path("placeBid/<str:title>", views.place_bid, name="bid"),
    path("closeListing/<str:title>", views.close_listing, name="close"),
    path("postComment/<str:title>", views.post_comment, name="postComment"),
    path("categories", views.categories_view, name="categories"),
    path("showCategory/<str:category>", views.single_category_view, name="showCategory")
]


import math
import boto3
from aws_cdk import (
    core,
    aws_ec2 as ec2,
    aws_ecs as ecs,
    aws_cloudwatch as cw
    )

from cdklocust.locust_container import locustContainer

class CdklocustStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, vpc, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        self.get_cdk_context()
        self.vpc=vpc

        #ECS cluster for the loadgen
        self.loadgen_cluster = ecs.Cluster(
            self, "Loadgen-Cluster",
            vpc=self.vpc
        )

        #Just using base ENI count, not caring about having ENI trunking turned on
        client = boto3.client('ec2')
        response = client.describe_instance_types(InstanceTypes=[self.ecs_instance_type])
        eni_per_instance = response['InstanceTypes'][0]['NetworkInfo']['MaximumNetworkInterfaces']

        number_of_instances = math.ceil((self.number_of_workers + 1) / (eni_per_instance-1))
        self.loadgen_cluster.add_capacity("AsgSpot",
                                          max_capacity=number_of_instances * 2,
                                          min_capacity=number_of_instances,
                                          instance_type=ec2.InstanceType(self.ecs_instance_type),
                                          spot_price="0.07",
                                          spot_instance_draining=True
                                         )
        #cloudmap for service discovery so workers can lookup mast via dns
        self.loadgen_cluster.add_default_cloud_map_namespace(name = self.cloudmap_namespace)

        #Create a graph widget to track reservation metrics for our cluster
        ecs_widget = cw.GraphWidget(
            left=[self.loadgen_cluster.metric_cpu_reservation()],
            right=[self.loadgen_cluster.metric_memory_reservation()],
            title="ECS - CPU and Memory Reservation",
            )

        #CloudWatch dashboard to monitor our stuff
        self.dashboard = cw.Dashboard(self, "Locustdashboard")
        self.dashboard.add_widgets(ecs_widget)

        if not self.distributed_locust:
            role = "standalone"
            locustContainer(self, "locust" + role, self.vpc, self.loadgen_cluster, role, self.target_url)
        else:
            role = "master"
            master_construct = locustContainer(self, "locust" + role, self.vpc,
                                               self.loadgen_cluster, role, self.target_url)

            lb_widget = cw.GraphWidget(
                left=[master_construct.lb.metric_active_connection_count(),
                      master_construct.lb.metric_target_response_time()],
                right=[master_construct.lb.metric_request_count()],
                title="Load Balancer")

            self.dashboard.add_widgets(lb_widget)

            role = "worker"
            worker_construct = locustContainer(self, "locust" + role, self.vpc,
                                               self.loadgen_cluster, role, self.target_url,
                                               self.number_of_workers)
            worker_construct.node.add_dependency(master_construct)

    def get_cdk_context(self):
    # grab stuff from context

        self.number_of_workers = int(self.node.try_get_context("number_of_workers"))
        self.ecs_instance_type = self.node.try_get_context("ecs_instance_type")
        self.vpc_cidr = self.node.try_get_context("vpc_cidr")
        self.distributed_locust = self.node.try_get_context("distributed_locust")
        self.cloudmap_namespace = self.node.try_get_context("cloudmap_namespace")
        self.target_url = self.node.try_get_context("target_url")


n = map(float, input().split(' '))

counts = {}

for x in n:
    if x in counts:
        counts[x] += 1
    else:
        counts[x] = 1

for x in sorted(counts):
    print("{} -> {} times".format(x, counts[x]))

02. Lists and Dictionaries/Dictionaries/02.Count_Real_Numbers.py


# -*- coding: utf-8 -*-
import nengo
import numpy as np
from nengo_ssp.vector_generation import HexagonalBasis, GridCellEncoders
#from nengo_ssp.utils import ssp_vectorized

class PathIntegrator(nengo.Network):
    def __init__(self, n_neurons, n_gridcells, scale_fac=1.0, basis=None,xy_rad=10, **kwargs):
        kwargs.setdefault("label", "PathIntegrator")
        super().__init__(**kwargs)

        if basis is None:
            X, Y, myK = HexagonalBasis(5,5)
            d = X.v.shape[0]
        else:
            X = basis[0]
            Y = basis[1]
            d = X.v.shape[0]
            myK = np.vstack([np.angle(np.fft.fftshift(np.fft.fft(X.v)))[0:d//2],
                             np.angle(np.fft.fftshift(np.fft.fft(Y.v)))[0:d//2]]).T

        n_oscs = d//2
        real_ids = np.arange(1,n_oscs*3,3)
        imag_ids = np.arange(2,n_oscs*3,3)
        S_ids = np.zeros(n_oscs*2 + 1, dtype=int)
        S_ids[0:d//2] = real_ids
        S_ids[d//2:(n_oscs*2)] = imag_ids
        S_ids[-1] = n_oscs*3
        i_S_ids = np.argsort(S_ids)

        G_encoders,G_sorts = GridCellEncoders(n_gridcells, X,Y, xy_rad)

        taus = 0.1*np.ones(n_oscs)

        with self:
            to_SSP = self.get_to_SSP_mat(d)
            i_to_SSP = self.get_from_SSP_mat(d)

            self.input_vel = nengo.Node(size_in=2, label="input_vel")
            self.input_SSP = nengo.Node(size_in=d, label="input_initial_SSP")
            #self.input_FSSP = nengo.Node(size_in=d-1, label="input_initial_FSSP")
            #nengo.Node(self.input_SSP, self.input_FSSP, )

            self.output = nengo.Node(size_in=d, label="output")

            self.velocity = nengo.Ensemble(n_neurons, dimensions=2,label='velocity')
            zero_freq_term = nengo.Node([1,0,0])

            self.osc = nengo.networks.EnsembleArray(n_neurons, n_oscs + 1,
                                                    ens_dimensions = 3,radius=np.sqrt(3), label="osc")
            self.osc.output.output = lambda t, x: x
            self.grid_cells = nengo.Ensemble(n_gridcells, dimensions=d, encoders = G_encoders,
                                        radius=np.sqrt(2), label="grid_cells")

            def feedback(x, tau):
                w = x[0]/scale_fac
                r = np.maximum(np.sqrt(x[1]**2 + x[2]**2), 1e-5)
                dx1 = x[1]*(1-r**2)/r - x[2]*w
                dx2 = x[2]*(1-r**2)/r + x[1]*w
                return 0, tau*dx1 + x[1], tau*dx2 + x[2]

            nengo.Connection(self.input_vel, self.velocity, transform = scale_fac)
            for i in np.arange(n_oscs):
                nengo.Connection(self.velocity, self.osc.ea_ensembles[i][0], transform = myK[i,:].reshape(1,-1),
                                 synapse=taus[i])

                S_back_mat = i_to_SSP[i_S_ids[2*i:(2*i+2)],:]

                nengo.Connection(self.input_SSP, self.osc.ea_ensembles[i][1:], transform=S_back_mat) #initialize

                nengo.Connection(self.osc.ea_ensembles[i], self.osc.ea_ensembles[i],
                                 function= lambda x: feedback(x, taus[i]),
                                 synapse=taus[i])

                #nengo.Connection(self.grid_cells, self.osc.ea_ensembles[i][1:], transform=S_back_mat, synapse=taus[i])

            nengo.Connection(zero_freq_term, self.osc.ea_ensembles[-1])
            nengo.Connection(self.osc.output[S_ids], self.grid_cells, transform = to_SSP, synapse=taus[0])

            #nengo.Connection(self.input_initial_SSP, self.grid_cells)

            nengo.Connection(self.grid_cells, self.output)

    def get_to_SSP_mat(self,D):
        W = np.fft.ifft(np.eye(D))
        W1 = W.real @ np.fft.ifftshift(np.eye(D),axes=0)
        W2 = W.imag @ np.fft.ifftshift(np.eye(D),axes=0)
        shiftmat1 = np.vstack([np.eye(D//2), np.zeros((1,D//2)), np.flip(np.eye(D//2), axis=0)])
        shiftmat2 = np.vstack([np.eye(D//2), np.zeros((1,D//2)), -np.flip(np.eye(D//2), axis=0)])
        shiftmat = np.vstack([ np.hstack([shiftmat1, np.zeros(shiftmat2.shape)]),
                              np.hstack([np.zeros(shiftmat2.shape), shiftmat2])])
        shiftmat = np.hstack([shiftmat, np.zeros((shiftmat.shape[0],1))])
        shiftmat[D//2,-1] = 1
        tr = np.hstack([W1, -W2]) @ shiftmat

        return tr

    def get_from_SSP_mat(self,D):
        W = np.fft.fft(np.eye(D))
        W1 = np.fft.fftshift(np.eye(D),axes=0) @ W.real
        W2 = np.fft.fftshift(np.eye(D),axes=0) @ W.imag
        shiftmat1 = np.hstack([np.eye(D//2), np.zeros((D//2, 2*(D//2) + D//2 + 2))])
        shiftmat2 = np.hstack([np.zeros((D//2, 2*(D//2) + 1)), np.eye(D//2), np.zeros((D//2, D//2 + 1))])
        shiftmat = np.vstack([ shiftmat1,shiftmat2])
        tr = shiftmat @ np.vstack([W1, W2])
        return tr


from __future__ import annotations

import dataclasses
from typing import TYPE_CHECKING

from randovania.bitpacking import bitpacking
from randovania.bitpacking.bitpacking import BitPackDecoder, BitPackValue
from randovania.game_description import default_database

if TYPE_CHECKING:
    from collections.abc import Iterator

    from randovania.game_description.pickup.ammo_pickup import AmmoPickupDefinition

@dataclasses.dataclass(frozen=True)
class AmmoPickupState(BitPackValue):
    ammo_count: tuple[int, ...] = (0,)
    pickup_count: int = 0
    requires_main_item: bool = True

    def check_consistency(self, ammo: AmmoPickupDefinition):
        db = default_database.resource_database_for(ammo.game)

        if len(self.ammo_count) != len(ammo.items):
            raise ValueError(f"Ammo state has {len(self.ammo_count)} ammo counts, expected {len(ammo.items)}")

        for count, ammo_name in zip(self.ammo_count, ammo.items):
            ammo_item = db.get_item(ammo_name)
            minimum_count = -ammo_item.max_capacity if ammo.allows_negative else 0
            if not (minimum_count <= count <= ammo_item.max_capacity):
                raise ValueError(
                    f"Ammo count for item {ammo_name} of value {count} is not "
                    f"in range [{minimum_count}, {ammo_item.max_capacity}]."
                )

        if self.pickup_count < 0:
            raise ValueError(f"Pickup count must be at least 0, got {self.pickup_count}")

    def bit_pack_encode(self, metadata) -> Iterator[tuple[int, int]]:
        ammo: AmmoPickupDefinition = metadata["ammo"]
        db = default_database.resource_database_for(ammo.game)

        for count, ammo_name in zip(self.ammo_count, ammo.items):
            ammo_item = db.get_item(ammo_name)
            yield from bitpacking.encode_int_with_limits(
                abs(count),
                (ammo_item.max_capacity // 2, ammo_item.max_capacity + 1),
            )
            if ammo.allows_negative:
                yield from bitpacking.encode_bool(count < 0)  # Negative?

        yield from bitpacking.encode_big_int(self.pickup_count)
        if ammo.unlocked_by is not None:
            yield from bitpacking.encode_bool(self.requires_main_item)

    @classmethod
    def bit_pack_unpack(cls, decoder: BitPackDecoder, metadata) -> AmmoPickupState:
        ammo: AmmoPickupDefinition = metadata["ammo"]
        db = default_database.resource_database_for(ammo.game)

        # Ammo Count
        ammo_count = []
        for ammo_name in ammo.items:
            ammo_item = db.get_item(ammo_name)
            count = bitpacking.decode_int_with_limits(
                decoder,
                (ammo_item.max_capacity // 2, ammo_item.max_capacity + 1),
            )
            if ammo.allows_negative and bitpacking.decode_bool(decoder):  # Negative?
                count *= -1
            ammo_count.append(count)

        # Pickup Count
        pickup_count = bitpacking.decode_big_int(decoder)

        # Require Main Item
        requires_main_item = True
        if ammo.unlocked_by is not None:
            requires_main_item = bitpacking.decode_bool(decoder)

        return cls(
            ammo_count=tuple(ammo_count),
            pickup_count=pickup_count,
            requires_main_item=requires_main_item,
        )

    @property
    def as_json(self) -> dict:
        result: dict = {}

        for field in dataclasses.fields(self):
            value = getattr(self, field.name)
            result[field.name] = value

        result["ammo_count"] = list(result["ammo_count"])

        return result

    @classmethod
    def from_json(cls, value: dict) -> AmmoPickupState:
        kwargs = {}

        for field in dataclasses.fields(cls):
            if field.name in value:
                kwargs[field.name] = value[field.name]

        if "ammo_count" in kwargs:
            kwargs["ammo_count"] = tuple(kwargs["ammo_count"])

        return cls(**kwargs)


#-*-python-*-
from warn import *
from Rnaseq import *
from Rnaseq.command import *

from sqlalchemy import *
from sqlalchemy.orm import mapper, sessionmaker

# usage: provenance load <readset pipeline>
# This is sort of a test command, probably won't be used in production

class Load(Command):
    def description(self):
        return "load a pipeline and quit (debugging tool)"

    def usage(self):
        return "usage: load -p <pipeline> -r <readset>"

    def run(self, *argv, **args):
        try:
            config=args['config']
            readset_file=config['readset_file']
            pipeline_name=config['pipeline_name']
        except KeyError as e:
            raise MissingArgError(str(e))

        # have to create session before creating any objects that session adds, etc:

        readset=Readset(name=readset_file).load()
        pipeline=Pipeline(name=pipeline_name, readset=readset)
        pipeline.update(RnaseqGlobals.config)
        pipeline.description='desc for juan'

        session=RnaseqGlobals.get_session()
        session.add(pipeline)
        session.commit()

#print __file__, "checking in"