import sys
import os
import datetime
import subprocess

def time_parse(text):
    for fmt in ("%S", "%M:%S", "%H:%M:%S"):
        try:
            return datetime.datetime.strptime(text, fmt)
        except ValueError:
            pass
    return (-1)

print ("Video Split")
print ("===========")
# Check to see if the input filename is passed
if (len(sys.argv) !=  2):
    print ("ERROR: No input filename specified")
    filename = input("Enter the name of the input filename --> ")
else:
    filename=sys.argv[1]

# Open up the input filename
try:
    file_object = open(filename,"r")
except IOError as e:
    print("Error: "+str(e))
    exit(-1)

# Read the first line.   This line should contain the input video filename
fn=file_object.readline().replace("\n","")

# Split up the input filename into both an extension and filename prefix.
filename = fn.rsplit('.', 1)[0]
extension = fn.rsplit('.', 1)[1]

if not os.path.exists(fn):
    print(f"Error: Can't open {fn}")
    exit(-1)

# Counter is used for the new file name
counter = 1

# Process each line in the file
for line in file_object:

    cleaned = line.replace("\n","")
    # Remove the newline character, and split the file into both fields (Start Time and End Time)
    words = cleaned.split()

    if len(words)<2:
        print(f"WARNING: Ignoring Line '{cleaned}', Doesn't contain time stamps.")

    else:
        if len(words) >=3:
            output_filename=f"{words[2]}.{extension}"
        else:
            output_filename=f"{filename}-part-{str(counter)}.{extension}"
            counter += 1
        print ("----------------------------------")
        print (f"Splitting {fn} between: {words[0]} and {words[1]} into {output_filename}")

        # parse the time frames to determine the number of seconds between both times
        # using the seconds value is better in the ffmpeg command
        begtime = time_parse(words[0])
        endtime = time_parse(words[1])
        if begtime == -1 or endtime == -1:
            print (f"WARNING: Ignoring '{cleaned}', contains an invalid timestamp")
        else:
            diff = (endtime-begtime).total_seconds()

            print(f"{endtime} - {begtime} = {diff}")

            # This is the original command line
            #command="ffmpeg -loglevel warning -i "+ fn+" -vcodec copy -acodec copy -ss "+words[0]+" -to "+words[1]+" "+output_filename

            # This is the updated command that removes the black screen at the begining of the split files
            # The only way to solve it is to use the -ss before the -i and then the -t to specify the seconds of the copy
            command=f"ffmpeg -loglevel warning -ss {words[0]} -i {fn} -t {diff} -vcodec copy -acodec copy {output_filename}"

            print(f"'{command}'")

            # Execute the command

#            os.system(command)
            try:
                subprocess.run(command,shell=True,check=True)
            except subprocess.CalledProcessError as e:
                print("Error: " + str(e))

# Close the file
file_object.close()


import torch
from torch import nn, Tensor

class ROIPool(nn.Module):
    def __init__(self,
                 feature_map: Tensor,
                 rois: Tensor,
                 output_size
                 ):
        """
        :param feature_map: Tensor. Shape:[N, C, H, W]
        :param rois: Tensor. Shape:[?, 5], 5代表图片index, x1,y1,x2,y2
        :param output_size:
        """
        super(ROIPool, self).__init__()
        self.feature_map = feature_map
        self.rois = rois
        self.output_size = output_size

    def forward(self):
        rois_list = []
        for index in range(self.feature_map.shape[0]):
            rois_list.append([])
        for roi in self.rois:
            rois_list[roi[0]].append(roi[1:])
        output_maps = []
        img_indexes = []
        for img_index, feature_map in enumerate(self.feature_map):
            if not rois_list[img_index]:
                continue
            else:
                for roi in rois_list[img_index]:
                    img_indexes.append(img_index)
                    x1, y1, x2, y2 = roi
                    crop = feature_map[:, y1:y2 + 1, x1:x2 + 1][None]
                    out = nn.AdaptiveAvgPool2d(self.output_size)(crop)
                    output_maps.append(out)
        output = torch.cat(output_maps, 0)
        return img_indexes, output

def generate_fake_rois():
    img_index = torch.randint(0, 15, (5, 1))
    rois_cord_x1y1 = torch.randint(1, 15, (5, 2))
    rois_cord_x2y2 = rois_cord_x1y1 + torch.randint(1, 15, (5, 2))
    rois = torch.cat([img_index, rois_cord_x1y1, rois_cord_x2y2], dim=1)
    return rois

if __name__ == '__main__':
    feature_map = torch.randn(16, 8, 32, 32)
    rois = generate_fake_rois()
    roipool = ROIPool(feature_map, rois, 3)
    img_indexes, output = roipool()
    print(img_indexes)
    print(output.shape)


#!/usr/bin/python3

"""Write a NumPy program to create a two-dimensional
array with shape (8,5) of random numbers. Select random numbers from a normal distribution (200,7)."""

import numpy as np

if __name__ == "__main__":
    # arr = np.random.randint(7,200,(8,5))
    # print("2-D array with shape (8,5) of random numbers from normal distribution (200,7):\n",arr)

    #Given in the site
    np.random.seed(20)
    cbrt = np.cbrt(7)
    nd1 = 200
    print(cbrt * np.random.randn(10, 4) + nd1)


import textwrap
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import random
from pathlib import Path
import glob
import sys

def get_fonts():
    fonts = []
    expressions = ["SF-Pro-Display*"]
    for e in expressions:
        fonts.extend(glob.glob("/library/Fonts/" + e))
    return fonts

LINE_LENGTH = 20
IMAGE_WIDTH, IMAGE_HEIGHT = (1000, 1000)
FONTS = get_fonts()

def make_image(text, filepath=None, custom_color=None, custom_font=None):

    """
    Creates a plain image based on text.
    filepath: str -> where the image is saved.
    if empty, won't save
    custom_color: tuple len 3 -> color in RGB
    if empty, random color
    custom_font: str -> path to font
    if empty, random font
    """

    def avg(t):
        return sum(t) / len(t)

    fnt_size = 500  # starting font size
    FNT_RATIO = 0.85
    text_width, text_height = tuple([x * 3 for x in [IMAGE_WIDTH, IMAGE_HEIGHT]])

    # get random colors
    bg_color = tuple(np.random.choice(range(256), size=3))
    if not custom_color:
        fg_color = "white" if avg(bg_color) < 128 else "black"
    else:
        fg_color = custom_color

    # get font family
    fnt_family = custom_font if custom_font else str(random.choice(FONTS))

    # scale text based on ratio
    while (
        text_width > FNT_RATIO * IMAGE_WIDTH or text_height > FNT_RATIO * IMAGE_HEIGHT
    ):

        img = Image.new("RGB", (IMAGE_WIDTH, IMAGE_HEIGHT), color=bg_color)
        fnt = ImageFont.truetype(fnt_family, fnt_size)
        text_canvas = ImageDraw.Draw(img)

        # calculate text size for scaling
        text_width, text_height = text_canvas.textsize(text, font=fnt)
        fnt_size -= 5

    # center text and write image
    X_POS = int((IMAGE_WIDTH - text_width) / 2)
    Y_POS = int((IMAGE_HEIGHT - text_height) / 2)
    text_canvas.text((X_POS, Y_POS), text, font=fnt, fill=fg_color)

    # save image
    saved = False
    if filepath:
        img.save(filepath)
        saved = True
    return img, saved

def demo():
    img, saved = make_image("This is some test text.")
    if not saved:
        img.save("img.png")

if __name__ == "__main__":
    if len(sys.argv) == 1:
        demo()
    elif len(sys.argv) == 2:
        make_image(sys.argv[-1], filepath="img.png")


from os import path, remove
from glob import glob

to_del = glob(path.join(path.expanduser('~'), '**/VBox.log*'), recursive=True)

if to_del:
	for f in to_del:
		remove(f)
		print(f'Removed {f}')
else:
	print("Nothing to remove!")


import imgaug as ia
from imgaug import augmenters as iaa
import cv2
import PIL
from PIL import Image
import numpy as np

def aug_seq():
    sometimes_5 = lambda aug: iaa.Sometimes(0.5, aug)
    sometimes_1 = lambda aug: iaa.Sometimes(0.1, aug)
    sometimes_8 = lambda aug: iaa.Sometimes(0.8, aug)
    sometimes_2 = lambda aug: iaa.Sometimes(0.2, aug)

    augmentations = iaa.Sequential([
        sometimes_2(iaa.CropAndPad(
            percent=(-0.02, 0.02),
            pad_mode=["edge"],
            pad_cval=(0, 255)
        )),
        iaa.Sequential([iaa.size.Resize(0.6), iaa.size.Resize(1 / 0.6)]),
        # sometimes_8(iaa.OneOf([iaa.Sequential([iaa.size.Scale(0.6),iaa.size.Scale(1/0.6)]),
        #                      iaa.Sequential([iaa.size.Scale(0.8),iaa.size.Scale(1/0.8)]),
        #                      iaa.Sequential([iaa.size.Scale(0.9),iaa.size.Scale(1/0.9)]),])),

        # This inverts completely inverts the text ( make black white etc.)
        sometimes_1(iaa.Invert(1, per_channel=True)),

        # This does some affine transformations
        sometimes_2(iaa.Affine(
            scale={"x": (0.8, 1), "y": (0.8, 1)},  # scale images to 80-120% of their size, individually per axis
            translate_percent={"x": (-0, 0), "y": (-0.1, 0.1)},  # translate by -20 to +20 percent (per axis)
            rotate=(-2, 2),  # rotate by -45 to +45 degrees
            shear=(-2, 2),  # shear by -16 to +16 degrees
            order=[0, 1],  # use nearest neighbour or bilinear interpolation (fast)
            cval=(0, 255),  # if mode is constant, use a cval between 0 and 255
            mode=["edge"]  # use any of scikit-image's warping modes (see 2nd image from the top for examples)
        )),
        sometimes_2(iaa.OneOf([
            iaa.GaussianBlur((0, 1.0)),  # blur images with a sigma between 0 and 3.0
            iaa.AverageBlur(k=(2, 3)),  # blur image using local means with kernel sizes between 2 and 7
            # iaa.MedianBlur(k=(3, 5)), # blur image using local medians with kernel sizes between 2 and 7
        ])),
        sometimes_2(iaa.Add((-10, 10), per_channel=0.5)),
        # change brightness of images (by -10 to 10 of original value)
        sometimes_8(iaa.AddToHueAndSaturation((-200, 200))),  # change hue and saturation
        sometimes_5(iaa.contrast.LinearContrast((0.8, 5), per_channel=0.5)),  # improve or worsen the contrast
        sometimes_1(iaa.ElasticTransformation(alpha=(0, 0.5), sigma=0.2)),
        # move pixels locally around (with random strengths)
        # sometimes_1(iaa.PiecewiseAffine(scale=(0.001, 0.005))),
        sometimes_1(iaa.PiecewiseAffine(scale=(0.004, 0.008))),
    ])
    return augmentations

augmentations = aug_seq()

def mix_aug(image):
    flag = True
    if isinstance(image, tuple):
        if isinstance(image[0],PIL.Image.Image):
            image = tuple(map(lambda x:np.array(x), image))
        else:
            image = tuple(map(lambda x:cv2.cvtColor(x, cv2.COLOR_BGR2RGB), image))
            flag = False
        aug_img = augmentations(images=image)
        if flag:
            aug_img = tuple(map(lambda x: Image.fromarray(x), aug_img))
        else:
            aug_img = tuple(map(lambda x: cv2.cvtColor(x, cv2.COLOR_RGB2BGR), aug_img))
    else:
        if isinstance(image,PIL.Image.Image):
            image = np.array(image)
        else:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            flag = False
        aug_img = augmentations(image=image)
        if flag:
            aug_img = Image.fromarray(aug_img)
        else:
            aug_img = cv2.cvtColor(aug_img, cv2.COLOR_RGB2BGR)
    return aug_img

if __name__ == "__main__":
    img_path = r'/workspace/code/1_work/tianchi_intel/two_stage/Recognition/demo_image/demo_10.jpg'
    img = Image.open(img_path)
    images = (img, img)
    res = mix_aug(images)
    ia.imshow(np.array(res[0]))


def bresenham_line(origin, pos):
    line = []

    x0, y0 = origin[0], origin[1]
    x1, y1 = pos[0], pos[1]

    dx = abs(x1 - x0)
    dy = abs(y1 - y0)

    x, y = x0, y0
    sx = -1 if x0 > x1 else 1
    sy = -1 if y0 > y1 else 1

    if dx > dy:
        err = dx / 2.0
        while x != x1:
            line.append((x, y))
            err -= dy
            if err < 0:
                y += sy
                err += dx
            x += sx
    else:
        err = dy / 2.0
        while y != y1:
            line.append((x, y))
            err -= dx
            if err < 0:
                x += sx
                err += dy
            y += sy
    line.append((x, y))

    return line

def ellipse(origin, end):
    path = []

    mid = (round((end[0]-origin[0])/2), round((end[1]-origin[1])/2))

    rx, ry = abs(mid[0]), abs(mid[1])
    xc, yc = origin[0] + mid[0], origin[1] + mid[1]
    x = 0
    y = ry

    if ry == 0:
        left = min(origin[0], end[0])
        right = max(origin[0], end[0])
        for i in range(left, right):
            path.append((i, origin[1] + mid[1]))
    else:
        d1 = ((ry * ry) - (rx * rx * ry) + (0.25 * rx * rx))
        dx = 2 * ry * ry * x
        dy = 2 * rx * rx * y

        while (dx < dy):
            path.append((x + xc, y + yc))
            path.append((-x + xc, y + yc))
            path.append((x + xc, -y + yc))
            path.append((-x + xc, -y + yc))

            if (d1 < 0):
                x += 1
                dx = dx + (2 * ry * ry)
                d1 = d1 + dx + (ry * ry)
            else:
                x += 1
                y -= 1
                dx = dx + (2 * ry * ry)
                dy = dy - (2 * rx * rx)
                d1 = d1 + dx - dy + (ry * ry)

        d2 = (((ry * ry) * ((x + 0.5) * (x + 0.5))) + ((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry))

        while (y >= 0):
            path.append((x + xc, y + yc))
            path.append((-x + xc, y + yc))
            path.append((x + xc, -y + yc))
            path.append((-x + xc, -y + yc))

            if (d2 > 0):
                y -= 1
                dy = dy - (2 * rx * rx)
                d2 = d2 + (rx * rx) - dy
            else:
                y -= 1
                x += 1
                dx = dx + (2 * ry * ry)
                dy = dy - (2 * rx * rx)
                d2 = d2 + dx - dy + (rx * rx)

    return path

def flood_fill(origin, canvas):
    """
    "Paint bucket tool"
    """
    area = []

    posList = []                                                    # a list for possible fillable positions
    canvasCopy = canvas.copy()
    posList.append((origin[0], len(canvasCopy) - 1 - origin[1]))    # add the clicked position to the list
    prevColor = canvasCopy[origin[1]][origin[0]]                    # check the original color that will be recolored
    newColor = [x+1 for x in prevColor]

    while posList:
        pos = posList.pop()   # take one position out of the list
        if canvasCopy[pos[1]][pos[0]] == prevColor:
            area.append((pos[0], len(canvasCopy) - 1 - pos[1]))
            canvasCopy[pos[1]][pos[0]] = newColor

            # check pos from upside
            x, y = pos[0], pos[1] - 1
            if y >= 0 and x >= 0 and y < len(canvasCopy) and x < len(canvasCopy[pos[1]]):
                if canvasCopy[y][x] == prevColor:
                    posList.append((x, y))

            # check pos from downside
            y = pos[1] + 1
            if y >= 0 and x >= 0 and y < len(canvasCopy) and x < len(canvasCopy[pos[1]]):
                if canvasCopy[y][x] == prevColor:
                    posList.append((x, y))

            # check pos from left side
            x, y = pos[0] - 1, pos[1]
            if y >= 0 and x >= 0 and y < len(canvasCopy) and x < len(canvasCopy[pos[1]]):
                if canvasCopy[y][x] == prevColor:
                    posList.append((x, y))

            # check pos from right side
            x = pos[0] + 1
            if y >= 0 and x >= 0 and y < len(canvasCopy) and x < len(canvasCopy[pos[1]]):
                if canvasCopy[y][x] == prevColor:
                    posList.append((x, y))

    return area

def rectangle(origin, end):
    path = []

    corners = [(origin[0], origin[1]), (end[0], end[1]), (origin[0], end[1]), (end[0], origin[1])]

    x0 = min(corners, key = lambda t: t[0])[0]
    y0 = max(corners, key = lambda t: t[1])[1]

    x1 = max(corners, key = lambda t: t[0])[0]
    y1 = min(corners, key = lambda t: t[1])[1]

    for i in range(x0, x1+1):
        path.append((i, y0))
        path.append((i, y1))

    for j in range(y1+1, y0):
        path.append((x0, j))
        path.append((x1, j))

    return path


from SqlHelpter import *
#conding-utf8
import re
import urllib.request
import urllib
import time;  # 引入time模块

ticks = time.time()
url='http://office.zzuli.edu.cn'

def getHtml(url):
    page=urllib.request.urlopen(url)
    html=page.read()
    return html.decode('utf-8')
def getDSJ(html):
    reg=r"(.*?)"
    lsre=re.compile(reg)
    ls=re.finditer(lsre,html)
    return ls

html=getHtml(url)
ls=getDSJ(html)
fd=open(r'theBigDate2.txt','a+')
print("ID"+"    "+"名字")
x=0
sql=SqlHelpter()
reghtml=r"^http://"
murl=re.compile(reghtml)
for item in ls:
	try:
		urls=item.group(1).replace("'","")
		title=item.group(3).replace("'","")
		if murl.match(urls):
			print("")
		else:
			urls=url+urls
		fd.write(title+"  --  "+urls+"\n")
		x=x+1
		sql.insert(title,urls)
		print(title+"    "+urls)
	except Exception :
		print("发生异常")
fd.close()
print("总共有"+str(x)+"个")


import pandas as pd
import glob2
import os

PATH_VAL = '..\\Data\\dicom-images-test\\'
SUBCSV = '..\\out\\submission.csv'

val = glob2.glob(os.path.join(PATH_VAL,'**/*.dcm'))

ids = []
rles = []

for f in val:
    id = f.split('\\')[-1][:-4]
    rle = ''
    ids.append(id)
    rles.append(rle)

sub_df = pd.DataFrame({'ImageId': ids, 'EncodedPixels': rles})
sub_df.loc[sub_df.EncodedPixels=='', 'EncodedPixels'] = '-1'
sub_df.head()

sub_df.to_csv(SUBCSV, index=False)


import threading
import pickle
from util import Uri, IP, PORT
import sys, os
from queue import Queue
import traceback
from multiprocessing.connection import Listener

connections = []
q_map = Queue()
q_reduce = Queue()

def endpoints(data: Uri):
    '''
        This function acts as a router, routing all endpoints to specific data or backend operation needed.

        Returns { "uri": endpoint requested, "data": data needed }

        Parameters
        ----------
        data: { "uri": endpoint requested, "data": data needed for request }

        Returns
        --------
        data: { "uri": endpoint requested, "data": data needed }
    '''
    global q_map
    global q_reduce

    if data["uri"] == Uri.GET_MAP:
        data = q_map.retrieve()
        data = {"uri": Uri.GET_MAP, "data": data}
        return pickle.dumps(data)

    if data["uri"] == Uri.GET_REDUCE:
        data = q_reduce.retrieve()
        data = {"uri": Uri.GET_REDUCE, "data": data}
        return pickle.dumps(data)

    if data["uri"] == Uri.FINISHED_MAP:
        q_map.finished_work()
        data = {"uri": Uri.FINISHED_MAP, "data": None}
        return pickle.dumps(data)

    if data["uri"] == Uri.FINISHED_REDUCE:
        q_reduce.finished_work()
        data = {"uri": Uri.FINISHED_REDUCE, "data": None}
        return pickle.dumps(data)

    if data["uri"] == Uri.UNFINISHED_MAP:
        q_map.requeue(data["data"])
        data = {"uri": Uri.UNFINISHED_MAP, "data": None}
        return pickle.dumps(data)

    if data["uri"] == Uri.UNFINISHED_REDUCE:
        q_reduce.requeue(data["data"])
        data = {"uri": Uri.UNFINISHED_REDUCE, "data": None}
        return pickle.dumps(data)

    if data["uri"] == Uri.STATUS_MAP:
        data = {"uri": Uri.STATUS_MAP, "data": q_map.is_finished_jobs()}
        return pickle.dumps(data)

    if data["uri"] == Uri.STATUS_REDUCE:
        data = {"uri": Uri.STATUS_REDUCE, "data": q_reduce.is_finished_jobs(), "map": q_map.is_finished_jobs()}
        return pickle.dumps(data)

def handler(c, endpoints):
    global connections
    while True:
        try:
            data = c.recv()
            data = pickle.loads(data)
            packet = endpoints(data)
            c.send(packet)

        except EOFError as error:
            error = traceback.format_exc()
            if len(connections) == 0:
                os._exit(0)
            if c in connections:
                connections.remove(c)

def insert_reduce_tasks(m: int) -> None:
    '''
        This function inserts reduce task into the queue.

        Returns None

        Parameters
        ----------
        index: specific number of reduce to work on

        Returns
        --------
        None
    '''
    global q_reduce
    m_int = int(m)

    for i in range(m_int):
        data = {"index": i}
        q_reduce.add(data)
        print(" [x] Queued data for reduce")

def insert_map_tasks(file_name: str, n: str, m: str) -> None:
    '''
        This function inserts map task into the queue.

        Returns None

        Parameters
        ----------
        file_name: the name of the file to process
        n: number of possible maps
        m: number of possible reduces

        Notes
        -------------------
        This implementation has the possibility of miscounting n words if it cuts the file on these words.
        One solution could be to read the file and make sure allocated partition number is a non character string.
        However this is not a great idea because it requires holding the http connection whilst reading the file.
        A better solution might be to get the first worker to make sure its last data is a alphabetical character (greedy), get the intermediate
        workers to maker sure its first data works back to the first non alphabetical char(non greedy) and last one as above.
        Then finally the last worker will have to check only its start point.
        The idea for using this approach was to make the workers totally independent of each other.

        Returns
        --------
        None
    '''
    global q_map
    try:
        f = open(file_name, "r")
        size = os.path.getsize(file_name)
        int_n = int(n)
        int_m = int(m) - 1
        partition_size = (size // int_n)
        for i in range(int_n):
            data = {"file_name": file_name, "i": i, "m": int_m, "partition_size": partition_size}
            q_map.add(data)
            print(f" [x] Queued this data for map: {data}")

    except:
        error = traceback.format_exc()
        print(error)

    finally:
        f.close()

def main(file_name: str, n: str, m: str) -> None:
    sock = Listener((IP, PORT))

    insert_map_tasks(file_name, n, m)
    insert_reduce_tasks(m)

    while True:
        c = sock.accept()
        Thread = threading.Thread(target=handler, args=(c, endpoints))
        Thread.daemon = True
        Thread.start()
        connections.append(c)
        print(connections)
        if q_reduce.is_finished_jobs():
            print("finished all jobs")
            sys.exit(0)

if __name__ == "__main__":
    try:
        file_name = sys.argv[1]
        n = sys.argv[2]
        m = sys.argv[3]
    except:
        print("Please call the file in this way: 'python simple_server file_name n m'")
    main(file_name, n, m)


import sys
from pathlib import Path
import unittest
from os import path
sys.path.append("..")
import Main

Windows = False
MusPath = fr"{Path.home()}/Music"
if sys.platform == 'Windows' or sys.platform == 'win32':
    Windows = True
    MusPath = fr"{Path.home()}\Music".replace("\\", "\\\\")

class TestMain(unittest.TestCase):
    def test_get_param(self):
        param = Main.get_param()
        self.assertEqual(param["apiKeys"][0], "", 'get_param is broken')

    def test_add_api_key(self):
        Main.add_api_key("test")
        param = Main.get_param()
        self.assertEqual(param["apiKeys"][0], "test", 'get_param or add_api_key is broken')
        Main.remove_api_key()

    def test_remove_api_key(self):
        Main.add_api_key("test")
        Main.remove_api_key()
        param = Main.get_param()
        self.assertEqual(param["apiKeys"][0], "", 'get_param or remove_api_key is broken')

    def test_get_yt_vid(self):
        highest, yt = Main.get_yt_vid("https://music.youtube.com/watch?v=nNms5rOaGlk", MusPath)
        self.assertEqual(highest[1], 160, 'get_yt_vid got wrong high')

        if Windows:
            self.assertTrue(path.exists(fr"{MusPath}\\temp.webm"), "get_yt_vid did not create temp.webm")
        else:
            self.assertTrue(path.exists(fr"{MusPath}/temp.webm"), "get_yt_vid did not create temp.webm")

if __name__ == "__main__":
    unittest.main()


from math import radians, pi, tan, log, cos, pow, sin

def conversion(lon, lat):
    grid = 5.0  # 격자 간격(km)
    re = 6371.00877/grid  # 지도 반경
    slat1 = 30.0 * radians(1)  # 표준 위도1
    slat2 = 60.0 * radians(1)  # 표준 위도2
    olon = 126.0 * radians(1)  # 기준점 경도
    olat = 38.0 * radians(1)  # 기준점 위도
    xo = 210 / grid  # 기준점 X좌표
    yo = 675 / grid  # 기준점 Y좌표

    sn = tan(pi * 0.25 + slat2 * 0.5)/tan(pi * 0.25 + slat1 * 0.5)
    sn = log(cos(slat1)/cos(slat2))/log(sn)
    sf = tan(pi * 0.25 + slat1 * 0.5)
    sf = pow(sf, sn) * cos(slat1)/sn
    ro = tan(pi * 0.25 + olat * 0.5)
    ro = re*sf/pow(ro, sn)

    ra = tan(pi * 0.25 + lat * radians(1) * 0.5)
    ra = re*sf/pow(ra, sn)
    theta = lon * radians(1) - olon
    if theta > pi:
        theta -= 2.0 * pi
    if theta < -pi:
        theta += 2.0 * pi
    theta *= sn
    x = ra * sin(theta) + xo
    y = ro - ra * cos(theta) + yo

    return [round(x) + 1, round(y) + 1]


from keras.layers import Dense, Dropout, CuDNNLSTM, Bidirectional
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import csv

reader = csv.reader(open('Dataset.csv', 'r'))
scaler = MinMaxScaler()
dataset = []

target = []
for r in reader:

    for i, c in enumerate(r):
        r[i] = eval(c)

    minimum_length = 16
    length = len(r) - 1
    if length > minimum_length:
        target.append(r.pop(-1))

        scaler.fit(r)
        r = scaler.transform(r)

        extra = length - minimum_length
        offset = extra // 2
        r = r[offset:minimum_length + offset + 1]

        dataset.append(r)

dataset = np.array(dataset)
target = np.array(target)

x_train, x_test, y_train, y_test = train_test_split(dataset, target, test_size=0.2, random_state=4)

model = Sequential()

model.add(Bidirectional(CuDNNLSTM(128, return_sequences=True), input_shape=x_train.shape[1:]))
model.add(Bidirectional(CuDNNLSTM(64)))

model.add(Dense(254, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss=tf.keras.metrics.binary_crossentropy, optimizer='adam', metrics=['accuracy'])

history = model.fit(x_train, y_train, epochs=50, batch_size=30, validation_data=(x_test, y_test))

plt.subplot(121)
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()

plt.subplot(122)
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()

model.save('FaintingDetector.h5')


import hashlib
p11=input('Введите пароль 1 : ')
p22=input('Повторите пароль : ')
p1=hashlib.md5(p11.encode())
p2=hashlib.md5(p22.encode())
print(p1)
print(p2)
if p1.hexdigest()==p2.hexdigest(): print('Все ок!')
else:
     print("Упс!")

Book/проверка_пароля.py

проверка_пароля.py


import torch
from torch import nn
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from torch.utils.data import random_split
import numpy as np
from sklearn.preprocessing import OneHotEncoder

class CustomDataset(Dataset):
    def __init__(self, inF, CH=[]):
        self.X = []
        self.y = []
        with open(inF) as inFile:
            head = inFile.readline()
            for line in inFile:
                line = line.strip()
                fields = line.split('\t')
                ch = fields[1]
                if CH:
                    if ch in CH:
                        self.X.append(fields[-2])
                        self.y.append(fields[-1])
                else:
                    self.X.append(fields[-2])
                    self.y.append(fields[-1])
    def __len__(self):
        return len(self.y)

    def __getitem__(self, idx):
        X = self.X[idx]
        y = self.y[idx]
        X = self.OneHot(list(X))
        y = self.OneHot(list(y), alphabet=['0', '1', '2'])
        X = torch.tensor(X, dtype=torch.float32)
        y = torch.tensor(y, dtype=torch.float32)
        return(X, y)

    def OneHot(self, L, alphabet=['A', 'C', 'G', 'T']):
        cat = list(np.array(sorted(alphabet)).reshape(1, -1))
        oe = OneHotEncoder(categories=cat, handle_unknown='ignore')
        s = oe.fit_transform(np.array(L).reshape(-1, 1)).toarray().transpose(1, 0)
        return(s)

train_chrs = ['chr11', 'chr13', 'chr15', 'chr17', 'chr19', 'chr21', 'chr2', 'chr4', 'chr6', 'chr8', 'chr10', 'chr12', 'chr14', 'chr16', 'chr18', 'chr20', 'chr22', 'chrX', 'chrY']
val_chrs = ['chr1', 'chr3', 'chr5', 'chr7', 'chr9']
#inFs = ['Homo_sapiens.GRCh38.110.bed12_seq_nt5000_flank40_head100.txt', 'Homo_sapiens.GRCh38.110.bed12_seq_nt5000_flank40.txt', 'Homo_sapiens.GRCh38.110.bed12_seq_nt5000_flank5000.txt']
inFs = ['Homo_sapiens.GRCh38.110.bed12_seq_nt5000_flank40_ht100.txt']

if __name__ == '__main__':
    for inF in inFs:
        ouF1 = inF.split('.txt')[0] + '_train.pth'
        ouF2 = inF.split('.txt')[0] + '_val.pth'

        ds_train = CustomDataset(inF, train_chrs)
        ds_val = CustomDataset(inF, val_chrs)

        torch.save(ds_train, ouF1)
        torch.save(ds_val, ouF2)


#Importation des librairies

import pandas as pd
import numpy as np
import plotly.express as px
import streamlit as st
from PIL import Image
import base64
import plotly.graph_objects as go

# Config of the page of our dashboard
st.set_page_config(
    page_title="Euroleague dashboard",
    page_icon="🏀",
    layout = "wide",

)

#Import data
#Import the global data
data = pd.read_csv("sportsref_download.xls.csv", sep = ";")
data = data.fillna(0)

#Import the data of the match
data_of_the_match = pd.read_csv("last_match.csv", sep = ";")

player_photo = pd.read_csv("player_photo.csv", sep = ";")

st.markdown(
    """

Stats analysis of Monaco team players against Partizan belgrade

    """,
    unsafe_allow_html=True,
)

st.divider()

col1, col2, col3 = st.columns(3)

with col2:

    l1_col1, l1_col2, l1_col3 = st.columns([1, 3, 1])

    # Add elements to the columns
    with l1_col2:

        player_select = st.selectbox("Player name :", (data_of_the_match.Player.values))
        st.markdown(f'

', unsafe_allow_html=True)

    with l1_col1:
        st.write("")

    with l1_col3:
        st.write("")

    st.divider()

    l2_col21, l2_col22, l2_col23 = st.columns([2, 3, 2])
    with l2_col21:
        st.write("
For this match
", unsafe_allow_html=True)
        minute_played = data_of_the_match.MP.loc[data_of_the_match["Player"] == player_select].values[0]
        st.write(f"
{minute_played}
", unsafe_allow_html=True)
        pt = data_of_the_match.PTS.loc[data_of_the_match["Player"] == player_select].values[0]
        st.write(f"
{pt}
", unsafe_allow_html=True)
        pf = data_of_the_match.PF.loc[data_of_the_match["Player"] == player_select].values[0]
        st.write(f"
{pf}
", unsafe_allow_html=True)
    with l2_col22:
        st.write("
VS
", unsafe_allow_html=True)
        st.write("
Minute played
", unsafe_allow_html=True)
        st.write("
Points
", unsafe_allow_html=True)
        st.write("
Personnal foul
", unsafe_allow_html=True)
    with l2_col23:
        st.write("
Euroleague season
", unsafe_allow_html=True)
        av_minute_played = data.MP.loc[data["Player"] == player_select].values[0]
        st.write(f"
{av_minute_played}
", unsafe_allow_html=True)
        av_pt = data.PTS.loc[data["Player"] == player_select].values[0]
        st.write(f"
{av_pt}
", unsafe_allow_html=True)
        av_pf = data.PF.loc[data["Player"] == player_select].values[0]
        st.write(f"
{av_pf}
", unsafe_allow_html=True)

with col1:

    st.write("
Offensive stats
", unsafe_allow_html=True)

    categories = ["Field goal (%)", "3 points (%)", "Free throw (%)","Field goal (%)"]

    fig = go.Figure()

    fig.add_trace(go.Scatterpolar(
    r=[data_of_the_match["FG%"].loc[data_of_the_match["Player"]==player_select].values[0],
    data_of_the_match["3P%"].loc[data_of_the_match["Player"]==player_select].values[0],
    data_of_the_match["FT%"].loc[data_of_the_match["Player"]==player_select].values[0],
    data_of_the_match["FG%"].loc[data_of_the_match["Player"]==player_select].values[0]],
    theta=categories,
    fill='toself',
    name='Match',
    fillcolor='red',
    line=dict(color='red', width=3),
    opacity=0.6
    ))

    fig.add_trace(go.Scatterpolar(
        r=[data["FG%"].loc[data["Player"]==player_select].values[0],
        data["3P%"].loc[data["Player"]==player_select].values[0],
        data["FT%"].loc[data["Player"]==player_select].values[0],
        data["FG%"].loc[data["Player"]==player_select].values[0]],
        theta=categories,
        fill='toself',
        name='Euroleague season',
        fillcolor='gold',
        line=dict(color='gold', width=3),
        opacity=0.8
    ))

    fig.update_layout(
    polar=dict(
        bgcolor = "rgba(0,0,0,0)",
        radialaxis=dict(
        visible=True,
        range=[0, 1]
        ),
        angularaxis=dict(
            tickfont=dict(size=13, family='Arial'),
            linewidth=3,
            type='category',
            direction='clockwise'
        )),
    showlegend=True,
    paper_bgcolor="rgba(0,0,0,0)",
    plot_bgcolor="rgba(0,0,0,0)"
    )

    st.plotly_chart(fig,use_container_width=True)

with col3:

    # Définir les données
    categories = ["Offensive rebound", "Defensive rebound", "Total rebound", "Assist", "Steal", "Block", "Turnover"]
    season_avg = [data["ORB"].loc[data["Player"]==player_select].values[0],
                   data["DRB"].loc[data["Player"]==player_select].values[0],
                   data["TRB"].loc[data["Player"]==player_select].values[0],
                   data["AST"].loc[data["Player"]==player_select].values[0],
                   data["STL"].loc[data["Player"]==player_select].values[0],
                   data["BLK"].loc[data["Player"]==player_select].values[0],
                   data["TOV"].loc[data["Player"]==player_select].values[0]]

    match_stats = [data_of_the_match["ORB"].loc[data_of_the_match["Player"]==player_select].values[0],
                   data_of_the_match["DRB"].loc[data_of_the_match["Player"]==player_select].values[0],
                   data_of_the_match["TRB"].loc[data_of_the_match["Player"]==player_select].values[0],
                   data_of_the_match["AST"].loc[data_of_the_match["Player"]==player_select].values[0],
                   data_of_the_match["STL"].loc[data_of_the_match["Player"]==player_select].values[0],
                   data_of_the_match["BLK"].loc[data_of_the_match["Player"]==player_select].values[0],
                   data_of_the_match["TOV"].loc[data_of_the_match["Player"]==player_select].values[0]]

    diffs = [((m-s)/s*100) if s!=0 else 0 for s,m in zip(season_avg, match_stats)]

    # Créer les graphiques
    fig_bar = go.Figure()
    fig_bar.add_trace(go.Bar(x=season_avg, y=categories, name='Euroleague season', orientation='h', marker=dict(color='gold')))
    fig_bar.add_trace(go.Bar(x=match_stats, y=categories, name='Match', orientation='h', marker=dict(color='red'), textposition='auto'))

    # Ajouter les annotations avec les différences en pourcentage
    for i, diff in enumerate(diffs):
        if diff > 0:
            x = match_stats[i] + 0.40
            text = f"+{diff:.1f}%"
        else:
            x = season_avg[i] + 0.40
            text = f"{diff:.1f}%"
        fig_bar.add_annotation(x=x, y=categories[i], text=text,
                                font=dict( size=14),
                                showarrow=False, align='center')

    fig_bar.update_layout(
        barmode='group',
        margin=dict(l=50, r=50, t=50, b=50), height=500,
        legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
        yaxis=dict(tickfont=dict(size=14)),
        xaxis=dict(tickfont=dict(size=14))
    )

    fig_radar = go.Figure()
    fig_radar.add_trace(go.Scatterpolar(r=season_avg, theta=categories, fill='toself', name='Euroleague season', fillcolor = "Gold", line=dict(color='Gold', width=3), opacity=0.8 ))
    fig_radar.add_trace(go.Scatterpolar(r=match_stats, theta=categories, fill='toself', name='Match', fillcolor = "red", line=dict(color='red', width=3), opacity=0.6 ))
    fig_radar.update_layout(
        polar=dict(
            bgcolor = "rgba(0,0,0,0)",
            radialaxis=dict(
                visible=True,
                range=[0, max(season_avg+match_stats)]
            ),
            angularaxis=dict(
                tickfont=dict(size=14, family='Arial'),
                linewidth=3,
                type='category',
                direction='clockwise'
            )
        ),
        showlegend=True,
        paper_bgcolor="rgba(0,0,0,0)",
        plot_bgcolor="rgba(0,0,0,0)"
    )

    # Afficher les radio buttons et les graphiques correspondants

    st.write("
Compare euroleague season vs match stats
", unsafe_allow_html=True)
    graph_type = st.radio('Graph Type', ('Bar Chart', 'Radar Chart'))
    if graph_type == 'Bar Chart':
        st.plotly_chart(fig_bar, use_container_width=True)
    else:
        st.plotly_chart(fig_radar, use_container_width=True)


# Django imports
from django import forms
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.conf import settings
from django.core.urlresolvers import reverse_lazy
from django.db import models
from django.db.utils import IntegrityError
from django.forms.utils import ErrorList
from django.utils.translation import ugettext_lazy as _
from django.utils.decorators import method_decorator
from django.utils.encoding import force_text
from django.utils.html import format_html_join
from django.shortcuts import redirect

# Locale imports
from . import constants

# Python imports
import json

class CustomModel(object):
    """Custom class for models instances."""

    def to_json(self, *args):
        """Retorna los datos de la persona en formato JSON."""

        # se buscan los argumentos, o el parametro de json_fields
        fields = args or getattr(self, 'json_fields', None)

        # si no hay fields retorna la excepcion
        if fields is None:
            raise NotImplementedError(
                _('No se ha definido método .to_json para la clase %s' % self.__class__.__name__)
            )

        # crea el diccionario
        JSON = {}
        for field in fields:
            # si los campos no estan en self.__dir__()
            if field not in self.__dir__():  # self.__dict__
                # levanta la excepcion
                raise ValueError('Field "%s" not found in "%s"' % (field, self.__class__.__name__))
            # agrega el campo en mayúscula
            JSON[field] = getattr(self, field).__str__().upper()
        # retorna el diccionario
        return JSON

    def save(self, *args, **kwargs):
        # antes de guardar busca en los campos
        for field in self._meta.fields:
            # si el campo es obligatorio
            if not field.blank and not field.null:
                # si está vacio
                if getattr(self, field.name, '') == '':
                    # levanta una excepcion
                    raise IntegrityError('{} No puede estar vacio'.format(field.name))
        # retorna el super
        return super().save(*args, **kwargs)

class CustomErrorList(ErrorList):
    """Clase para hacer los errores de los formularios."""

    def __str__(self):
        # retorna en el string la nueva funcion
        return self.as_material()

    def as_material(self):
        # se define la funcion
        VOID = ''
        # si no hay datos, retorna vacio
        if not self.data:
            return VOID

        # de lo contrario retorna el error
        return format_html_join(
            VOID,
            '
{}
',
            ((force_text(e), ) for e in self)
        )

class FormMixin(object):
    """Clase Mixin para trabajar con los formularios"""

    # agrega la clase general de css, definida en las constantes
    error_css_class = constants.CSS_ERROR_CLASS

    def __init__(self, *args, **kwargs):
        # agrega la clase de error a todos los formularios
        super().__init__(error_class=CustomErrorList, *args, **kwargs)
        for field in self.fields:
            # agrega a todos los campos el placeholder con el label, y la clase
            if hasattr(self.fields[field], 'choices'):
                self.fields[field].widget.attrs.update({
                    'class': constants.SELECT_CLASS,
                    'placeholder': self.fields[field].label,
                    'tabindex': '-1'
                })
            else:
                self.fields[field].widget.attrs.update({
                    'class': constants.INPUT_CLASS,
                    'placeholder': self.fields[field].label
                })

    def add_class_error_to_input(self):
        """Agrega una clase de error de css al input."""
        for field in self.fields:
            if getattr(self, '_errors', None) is not None and field in self._errors:
                # si la clase está en los errores, le asigan la clase al input
                if hasattr(self.fields[field], 'choices'):
                    self.fields[field].widget.attrs.update({
                        'class': constants.CSS_ERROR_CLASS + ' ' + constants.SELECT_CLASS
                    })
                else:
                    self.fields[field].widget.attrs.update({
                        'class': constants.CSS_ERROR_CLASS + ' ' + constants.INPUT_CLASS
                    })

    def is_valid(self, *args, **kwargs):
        # se sobreescribe el metodo is_valid
        valid = super().is_valid(*args, **kwargs)

        # si no esta valido
        if not valid:
            # llama a la funcion para agrega errores al input
            self.add_class_error_to_input()
        # retorna el valido
        return valid

class CustomModelForm(FormMixin, forms.ModelForm):
    """Clase de base para trabajar con modelforms."""
    pass

class CustomForm(FormMixin, forms.Form):
    """Clase de base para trabajar con forms."""
    pass

class FechasRangoFormMixin(CustomForm):
    """Clase de base para trabajar un formulario con fecha inicial y fecha final."""

    fecha_inicial = forms.DateField(label=_('Fecha Inicial'))
    fecha_final = forms.DateField(label=_('Fecha Final'))

    def clean(self, *args, **kwargs):
        cleaned_data = super().clean(*args, **kwargs)

        fecha_inicial = cleaned_data.get('fecha_inicial', None)
        fecha_final = cleaned_data.get('fecha_final', None)

        if fecha_final is not None and fecha_inicial is not None:
            # se valida el campo
            if fecha_inicial > fecha_final:
                self.add_error(
                    'fecha_inicial', _('Fecha inicial no puede ser mayor que la Fecha final')
                )

        return cleaned_data

class CustomMixinView(object):
    """Mixin para las vistas basadas en clases"""

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        # si existe el grupo requerido
        if getattr(self, 'group_required', None) is not None:
            # saca los grupos
            grupos = self.group_required
            # saca el usuario
            user = request.user
            # si el usuario no tiene los grupos, y ademas no es superusuario
            if not user.groups.filter(name__in=grupos).exists() and not user.is_staff and not user.is_superuser:
                # lo redirecciona al login
                return redirect(settings.LOGIN_URL)
        # retorna el metodo dispatch
        return super().dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        # agrega el mensaje de success al formulario
        messages.success(
            self.request,
            _(constants.SUCCESS_FORM)
        )
        return super().form_valid(form)

    def form_invalid(self, form):
        # agrega el mensaje de error al formulario
        messages.error(
            self.request,
            _(constants.ERROR_FORM)
        )
        return super().form_invalid(form)

    def get_success_url(self):
        # sobreescribe el metodo get_success_url para las instancias de UpdateView
        if isinstance(self.success_url, str):
            # si es un string, retorna la url del string, mas el id del objeto
            return reverse_lazy(self.success_url, args=(self.object.id, ))
        # de lo contrario, retorna la url
        return super().get_success_url()

class CustomQuerySet(object):
    """Clase modelo para los querysets."""

    def to_json(self, **kwargs):
        """Metodo de serializado de querysets."""

        from django.core import serializers

        fields = kwargs.pop(
            'fields',
            getattr(self.model, 'json_fields', [x.name for x in self.model._meta.fields])
        )

        queryset = serializers.serialize('json', self, fields=fields, **kwargs)

        if isinstance(queryset, str):
            return json.loads(queryset)

        return queryset


import datetime
import random

import faker

fake = faker.Faker()

class Employee:
    GENDER = ('Male', 'Female')

    def __init__(self, full_name: str = None, birth_day: datetime.date = None, gender: str = None):
        self.full_name: str = full_name
        self.__birth_day: datetime.date = birth_day
        self.gender: str = gender

    def __repr__(self):
        return f'{self.full_name}'

    @property
    def birth_day(self):
        if self.__birth_day is not None:
            return self.__birth_day.strftime('%d:%m:%Y')

    @birth_day.setter
    def birth_day(self, bd: datetime.date):
        self.__birth_day: datetime.date = bd

    def generate_fake_employee(self):
        self.gender = random.choice(self.GENDER)
        self.full_name = fake.name_male() if self.gender == 'Male' else fake.name_female()
        self.birth_day = fake.date_of_birth()

    def input_employee(self):
        self.full_name = input("Type full name: ")
        self.gender = input(f"Type gender({'/'.join(self.GENDER)}): ")
        bd = input("Type birth_day(day:month:year): ").split(':')
        self.birth_day = datetime.date(*map(int, bd[::-1]))

    def print_all_info(self):
        print(
            f"Full name: {self.full_name}\n"
            f"Birth day: {self.birth_day}\n"
            f"Gender: {self.gender}\n"
        )

if __name__ == '__main__':
    employees = []
    for i in range(1, 3):
        print(f"-------Generate {i} employee-------")
        employee = Employee()
        employee.input_employee()
        employees.append(employee)
        employee.print_all_info()
    male_employees = list(filter(lambda empl: empl.gender == 'Male', employees))

    print(male_employees)


from django.contrib import admin

# Register your models here.
from import_export.admin import ImportExportMixin

from prices.models import CarPrice, InsuranceFee, Coupon

@admin.register(CarPrice)
class CarPriceAdmin(ImportExportMixin, admin.ModelAdmin):
    list_display = ('id',
                    'car',
                    'standard_price',
                    'min_price_per_km',
                    'mid_price_per_km',
                    'max_price_per_km',
                    'weekday_price_per_ten_min',
                    'weekend_price_per_ten_min')

@admin.register(InsuranceFee)
class InsuranceFeeAdmin(ImportExportMixin, admin.ModelAdmin):
    list_display = ('id',
                    'car',
                    'light_price',
                    'standard_price',
                    'special_price',
                    'light_price_per_ten_min',
                    'standard_price_per_ten_min',
                    'special_price_per_ten_min')

@admin.register(Coupon)
class CouponAdmin(ImportExportMixin, admin.ModelAdmin):
    list_display = ('id',
                    'member',
                    'date_time_start',
                    'expire_date_time',
                    'title',
                    'limit_delta_term',
                    'discount_fee',
                    'is_enabled',
                    'will_use_check',
                    'is_used',
                    'is_free',
                    'description',
                    'created_at',
                    'updated_at')


from numpy import ones, zeros, sort, argsort

def HFPquantile(x,conf,p=None):
    # This function computes the quantile of a Flexible probabilities
    # distribution
    #  INPUTS
    #  x            :[vector](1 x t_end) scenarios
    #  conf         :[vector](1 x n_ql) confidence levels
    #  p  (optional):[vector](1 x t_end) Flexible Probabilities
    #  OPS
    #  q_HFP        :[vector](1 x n_ql) quantiles

    ## Code

    n_ql = conf.shape[1]
    # if the third argument is missing, the Flexible Probabilities are set to be uniform
    if p is None:
        p = (1/x.shape[1])*ones((1,x.shape[1]))

    x_sort, y = sort(x), argsort(x)

    p_sort = p[0,y]

    q_HFP = zeros((1,n_ql))
    cum = 0
    j = 0
    t = 0
    while j < n_ql:
        while cum < conf[0,j] and t < x.shape[1]:
            cum = cum + p_sort[0,t]
            t = t+1
        if t == 0:
            q_HFP[0,j] = x_sort[0,0]
        else:
            q_HFP[0,j] = x_sort[0,t-1]
        j = j+1
    return q_HFP


directions = list(
    map(lambda x: -1 if x == "<" else 1, open("input.txt").readline().strip())
)

maxRocks1 = 2022
maxRocks2 = 1_000_000_000_000

chamberW = 7
chamber = []
height = 0
numRocks = 0
highest = -1

part1 = 0
part2 = 0

rocks = [
    [["#", "#", "#", "#"]],
    [[".", "#", "."], ["#", "#", "#"], [".", "#", "."]],
    [["#", "#", "#"], [".", ".", "#"], [".", ".", "#"]],
    [["#"], ["#"], ["#"], ["#"]],
    [["#", "#"], ["#", "#"]],
]

blocked = [(0, 0) for _ in range(chamberW)]

dirPos = 0
currentRock = 0

cycles = set()
cycleFound = False
while numRocks < maxRocks2:

    # Calculate part 1
    if numRocks >= maxRocks1 and not part1:
        part1 = len(chamber) + height

    # drop rock, 3 above the highest rock, and 2 from the left
    rock = rocks[currentRock]
    left = 2
    top = highest + 4
    right = left + len(rock[0]) - 1

    hasSettled = False
    while not hasSettled:
        # check for cycles
        newCycle = (currentRock, dirPos, "".join(map("".join, chamber)))
        if newCycle in cycles:
            if cycleFound:
                cycleSize = len(cycles)
                cycleHeight = height - cycleInfo[0]
                cycleStones = numRocks - cycleInfo[1]
                remainingCycles = (maxRocks2 - numRocks) // cycleStones
                height += remainingCycles * cycleHeight
                numRocks += remainingCycles * cycleStones
                cycles = set()
            else:
                cycleInfo = (height, numRocks)
                cycles = set()
                cycles.add(newCycle)
            cycleFound = True
        else:
            cycles.add(newCycle)

        bottom = top + len(rock) - 1

        # check if the rock is blocked before pushing
        dir = directions[dirPos]
        toCheck = []
        if (dir == 1 and right >= chamberW - 1) or (dir == -1 and left <= 0):
            dir = 0
        elif dir == -1:
            for row in range(len(rock)):
                for col in range(len(rock[row])):
                    if rock[row][col] == "#":
                        toCheck.append((left + col - 1, top + row))
                        break
        else:
            for row in range(len(rock)):
                for col in range(len(rock[row]) - 1, -1, -1):
                    if rock[row][col] == "#":
                        toCheck.append((left + col + 1, top + row))
                        break
        for pos in toCheck:
            if pos[1] >= len(chamber):
                break
            elif chamber[pos[1]][pos[0]] == "#" or pos[0] < 0 or pos[0] >= chamberW:
                dir = 0
                break

        # push
        left += dir
        right += dir

        # Check if rock can move down
        toCheck = []
        for col in range(len(rock[0])):
            for row in range(len(rock)):
                if rock[row][col] == "#":
                    toCheck.append((left + col, top + row - 1))
                    break
        for pos in toCheck:
            if pos[1] >= len(chamber):
                continue
            if pos[1] < 0 or chamber[pos[1]][pos[0]] == "#":
                # Rock has settled
                # Expand chamber if needed
                if bottom >= highest:
                    highest = bottom
                if highest >= len(chamber):
                    for _ in range(highest - len(chamber) + 1):
                        chamber.append(["." for _ in range(chamberW)])
                # Add rock to chamber
                for row in range(len(rock)):
                    for col in range(len(rock[row])):
                        if rock[row][col] == "#":
                            chamber[top + row][left + col] = "#"
                            blocked[left + col] = (
                                blocked[left + col][0] + 1,
                                top + row,
                            )

                # Check if we can remove any rows
                if all(b[0] >= 2 for b in blocked):
                    minBlocked = min(b[1] for b in blocked)
                    chamber = chamber[minBlocked:]
                    highest -= minBlocked
                    height += minBlocked
                    top = highest + 4
                    bottom = top + len(rock) - 1
                    blocked = [(0, 0) for _ in range(chamberW)]

                hasSettled = True
                numRocks += 1
                break

        # move rock down
        top -= 1
        dirPos = (dirPos + 1) % len(directions)

    currentRock = (currentRock + 1) % len(rocks)

part2 = len(chamber) + height

print("Part 1:", part1)
print("Part 2:", part2)


import os
from telethon import TelegramClient, events
from tinydb import TinyDB, Query

from server import keepalive

keepalive()

db = TinyDB('db.json')
User = Query()

def main():
  id = os.environ['api_id']
  hash = os.environ['api_hash']
  # Use your own values from my.telegram.org
  api_id = id
  api_hash = hash
  client = TelegramClient('anon', api_id, api_hash)

  @client.on(events.NewMessage)
  async def my_event_handler(event):
      print(event)
      channelid=event.peer_id.channel_id
      for i in getdata():
        print(i,channelid)
        chatid=str(i['toid'])
        if "-100"+str(channelid)==str(i['fromid']):
          #print(chatid,event.id,str(i['fromid'])[4:])
          await client.forward_messages(int(i['toid']),event.id,int(i['fromid']))

  client.start()
  client.run_until_disconnected()

def insertdata(fromchannel,tochannel):
  global db
  db.insert({"fromid":fromchannel,"toid":tochannel})

def getdata():
  global db
  return db.all()

if __name__=="__main__":
  main()


import mvn
from jifimogs.tools.JIFIdirs import resFN, datFN
import pickle as _pkl
import jifimogs.tools.plottools as mF
from matplotlib.ticker import FormatStrFormatter
import numpy as _N
import matplotlib.pyplot as _plt
import jifimogs.tools.utils as _util
import matplotlib.ticker as ticker

tksz = 21
epcs = 15
K    = 2
KGT  = 4      #  actual K
N    = 80     #  resolution of imshow.  interpolation="None" reduces file size drastically.
onepage=False
saturate = False
useEstMode = True
satLvl = 0.4
useGT    = False
satpct= 0.85
smpITER      = 2
thin    = 1
#  mk_hypPstMd   is epc x M x K

#tet = 106
tet = 1
datfn = "bc2_mdec0101"
#datfn = "frank_mdec0202"
epchfn= "15_2"
label = "1"
disp_pct = 0.99

fnSMP = ""

#_plt.ioff()

ytick_pos = "left"
imgfmt = "eps"
dfn = "%(df)s-%(ef)s" % {"df" : datfn, "ef" : epchfn}
if label is not None:
    dfn += "-%s" % label
if tet is not None:
    dfn += "/%d" % tet

# uses = [[0, 1], [0, 2], [0, 3], [0, 4],
#                 [1, 2], [1, 3], [1, 4],
#                         [2, 3], [2, 4],
#                                 [3, 4]]

# """
# uses = [[1, 4]]
# """

spis = [1, 2, 3]
if datfn == "bond_mdec0302":
    if tet == 112:
        uses = [[0, 1], [0, 2], [1, 2]]

        disp_ticks = _N.array([[-1, -1, -1, -1],
                               [0, 50, 100, 150],   # -15 162
                               [-50, 80, 210, 340]]) # -69 353
    elif tet == 114:
        uses = [[0, 2], [0, 4], [2, 4]]

        disp_ticks = _N.array([[-1, -1, -1, -1],
                               [-1, -1, -1, -1],
#                               [-50, 70, 190, 310],   # -15 162
                               #[-20, 85, 190, 295],   # -15 162
                               [0, 80, 160, 240],   # -15 162
                               [-1, -1, -1, -1],
                               [-50, 150, 350, 550]]) # -69 353

elif datfn == "frank_mdec0202":
    uses = [[0, 1], [0, 3], [1, 3]]

    disp_ticks = _N.array([[-1, -1, -1, -1],  # dummy
                           [20, 45, 70, 95], # 19.975 90.835
                           [-1, -1, -1, -1],  # dummy
                           [35, 60, 85, 110]]) # 33.51 98.38

elif datfn == "bc2_mdec0101":
    uses = [[0, 1], [0, 3], [1, 3]]

    disp_ticks = _N.array([[-1, -1, -1, -1],  # dummy
                           [ 55, 94, 133, 172], # 19.975 90.835
                           [-1, -1, -1, -1],  # dummy
                           [55, 105, 155, 205]]) # 33.51 98.38

"""
uses = [[1, 4]]
"""

showonly_this  = None
#showonly_this = _N.array([9])
#rmclstrs = _N.array([4, 16, 29, 30])
#rmclstrs = _N.array([0,1, 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28, 29, 30,31,32])
#rmclstrs = _N.array([0, 1, 2,3,4,5,6,7,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32])
#rmclstrs = _N.array([0, 1, 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22,23,24,25,26,27,28,29,30,31,32])
rmclstrs  = None

print(N)

p_rgb   = _N.zeros((epcs, len(uses), N, N, 3))

if tet is not None:
    dat   = _N.loadtxt(datFN("%(dfn)s_%(tet)d.dat" % {"dfn" : datfn, "tet" : tet}))
else:
    dat   = _N.loadtxt(datFN("%(dfn)s.dat" % {"dfn" : datfn,}))

if useGT:
    if tet is None:
        lm =  _util.depickle(datFN("%s_prms.pkl" % datfn))
    else:
        lm =  _util.depickle(datFN("%(dfn)s_%(tet)d_prms.pkl" % {"dfn" : datfn, "tet" : tet}))
    intvs01         = _N.loadtxt(datFN("itv%s.dat" % epchfn))
    intvs = _N.array(intvs01*dat.shape[0], dtype=_N.int)
    GTsmpIntv = lm["intv"]

Ms   = _N.empty(epcs, dtype=_N.int)

for epc in range(epcs):
    if not useGT:
        lm =  _util.depickle("%(d)s/posteriors_%(e)d.dmp" % {"d" : dfn, "e" : epc})
        intvs        = lm["intvs"]

    t0   = intvs[epc]
    t1   = intvs[epc+1]

    #sts = _N.where(dat[t0:t1, 1] == 1)[0]   # use all of data

    sts = _N.where(dat[intvs[0]:intvs[epcs], 1] == 1)[0]+intvs[0] #  all epochs - so we can easily compare plots between epoch because they use same axis limits
    sd  = _N.sort(dat[sts, 2:], axis=0)    #  first index is tetrode

    #sds = _N.std(dat[sts, 2:], axis=0)
    #mns = _N.median(dat[sts, 2:], axis=0)

    Nsp  = len(sts)
    #amps= sds*4  #  4 standard devs
    #mins= mns - amps;   maxs = mns + amps
    amps= sd[int(Nsp*disp_pct)] - sd[int(Nsp*(1-disp_pct))]

    mins = sd[int(Nsp*(1-disp_pct))] - 0.5*amps
    maxs = sd[int(Nsp*disp_pct)] + 0.5*amps
#     mins= _N.min(sd, axis=0) - 0.2*amps;     maxs= _N.max(sd, axis=0) + 0.2*amps

    Wdth= sd[-1] - sd[0]

    #amps = maxs - mins
    #dA   = 0.05*amps
    #x_L  = _N.array([-6, mins[0]-dA[0], mins[1]-dA[1], mins[2]-dA[2], mins[3]-dA[3]])
    #x_H  = _N.array([ 6, maxs[0]+dA[0], maxs[1]+dA[1], maxs[2]+dA[2], maxs[3]+dA[3]])
    #if K == 2:
    #x_L  = _N.array([-6, mins[0], mins[1], ])
    #    x_H  = _N.array([ 6, maxs[0], maxs[1], ])
    #if K == 4:
    x_L  = _N.array([-6, mins[0], mins[1], mins[2], mins[3]])
    x_H  = _N.array([ 6, maxs[0], maxs[1], maxs[2], maxs[3]])

    if (not useGT) and useEstMode:
        M            = lm["M"]
        if "freeClstr" in lm:
            inuse_clstr = _N.where(lm["freeClstr"] == False)[0]
        else:
            inuse_clstr = _N.arange(M)
        M            = len(inuse_clstr)
        Mwowonz      = M

        Ms[epc]      = M
        uss          = lm["mk_prmPstMd"][0][inuse_clstr]
        Sgs          = lm["mk_prmPstMd"][1][inuse_clstr]
        l0s          = lm["sp_prmPstMd"][::3][inuse_clstr]

        #ix = _N.where(l0s < 0)
        #l0s[ix] = 0.001

        if showonly_this is not None:
            rmclstrs = _N.setdiff1d(_N.arange(M), showonly_this)
        if rmclstrs is not None:
            l0s[rmclstrs] = 0

        fss          = lm["sp_prmPstMd"][1::3][inuse_clstr]
        q2s          = lm["sp_prmPstMd"][2::3][inuse_clstr]
        # print((l0s/_N.sqrt(q2s)))
        # print(_N.linalg.det(Sgs))
        # print("----------------")

        #nz_pth       = lm["nz_pth"]
    elif useGT:
        t0 =    intvs[epc] // GTsmpIntv
        t1 =    intvs[epc+1] // GTsmpIntv

        kms = lm["km"]
        M  = max([item for sublist in kms for item in sublist]) + 1

        uss = _N.empty((M, KGT))
        Sgs = _N.empty((M, KGT, KGT))
        l0s = _N.empty(M)
        fss = _N.empty(M)
        q2s = _N.empty(M)
        im  = -1
        for inrn in range(len(kms)):
            for ii in range(len(kms[inrn])):
                im += 1
                arr = _N.array(lm["u"][inrn, t0:t1])
                uss[im]          = _N.mean(arr, axis=0)
                Sgs[im]          = lm["covs"][inrn]
                l0s[im]          = _N.mean(lm["l0"][im, t0:t1], axis=0)
                fss[im]          = _N.mean(lm["f"][im, t0:t1], axis=0)
                q2s[im]          = _N.mean(lm["sq2"][im, t0:t1], axis=0)
        ix = _N.where(l0s < 0)
        l0s[ix] = 0.001

    else:   # use last sample
        smps = lm["smp_sp_prms"]
        mmps = lm["smp_mk_prms"]

        fnSMP        = "-%s" % smpITER
        uss          = mmps[0][:, smpITER].T#reshape(1, M, K)
        Sgs          = mmps[1][:, :, smpITER].T#.reshape(1, M, K, K)
        l0s          = smps[0, smpITER]#.reshape((1, M))
        fss          = smps[1, smpITER]#.reshape((1, M))
        q2s          = smps[2, smpITER]#.reshape((1, M))
        M            = smps.shape[2]#lm["sp_prmPstMd"].shape[1]/3

    ii    = -1

    for use in uses:
        ii += 1
        labX= "pos" if (use[0] == 0) else "mk tet%d" % use[0]
        labY= "pos" if (use[1] == 0) else "mk tet%d" % use[1]

        x1   = _N.linspace(x_L[use[0]], x_H[use[0]], N)
        x2   = _N.linspace(x_L[use[1]], x_H[use[1]], N)
        amps = x_H - x_L

        xy  = [x1, x2]

        grid = _N.array(_N.meshgrid(*xy, indexing="ij"))
        Ndat = dat.shape[0]

        Sg  = _N.zeros((2, 2))
        mns = _N.empty(2)
        mnsr = mns.reshape((2, 1, 1))

        for m in range(M):
            for ch in range(2):
                if use[ch] == 0:
                    Sg[ch, ch]      =   q2s[m]
                    mnsr[ch, 0, 0]  =   fss[m]
                else:
                    mkd = use[ch]-1
                    Sg[ch, ch]      =   Sgs[m, mkd, mkd]
                    mnsr[ch, 0, 0] =   uss[m, mkd]

            if (use[0] > 0) and (use[1] > 0):
                mkd1 = use[0] - 1;          mkd2 = use[1] - 1
                Sg[0, 1]   =   Sgs[m, mkd1, mkd2]
                Sg[1, 0]   =   Sgs[m, mkd2, mkd1]
            iSg = _N.linalg.inv(Sg)

            xyg = (grid-mnsr).T

            if _N.linalg.det(Sg) > 0:
                p_rgb[epc, ii, :, :, 0] +=  _N.sqrt((l0s[m] / _N.sqrt(2*_N.pi*_N.linalg.det(Sg)) * _N.exp(-0.5*_N.einsum("ijk,kl,ijl->ij", xyg, iSg, xyg))))
            else:
                print("Sg of cluster %d is not positive definite" % m)
                print("use[0]=%(0)d   use[1]=%(1)d" % {"0" : use[0], "1" : use[1]})
                print(Sg)

        if saturate and satpct < 1.:
            # fp = p[epc, ii].flatten()
            # thr = _N.sort(fp)[int(len(fp)*satpct)]
            # inds = _N.where(p[epc, ii] > thr)
            #p[epc, ii, inds[0], inds[1]] = thr
            #p_rgb = _N.log(1 + p_rgb)
            p_rgb[:, :, :, :, 0]  = _N.sqrt(p_rgb[:, :, :, :, 0])
#amp     = _N.max(_N.abs(p))    #  maximum amplitude

p_rgb[:, :, :, :, 1] = p_rgb[:, :, :, :, 0]
p_rgb[:, :, :, :, 2] = p_rgb[:, :, :, :, 0]
#p_rgb[:, :, :, :, 0] /= _N.max(p_rgb[:, :, :, :, 0])
for iu in range(len(uses)):
    p_rgb[:, iu, :, :] /= _N.max(p_rgb[:, iu, :, :, 0])

#############################
##########################
for epc in range(epcs):
    t0   = intvs[epc]
    t1   = intvs[epc+1]

    sts = _N.where(dat[t0:t1, 1] == 1)[0] + t0
    mkpos = _N.empty((len(sts), 2))

    for fign in range(1, 3):
        ii = -1
        #fig  = _plt.figure(num=fign, figsize=(12.2, 12))
        #fig  = _plt.figure(num=fign, figsize=(13.2, 12.7))  # if adding labels
        fig  = _plt.figure(num=fign, figsize=(4.5, 13.2))  # if adding labels

        for use in uses:
            ii += 1
            x1   = _N.linspace(x_L[use[0]], x_H[use[0]], N)
            x2   = _N.linspace(x_L[use[1]], x_H[use[1]], N)

            ch1  = 0 if use[0] == 0 else use[0] + 1
            ch2  = 0 if use[1] == 0 else use[1] + 1
            mkpos[:, 0] = (dat[sts, ch1] - x_L[use[0]]) * (N/float(amps[use[0]]))
            mkpos[:, 1] = (dat[sts, ch2] - x_L[use[1]]) * (N/float(amps[use[1]]))
            ax = fig.add_subplot(3, 1, spis[ii])
            #_plt.title("epoch %d" % epc, fontsize=tksz)
            if fign == 1:
                scps = 5 if K == 4 else 11
                ax.scatter(mkpos[::thin, 0], mkpos[::thin, 1], s=scps, color="orange")
            #ax.imshow(p[epc, ii], origin="lower", cmap=_plt.get_cmap("Blues"), vmin=0, vmax=_N.max(p[:, ii]), interpolation="none")

            ax.imshow(1-p_rgb[epc, ii], origin="lower", interpolation="none")

            #if fign == 2:
            #ax.contour(_N.arange(N), _N.arange(N), p[0, ii], levels=_N.array([_N.mean(p[:, ii])*satLvl]), colors=["#333333"])
            #_plt.xticks(, _N.linspace(x_L[use[0]], x_H[use[0]], 3), fontsize=tksz)
            #_plt.yticks([0, N/2., N], _N.linspace(x_L[use[1]], x_H[use[1]], 3), fontsize=tksz)

#plt.gca().yaxis.set_major_formatter(mtick.FormatStrFormatter('%.0e'))
            #ax.xaxis.set_major_formatter(FormatStrFormatter('%.2f'))

            if ytick_pos == "left":
                mF.arbitraryAxes(ax, axesVis=[True, True, False, False], xtpos="bottom", ytpos=ytick_pos)
            elif ytick_pos == "right":
                mF.arbitraryAxes(ax, axesVis=[False, True, True, False], xtpos="bottom", ytpos=ytick_pos)

            #mF.setTicksAndLims(xlabel=labX, ylabel=labY, xticks=None, yticks=None, xticksD=None, yticksD=None, xlim=None, ylim=None, tickFS=26, labelFS=28)

            # xlab = None
            # ylab = None
            # if ((spis[ii] % K) == 1):
            #     ylab = "elctrd"
            # if (spis[ii] == K*(K-1)+1):
            #     print "i am here"
            #     xlab = "position"
            # elif (spis[ii] > K*(K-1)+1):
            #     xlab = "elctrd"

            if use[0] == 0:
                #mF.setTicksAndLims(xlabel=None, ylabel=None, xticks=[0, 0.25*N, 0.5*N, 0.75*N, N], yticks=[0, N/2., N], xticksD=["H", "L", "H", "R", "H"], yticksD=_N.round(_N.linspace(x_L[use[1]], x_H[use[1]], 3), 2), xlim=None, ylim=None, tickFS=tksz, labelFS=(tksz+2))
                yticks_disp = disp_ticks[use[1]]
                y_uV_H = x_H[use[1]]
                y_uV_L = x_L[use[1]]
                yticks = (N / (y_uV_H - y_uV_L)) * (yticks_disp - y_uV_L)
                print("yticks!!!!    %d", use[0])
                print(yticks)

                #mF.setTicksAndLims(xlabel="position", ylabel=("mark ch %d" % use[1]), xticks=[0, 0.25*N, 0.5*N, 0.75*N, N], xticksD=["H", "L", "H", "R", "H"], yticks=yticks, yticksD=yticks_disp, xlim=None, ylim=None, tickFS=tksz, labelFS=(tksz+2))

                if (yticks[0] < 0) or (yticks[-1] > N):
                    print("!!!!!!!!!!  ticks not right, images might be wrong dimensions")
                    print(yticks)
                mF.setTicksAndLims(xlabel="position", ylabel=("mark ch %d $(\\mu V)$" % use[1]), xticks=[0, 0.25*N, 0.5*N, 0.75*N, N], xticksD=["H", "L", "H", "R", "H"], yticks=yticks, yticksD=yticks_disp, xlim=None, ylim=None, tickFS=tksz, labelFS=(tksz+2))

            else:
                #mF.setTicksAndLims(xlabel=None, ylabel=None, xticks=[0, N/2., N], yticks=[0, N/2., N], xticksD=_N.round(_N.linspace(x_L[use[0]], x_H[use[0]], 3), 2), yticksD=_N.round(_N.linspace(x_L[use[1]], x_H[use[1]], 3), 2), xlim=None, ylim=None, tickFS=tksz, labelFS=(tksz+2))
                #  0 -->  x_L
                #  N -->  x_H
                xticks_disp = disp_ticks[use[0]]
                yticks_disp = disp_ticks[use[1]]
                x_uV_H = x_H[use[0]]
                y_uV_H = x_H[use[1]]
                x_uV_L = x_L[use[0]]
                y_uV_L = x_L[use[1]]

                xticks = (N / (x_uV_H - x_uV_L)) * (xticks_disp - x_uV_L)
                yticks = (N / (y_uV_H - y_uV_L)) * (yticks_disp - y_uV_L)

                mF.setTicksAndLims(xlabel=("mark ch %d" % use[0]), xticks=xticks, xticksD=xticks_disp, yticks=yticks, yticksD=yticks_disp, ylabel=("mark ch %d" % use[1]), xlim=[0, N], ylim=[0, N], tickFS=tksz, labelFS=(tksz+2))

                #mF.setTicksAndLims(xlabel=None, xticks=xticks, xticksD=xticks_disp, yticks=yticks, yticksD=yticks_disp, ylabel=None, xlim=[0, N], ylim=[0, N], tickFS=tksz, labelFS=(tksz+2))

                #ax.yaxis.set_major_locator(ticker.MultipleLocator(20))
                #ax.xaxis.set_major_locator(ticker.MultipleLocator(20))
                #mF.setTicksAndLims(xlabel=None, ylabel=None, tickFS=tksz, labelFS=(tksz+2))

        fn_of = "GTonlyfit" if useGT else "onlyfit"
        fn_sf = "GTscatterfit" if useGT else "scatterfit"
        if showonly_this is not None:
            fn_of = "%(fn)s_cntrb_%(m)d" % {"fn" : fn_of, "m" : showonly_this[0]}
            fn_sf = "%(fn)s_cntrb_%(m)d" % {"fn" : fn_sf, "m" : showonly_this[0]
}

        # for fign in range(1, 3):
        #     fig  = _plt.figure(num=fign)
        if ytick_pos == "left":
            fig.subplots_adjust(left=0.24, right=0.96, bottom=0.06, top=0.95, wspace=0.5)
        else:
            fig.subplots_adjust(left=0.04, right=0.76, bottom=0.06, top=0.95, wspace=0.5)

        if fign == 1:
            _plt.savefig("%(eb)s/%(fn)s_%(ep)d%(sm)s_%(lr)s.%(fmt)s" % {"eb" : dfn, "ep" : epc, "sm" : fnSMP, "fn" : fn_sf, "fmt" : imgfmt, "lr" : ytick_pos}, transparent=True)
        else:
            _plt.savefig("%(eb)s/%(fn)s_%(ep)d%(sm)s_%(lr)s.%(fmt)s" % {"eb" : dfn, "ep" : epc, "sm" : fnSMP, "fn" : fn_of, "fmt" : imgfmt, "lr" : ytick_pos}, transparent=True)
        _plt.close()

    # #  Place prior for freeClstr near new non-hash spikes that are far
    # #  from known clusters that are not hash clusters

    # #  show me the non-hash new spikes in this epoch that are near known
    # #  non-hash clusters

    #ch  = 0 if use[1] == 0 else use[1] + 1
    mkpos[:, 0]  = dat[sts, 0]
    mkpos[:, 1] = dat[sts, 2]

_plt.ion()


"""creating service propostion tables

Revision ID: f9cba50da292
Revises: 40dbe60d7b4a
Create Date: 2021-03-23 01:21:32.429583

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'f9cba50da292'
down_revision = '40dbe60d7b4a'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('proposed_services',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('proposition_owner_id', sa.Integer(), nullable=False),
    sa.Column('proposition_status', sa.Enum('ACTIVE', 'CLOSED', 'CANCELED', name='servicepropositionstatus'), nullable=True),
    sa.Column('service_type', sa.Enum('CONSULTING', 'TRAINING', 'TEACHING', 'COACHING', name='servicetype'), nullable=True),
    sa.Column('proposition_description', sa.Text(), nullable=True),
    sa.ForeignKeyConstraint(['proposition_owner_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_proposed_services_proposition_owner_id'), 'proposed_services', ['proposition_owner_id'], unique=False)
    op.create_table('proposed_service_content_types',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('service_content_type', sa.Enum('RECORDED_CONTENT', 'WRITTEN_CONTENT', 'ONLINE_PERSONAL_MEETING', 'ONLINE_CONFERENCE', 'FACE_TO_FACE_MEETING', 'FACE_TO_FACE_CONFERENCE', name='servicecontenttype'), nullable=True),
    sa.Column('proposed_service_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['proposed_service_id'], ['proposed_services.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('proposed_service_content_types')
    op.drop_index(op.f('ix_proposed_services_proposition_owner_id'), table_name='proposed_services')
    op.drop_table('proposed_services')
    # ### end Alembic commands ###


#coding:utf-8
import os
import codecs,sys
import cv2
import string
import re
import math
import numpy as np
import argparse
import GetFileFromDir as GetFile

def dotsToRec4(dots):
    xmin, xmax, ymin, ymax = dots[0][0], dots[0][0], dots[0][1], dots[0][1]
    for i in range(3):
        xmin = min(xmin, dots[i+1][0])
        xmax = max(xmax, dots[i+1][0])
        ymin = min(ymin, dots[i+1][1])
        ymax = max(ymax, dots[i+1][1])
    x = (xmin + xmax)/2
    y = (ymin + ymax)/2
    w = xmax - xmin
    h = ymax - ymin
    rec4 = [x, y, w, h]
    return rec4
def dotsToRec8(dots):
    xmin, xmax, ymin, ymax = dots[0][0], dots[0][0], dots[0][1], dots[0][1]
    for i in range(3):
        xmin = min(xmin, dots[i+1][0])
        xmax = max(xmax, dots[i+1][0])
        ymin = min(ymin, dots[i+1][1])
        ymax = max(ymax, dots[i+1][1])
    rec8 = [xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax]
    return  rec8
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--labelTxt', default=r'E:\Data\GF2\txt\originlabelTxt2', type=str)
    parser.add_argument('--outdir', default=r'E:\Data\GF2\txt\8dotsRec', type=str)
    parser.add_argument('--outdirRec', default=r'E:\Data\GF2\txt\4dotsRec', type=str)
    args = parser.parse_args()
    list = GetFile.GetFileFromThisRootDir(args.labelTxt, 'txt');

    gfclass = {'0A':'plane', '0B':'plane', '0C':'plane', '0':'plane', '2':'bridge', '5':'ship', '5A':'ship', '5B':'ship',
           '8':'storage', '11':'harbor'}
    jlclass = {'0':'plane', '2':'bridge', '9':'ship', '6':'harbor', '5':'storage'}

    for file in list:
        strlist = file.split('\\');
        filename = strlist[len(strlist) - 1]
        suffix = os.path.splitext(filename)[1]
        filename = filename[0:(len(filename) - len(suffix))];
        print('yes', filename)
        f = open(file, 'r', encoding='utf_16')
        outdir = os.path.join(args.outdir, filename + '.txt');
        outdirRec = os.path.join(args.outdirRec, filename + '.txt')

        out4 = codecs.open(outdirRec, 'w', 'utf_16');
        out8 = codecs.open(outdir, 'w', 'utf_16');
        while True:
            dots = []
            line = f.readline()

            if line:
                line = line.strip()
                linelist = line.split(' ')

                ##change class name, drop blurr 2, reserve blurr 1
                print(linelist[8])
                linelist[8] = gfclass[linelist[8]]
                #linelist[8] = jlclass[linelist[8]]

                print('linelist', linelist)
                dots.append([int(linelist[0]), int(linelist[1])])
                dots.append([int(linelist[2]), int(linelist[3])])
                dots.append([int(linelist[4]), int(linelist[5])])
                dots.append([int(linelist[6]), int(linelist[7])])
                rec4 = dotsToRec4(dots)
                rec8 = dotsToRec8(dots)
                outline4 = ''
                for i in range(4):
                    outline4 = outline4 + str(rec4[i]) + ' '
                outline4 = outline4 + linelist[8]
                if (len(linelist) > 8):
                    if (linelist[8] == '1'):
                        outline4 = outline4 + ' ' + linelist[9]
                print('out4', outline4)
                out4.write(outline4 + '\n')

                outline8 = ''
                for i in range(8):
                    outline8 = outline8 + str(rec8[i]) + ' '
                outline8 = outline8 + linelist[8]
                if (len(linelist) > 8):
                    if (linelist[8] == '1'):
                        outline8 = outline8 + ' ' + linelist[9]
                print('out8', outline8)
                #out8.write(line + '\n')
                out8.write(outline8 + '\n')
            else:
                break
        f.close()
if __name__ == '__main__':
    main()


import requests
from parsel import Selector
from time import sleep
from tech_news.database import create_news

# Requisito 1
def fetch(url):
    sleep(1)
    try:
        response = requests.get(
            url, headers={"user-agent": "Fake user-agent"}, timeout=3
        )
        response.raise_for_status()
    except (requests.exceptions.HTTPError, requests.Timeout):
        return None
    else:
        return response.text

# Requisito 2
def scrape_updates(html_content):
    selector = Selector(text=html_content)

    news_urls = [
        news.css("a::attr(href)").get()
        for news in selector.css("#content article")
    ]

    return news_urls

# Requisito 3
def scrape_next_page_link(html_content):
    selector = Selector(text=html_content)

    next_page_url = selector.css(".next::attr(href)").get()
    return next_page_url

# Requisito 4
def scrape_news(html_content):
    selector = Selector(text=html_content)

    news = {}

    news["url"] = selector.css("link[rel=canonical]::attr(href)").get()
    news["title"] = selector.css(".entry-title::text").get().strip()
    news["timestamp"] = selector.css(".meta-date::text").get()
    news["writer"] = selector.css(".author a::text").get()

    comments_count = selector.css(".post-comments .title-block::text").get()
    news["comments_count"] = (
        int(comments_count.strip().split(" ")[0]) if comments_count else 0
    )

    news["summary"] = "".join(
        selector.css(".entry-content > p:first-of-type *::text").getall()
    ).strip()

    tags = selector.css(".post-tags ul li a::text").getall()
    news["tags"] = tags if tags else []

    news["category"] = selector.css(".entry-details span.label::text").get()

    return news

# Requisito 5
def get_tech_news(amount):
    url = "https://blog.betrybe.com/"
    news = []

    i = 0

    while len(news) < amount:
        html_content = fetch(url)
        news_urls = scrape_updates(html_content)

        try:
            news.append(scrape_news(fetch(news_urls[i])))
            i += 1
        except IndexError:
            url = scrape_next_page_link(html_content)
            i = 0

    create_news(news)
    return news


import keras
from keras import backend as K
from keras import layers
from keras import Model

import numpy as np

from custom_layers import xentKLLossLayer, SamplingLayer
from custom_fns import none_loss

# conv w/ (1,1) strides with pooling in-between
def conv_v2(latent_dim=8, num_filters=64, dense_dim=128, kernel_size=(3,3),
            input_shape=(28,28,1), conv_layers=2, pool='max'):
    encoder_input = keras.Input(shape=input_shape, name='encoder_input')
    x = encoder_input
    for i in range(conv_layers):
        x = layers.Conv2D(num_filters, kernel_size, strides=(1,1),
                          padding='same', activation='relu',
                          name='encoder_conv_'+str(i))(x)
        if pool is 'max':
            x = layers.MaxPooling2D(pool_size=(2,2), name='encoder_maxpool_'+str(i))(x)
        elif pool is 'average':
            x = layers.AveragePooling2D(pool_size=(2,2), name='encoder_avgpool_'+str(i))(x)
        elif pool is 'none':
            x = layers.Conv2D(num_filters, kernel_size, strides=(2,2),
                              padding='same', activation='relu',
                              name='encoder_conv_'+str(i)+'b')(x)
    shape_before_flattening = K.int_shape(x)
    x = layers.Flatten(name='flatten')(x)
    x = layers.Dense(dense_dim, activation='relu', name='encoder_dense_')(x)
    z_mean = layers.Dense(latent_dim, name='latent_mean')(x)
    z_log_var = layers.Dense(latent_dim, name='latent_var')(x)
    z = SamplingLayer(latent_dim, name='latent_sampling')([z_mean, z_log_var])
    # self.encoder = Model(encoder_input, z, name='encoder')

    decoder_input = layers.Input(K.int_shape(z)[1:], name='decoder_input')
    x = layers.Dense(np.prod(shape_before_flattening[1:]), activation='relu', name='decoder_dense')(decoder_input)
    x = layers.Reshape(shape_before_flattening[1:], name='reshape')(x)
    for i in range(conv_layers):
        x = layers.Conv2DTranspose(num_filters, kernel_size, strides=(2,2), padding='same',
                                    activation='relu', name='decoder_conv_'+str(i))(x)
    # below squeeze everything to btwn 0-1 grayscale values
    x = layers.Conv2D(1, 3, padding='same', activation='sigmoid', name='decoder_conv_to_img')(x)
    decoder = Model(decoder_input, x, name='decoder')
    z_decoded = decoder(z)
    y = xentKLLossLayer(name='loss')([encoder_input, z_decoded, z_mean, z_log_var])

    model = Model(encoder_input, y, name='vae')
    model.compile(optimizer='rmsprop', loss=none_loss) #loss=None)
    model.summary()
    return model


from datetime import datetime
from os import path, makedirs

from django.contrib.auth.models import User
from django.http import HttpResponse
from rest_framework.views import APIView

from platform.common.zip import Zip
from platform.models import Upload as upload
from platform.windows.models import Dump

class Upload(APIView):
    """ Saves uploaded file on the server. """

    def post(self, request):

        try:

            # Getting the uploaded file data.
            uploaded_file = request.data.get("filDump")

            # Checking if a file was not uploaded.
            if not uploaded_file:
                raise Exception("No file was uploaded.")

            # Getting the User ID.
            user_id = request.data.get("user")

            # Checking if User ID was not specified.
            if not user_id:
                raise Exception("No User was specified.")

            # Getting User from database.
            user = User.objects.get(pk=user_id)

            # Checking if the user does not exist.
            if user is None:
                raise Exception("User does not exist.")

            # Getting name of the file.
            uploaded_file_name = uploaded_file.name

            # Specifying upload directory.
            upload_dir = "./media/%s" % user_id

            # Checking if the directory does not exist.
            if not path.exists(upload_dir):
                # Making the directory.
                makedirs(upload_dir)

            # Specifying the upload location.
            upload_location = "%s/%s-%s" % (
                upload_dir,
                datetime.now().strftime("%Y%m%d-%H%M%S"),
                uploaded_file_name
            )

            # Opening the destination file in write-binary mode.
            with open(upload_location, "wb") as filDump:

                # Dividing the file data in chunks and iterating through each chunk.
                for chunk in uploaded_file.chunks():
                    # Saving the chunk.
                    filDump.write(chunk)

            # Setting User's ID in the session.
            request.session["api_user"] = user_id

            self.checkStatus(user_id)  # delete the dump which is not analayzed completely
            # Returning the HTTP Response with success and location of uploaded file..
            return HttpResponse('{"success": 1, "location": "%s"}' % upload_location)

        except Exception as ex:

            # Returning the HTTP Response with error.
            return HttpResponse('{"success":0, "error":"%s"}' % str(ex))

    # to flush data of any dump which is not anayzed completely before upload new
    @staticmethod
    def checkStatus(user_id):

        try:
            upload_all = upload.objects.filter(user_id=user_id)
            for i in upload_all:
                if i.status == "Analyzing":
                    del_id = i.pk
                    upload.objects.filter(pk=del_id).delete()
                    Dump.objects.filter(upload=del_id).delete()

        except Exception as ex:
            raise Exception(ex)

class Extract(APIView):
    """ Extracts the uploaded zip archive. """

    def post(self, request):

        try:

            # Getting the ZIP archive location from POST-ed data.
            zip_location = request.data.get("zip_location")

            # Checking if ZIP location is not specified.
            if not zip_location:
                raise Exception("No file was uploaded.")

            # Initializing a new object of Zip class wtih zip location and extracting it.
            zip_object = Zip(zip_location)
            zip_object.extract()

            # Returning the response.
            return HttpResponse('{"success":1, "status":"Extraction complete.", "data":%s}'
                                % '{"dump_location":"%s"}'
                                % zip_object.dump_location.replace("\\", "/"))

        except Exception as ex:

            # Returning any exception messages.
            return HttpResponse('{"success":0, "error":"%s"}' % str(ex))


from . import index, other, user, video
from flask import Blueprint, request, url_for, session, abort

def create_bp_admin():
    admin_blu = Blueprint('admin', __name__, url_prefix='/admin')
    user.api.register(admin_blu, url_prefix='/user')
    index.api.register(admin_blu, url_prefix='/')
    video.api.register(admin_blu, url_prefix='/video')
    other.api.register(admin_blu, url_prefix='/other')
    return admin_blu

admin_blu = create_bp_admin()

# @admin_blu.before_request
# def before_request():
#     # 判断如果不是登录页面的请求
#     if not request.url.endswith(url_for("admin.login")):
#         user_id = session.get("user_id")
#         is_admin = session.get("is_admin", False)
#
#         if not user_id or not is_admin:
#             # 判断当前是否有用户登录，或者是否是管理员，如果不是，直接重定向到项目主页
#             abort(404)


import os
import time
import json
from icecream import ic
from urllib import request
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import pandas as pd
import re

NOT_DONE_QUE = dict()

def write_cookie():
    options = webdriver.ChromeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(r'C:\Program Files\Google\Chrome\Application\chromedriver.exe')
    url = "https://guorn.com"
    driver.get(url)
    time.sleep(20)
    # 此时浏览器已经打开，在浏览器中打开网页并登陆
    filename = r"F:\quantitative_trade\cookie.json"
    # 开始写入cookie
    cookies = driver.get_cookies()
    jsonCookies = json.dumps(cookies)
    with open(filename, 'w') as f:
        f.write(jsonCookies)
    driver.close()

def init():
    driver = webdriver.Chrome(r'C:\Program Files\Google\Chrome\Application\chromedriver.exe')
    dirname = r"F:\quantitative_trade"
    driver.implicitly_wait(30)
    # 记得写完整的url 包括http和https
    driver.get('https://guorn.com')
    # 首先清除由于浏览器打开已有的cookies
    driver.delete_all_cookies()

    # 读取cookie
    with open(os.path.join(dirname, 'cookie.json'), 'r') as cookief:
        # 使用json读取cookies 注意读取的是文件 所以用load而不是loads
        cookieslist = json.load(cookief)

    # 方法1 将expiry类型变为int
    for cookie in cookieslist:
        # 并不是所有cookie都含有expiry 所以要用dict的get方法来获取
        if isinstance(cookie.get('expiry'), float):
            cookie['expiry'] = int(cookie['expiry'])
        driver.add_cookie(cookie)

    return driver

def parser_href(driver, path=5):
    driver.get('https://guorn.com/user/home?page=created')
    driver.implicitly_wait(40)
    try:
        xpath = f'//*[@id="content-cnt"]/div[2]/div[5]/div[3]/div/button[{path}]'
        text = driver.find_element_by_xpath(xpath).text.split('(')
        num, text_ = int(text[1][:-1]), text[0]
        ic(num, text_)
        driver.find_element_by_xpath(xpath).click()
        ic('已点击')
    except Exception as e:
        ic(e)
        return None

    hrefs = dict()
    for i in range(1, num + 1):
        href_xpath = f'//*[@id="content-cnt"]/div[2]/div[5]/div[3]/table[1]/tbody/tr[{i}]/td[1]/a'
        info = driver.find_element_by_xpath(href_xpath)
        href = info.get_attribute('href')
        name = driver.find_element_by_xpath(href_xpath).text.strip()
        hrefs[name] = href
    pd.DataFrame({'name': list(hrefs.keys()), 'href': list(hrefs.values())}).to_csv(f'{text_}_hrefs.csv', index=None)
    return driver, hrefs

def modify_2_years(driver, hrefs, year='两'):
    driver.implicitly_wait(40)
    num_month = '7'
    if year == '两':
        num_year = '2019'
    elif year == '三':
        num_year = '2018'
    elif year == '半':
        num_year = '2021'
        num_month = '1'
    # 查看定义 xpath
    ckdy_path = '//*[@id="strategy-cnt"]/div[1]/span[2]'
    # 日期控件
    start_time_path = '//*[@id="time-box"]/div[1]/input'
    # 另存为 xpath
    lcw_path = '//*[@id="stockScreen-top-bar"]/span[2]'
    # 策略名
    give_name_path = '//*[@id="save-as-dialog"]/div/div/div[2]/input'
    # 保存
    store_path = '//*[@id="save-as-dialog"]/div/div/div[3]/button[2]'

    for k, v in hrefs.items():
        try:
            ic(k, v)
            wait = WebDriverWait(driver, 30, 0.5)
            driver.get(v)
            wait.until(lambda diver: driver.find_element_by_xpath(ckdy_path))
            time.sleep(15)

            driver.find_element_by_xpath(ckdy_path).click()

            wait.until(lambda diver: driver.find_element_by_xpath(start_time_path))
            time.sleep(5)

            start_time = driver.find_element_by_xpath(start_time_path)
            start_time.click()

            wait.until(lambda diver: driver.find_element_by_xpath(lcw_path))
            js_value = f'document.querySelector("#time-box > div.input-group.date.form_date_start > input").value="{num_year}/{num_month}/13"'  # js添加时间
            driver.execute_script(js_value)
            time.sleep(4)

            driver.find_element_by_xpath(lcw_path).click()
            name = k + f"_近{year}年"
            time.sleep(2)
            wait.until(lambda diver: driver.find_element_by_xpath(give_name_path))
            give_name = driver.find_element_by_xpath(give_name_path)
            give_name.click()
            time.sleep(2)
            give_name.send_keys(name)
            time.sleep(2)
            wait.until(lambda diver: driver.find_element_by_xpath(store_path))
            store = driver.find_element_by_xpath(store_path)
            store.click()

            # //*[@id="common-warn-dialog"]/div/div/div/div[3]/button[1]  知道啦
            # //*[@id="strategy-saved-info"]/button

            ok_p, err_p = '//*[@id="strategy-saved-info"]/button', '//*[@id="common-warn-dialog"]/div/div/div/div[3]/button[1]'
            wait.until(lambda diver: driver.find_element_by_xpath(ok_p) or driver.find_element_by_xpath(err_p))
            # driver.find_element_by_xpath(err_p).click()

            time.sleep(25)
        except Exception as e:
            pd.Series({k: v}).to_csv(f'error{year}.csv', mode='a', header=False)
            NOT_DONE_QUE[k] = [v]
            ic(e)
            ic('error')
            continue

if __name__ == '__main__':
    # write_cookie()
    driver = init()
    # parser_href(driver, path=5)
    # parser_href(driver, path=2)
    # pd.set_option('display.max_columns', None)
    #
    dict_ = dict()
    # dict1 = pd.read_csv('chaosuan_hrefs.csv')
    # dict2 = pd.read_csv('HT策略分类_hrefs.csv')
    # dict_all = pd.concat([dict1, dict2])
    dict_all = pd.read_csv('task.csv')
    for k, v in dict_all.iterrows():
        ic(v['name'].strip(), v['href'])
        dict_[v['name'].strip()] = v['href']

    modify_2_years(driver, dict_, year='三')


#----------------------------------------------
# -*- encoding=utf-8 -*-                      #
# __author__:'xiaojie'                        #
# CreateTime:                                 #
#       2019/6/19 14:52                       #
#                                             #
#               天下风云出我辈，                 #
#               一入江湖岁月催。                 #
#               皇图霸业谈笑中，                 #
#               不胜人生一场醉。                 #
#----------------------------------------------
# https://github.com/bojone/infomax
import numpy as np
import glob
from scipy import misc

from keras.models import Model
from keras.layers import *
from keras import backend as K
from keras.optimizers import Adam
from keras.datasets import cifar10

import warnings
warnings.filterwarnings('ignore')

(x_train,y_train),(x_test,y_test) = cifar10.load_data()
print(x_train.shape,y_train.shape,x_test.shape,y_test.shape,x_train.dtype)
x_train = x_train.astype(np.float32)/255-0.5
x_test = x_test.astype(np.float32)/255-0.5
y_train = y_train.reshape(-1)
y_test = y_test.reshape(-1)
img_dim = x_train.shape[1]
print(x_train.shape,y_train.shape,x_test.shape,y_test.shape,img_dim)

z_dim = 256 # 隐变量维度
alpha = 0.5 # 全局互信息的loss比重
beta = 1.5 # 局部互信息的loss比重
gamma = 0.01 # 先验分布的loss比重

#编码器(卷积与最大池化)
x_in = Input(shape=(img_dim,img_dim,3))
x=x_in

for i in range(3):
    x = Conv2D(z_dim // 2**(2-i),
               kernel_size=(3,3),
               padding='SAME')(x)
    x=BatchNormalization()(x)
    x=LeakyReLU(0.2)(x)
    x = MaxPool2D((2,2))(x)

feature_map = x # 截断到这里，认为到这里就是feature_map(局部特征)
print('feature_map.shape',feature_map.shape)
feature_map_encoder = Model(x_in,x)

for i in range(2):
    x= Conv2D(z_dim,
              kernel_size=(3,3),
              padding='SAME')(x)
    x = BatchNormalization()(x)
    x=LeakyReLU(0.2)(x)

print('AAAAAAAAAA',x.shape)
x = GlobalAveragePooling2D()(x)#全局特征
print('BBBBBBBBB',x.shape)

z_mean = Dense(z_dim)(x) # 均值，也就是最终输出的编码
print('z_mean.shape',z_mean.shape)
z_log_var = Dense(z_dim)(x)# 方差，这里都是模仿VAE的

encoder = Model(x_in,z_mean) # 总的编码器就是输出z_mean

#重参数技巧
def sampling(args):
    z_mean,z_log_var = args
    u = K.random_normal(shape=K.shape(z_mean))
    return z_mean + K.exp(z_log_var/2)*u

# 重参数层，相当于给参数加上噪声
z_samples = Lambda(sampling)([z_mean,z_log_var])
prior_kl_loss = -0.5*K.sum(1+z_log_var-K.square(z_mean)-K.exp(z_log_var),axis=-1)
prior_kl_loss = K.mean(prior_kl_loss)
print('TTTTTTTTTTTT',z_log_var.shape)
print('prior_kl_loss',prior_kl_loss)
print('z_samples.shape',z_samples.shape)

# shuffle层,打乱第一个轴
def shuffling(x):
    idxs = K.arange(0,K.shape(x)[0])
    idxs = K.tf.random_shuffle(idxs)
    return K.gather(x,idxs)

# 与随机采样的特征拼接(全局)
z_shuffle = Lambda(shuffling)(z_samples)
z_z_1 = Concatenate()([z_samples,z_samples])
z_z_2 = Concatenate()([z_samples,z_shuffle])
print('z_z_2.shape',z_z_2.shape)

# 与随机采样的特征拼接(局部)
feature_map_shuffle = Lambda(shuffling)(feature_map)
print('feature_map_shuffle.shape',feature_map_shuffle.shape)
z_samples_repeat = RepeatVector(16)(z_samples)
print('z_samples_repeat.shape',z_samples_repeat.shape)
z_samples_map = Reshape((4,4,z_dim))(z_samples_repeat)#(?, 4, 4, 256)
z_f_1 = Concatenate()([z_samples_map,feature_map])
z_f_2 = Concatenate()([z_samples_map,feature_map_shuffle])# (?, 4, 4, 512)
print('CCCCCCCCCCCC',z_samples_map.shape,feature_map.shape,feature_map_shuffle.shape)

# 全局判别器
z_in = Input(shape=(z_dim*2,))
print('z_in.shape',z_in.shape)
z = z_in
z = Dense(z_dim,activation='relu')(z)
z = Dense(z_dim,activation='relu')(z)
z = Dense(z_dim,activation='relu')(z)
z = Dense(1,activation='sigmoid')(z)
print('GlobalDiscriminator.shape',z.shape)

GlobalDiscriminator = Model(z_in,z)
z_z_1_score = GlobalDiscriminator(z_z_1)
z_z_2_score = GlobalDiscriminator(z_z_2)
print('z_z_2_score.shape',z_z_2_score.shape)
global_info_loss = -K.mean(K.log(z_z_1_score+1e-6)+K.log(1-z_z_2_score+1e-6))
print('global_info_loss:',global_info_loss)

# 局部判别器
z_in = Input(shape=(None,None,z_dim*2))
z = z_in
z = Dense(z_dim,activation='relu')(z)
z = Dense(z_dim,activation='relu')(z)
z = Dense(z_dim,activation='relu')(z)
z = Dense(1, activation='sigmoid')(z)
print('z.shape',z.shape)

LocalDiscriminator = Model(z_in,z)
z_f_1_score = LocalDiscriminator(z_f_1)
z_f_2_score = LocalDiscriminator(z_f_2)
print('z_f_2_score.shape',z_f_2_score.shape)
print('z_f_2.shape',z_f_2.shape)
# local_info_loss = -K.mean(K.log(z_f_1_score+1e-6)+K.log(1-z_f_2_score+1e-6))
local_info_loss = -K.sum(K.log(z_f_1_score+1e-6)+K.log(1-z_f_2_score+1e-6),
                         axis=list(range(1,K.ndim(z_f_1_score))))
print('local_info_loss.shape',local_info_loss.shape)
local_info_loss = K.mean(local_info_loss)

print('local_info_loss',local_info_loss)

# 用来训练模型
model_train = Model(x_in,[z_z_1_score,z_z_2_score,z_f_1_score,z_f_2_score])
model_train.add_loss(alpha*global_info_loss+beta*local_info_loss+gamma*prior_kl_loss)
# model_train.add_loss(alpha*global_info_loss+gamma*prior_kl_loss)
model_train.compile(optimizer=Adam(1e-3))
model_train.metrics_names.append('global_info_loss')
model_train.metrics_tensors.append(global_info_loss)
print('===============',model_train.metrics_names)

model_train.fit(x_train,epochs=50,batch_size=64,verbose=True)
model_train.save_weights('total_model.cifar10.weights')

# 输出编码器的特征
zs = encoder.predict(x_train,verbose=True)
zs.mean() # 查看均值(简单观察先验分布有没有达到效果)
zs.std()# 查看方差(简单观察先验分布有没有达到效果)

# 随机选一张图片,输出最相近的图片
# 可以选用欧式距离或者cos值
def sample_knn(path):
    n = 10
    topn = 10
    figure1 = np.zeros((img_dim*n,img_dim*topn,3))
    figure2 = np.zeros((img_dim*n,img_dim*topn,3))
    zs_ = zs/(zs**2).sum(1,keepdims=True)**0.5
    for i in range(n):
        one = np.random.choice(len(x_train))
        idxs = ((zs**2).sum(1)+(zs[one]**2).sum()-2*np.dot(zs,zs[one])).argsort()[:topn]
        for j,k in enumerate(idxs):
            digit = x_train[k]
            figure1[i*img_dim:(i+1)*img_dim,
                    j*img_dim:(j+1)*img_dim] = digit
        idxs = np.dot(zs_,zs_[one]).argsort()[-n:][::-1]
        for j,k in enumerate(idxs):
            digit = x_train[k]
            figure2[i*img_dim:(i+1)*img_dim,
                    j*img_dim:(j+1)*img_dim] = digit

    figure1 = (figure1+1)/2*255
    figure1 = np.clip(figure1,0,255)
    figure2 = (figure2+1)/2 * 255
    figure2 = np.clip(figure2,0,255)
    misc.imsave(path+'_l2.png',figure1)
    misc.imsave(path+'_cos.png',figure2)