from matplotlib import transforms
import numpy as np
import matplotlib.pyplot as plt
import sys
import os

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from matplotlib import cm
from random import random, seed
import utils

# to suppress warnings from fig.tight_layout() in some plotsFalse
# import warnings
# warnings.filterwarnings("ignore")

plt.style.use('seaborn')
plt.rc('text', usetex=True)
plt.rc('font', family='DejaVu Sans')
path_plots = '../output/plots'

def set_ax_info(ax, xlabel, ylabel, title=None, zlabel=None):
    """Write title and labels on an axis with the correct fontsizes.

    Args:
        ax (matplotlib.axis): the axis on which to display information
        title (str): the desired title on the axis
        xlabel (str): the desired lab on the x-axis
        ylabel (str): the desired lab on the y-axis
        zlabel (str): the desired lab on the z-axis

    """
    if zlabel is None:
        ax.set_xlabel(xlabel, fontsize=20)
        ax.set_ylabel(ylabel, fontsize=20)
        ax.set_title(title, fontsize=20)
        ax.tick_params(axis='both', which='major', labelsize=15)
        ax.ticklabel_format(style='plain')
        ax.legend(fontsize=15)
    else:
        ax.set_xlabel(xlabel, fontsize=18)
        ax.set_ylabel(ylabel, fontsize=18)
        ax.set_zlabel(zlabel, fontsize=18)
        ax.tick_params(axis='both', which='major', labelsize=12)

def push(file):
    os.system(f"git add {file}")
    os.system("git commit -m 'plots'")
    os.system("git push")
    print(f"Pushed to git: {file}")

def show(fig, fname, args):
    if args.dataset == "SRTM":
        print("Terrain data: \'SRTM_\' added at beginning of plot file name")
        fname = "SRTM_" + fname
    if args.save:
        print("Saving plot: ", fname + ".pdf")
        fig.savefig(os.path.join(path_plots, fname + '.pdf'))
    if args.push:
        push(os.path.join(path_plots, fname + ".pdf"))
    if args.show:
        plt.show()
    else:
        plt.clf()

def Plot_3DDataset(x, y, z, args, predict=False):
    """3D plot the data and saves the plot in the output folder
        Either Franke funcprint(cm.hot(0.3))tion or terrain data
    Args:
        x (array):  x-data
        y (array):  y-data
        z (array):  z-values
        args (argparse): argparse containing info about run
    """

    if 1 in z.shape:
        nx = x.shape[0]
        ny = x.shape[1]
        z = z.reshape((nx, ny))

    fig = plt.figure()
    ax = fig.gca(projection="3d")
    # Plot the surface.
    surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm,
                           linewidth=0, antialiased=False)
    # Customize the z axis.
    ax.zaxis.set_major_locator(LinearLocator(10))
    ax.zaxis.set_major_formatter(FormatStrFormatter("%.02f"))

    # Add a color bar which maps values to colors.
    fig.colorbar(surf, shrink=0.5, aspect=5)
    # general formalities
    if predict:
        fname = "prediction_p" + str(max(args.polynomial))
        title = f"Prediction of raw data for P = {max(args.polynomial)}"
    else:
        fname = f"rawdata"
        title = "Raw data"
    if args.dataset == "Franke":
        fname += f"_eps{args.epsilon}".replace(".", "")
    fname = fname.replace('.', '')  # remove dots from fname
    xlabel = '$x$'
    ylabel = '$y$'
    zlabel = '$z$'
    set_ax_info(ax, xlabel, ylabel, title, zlabel)
    fig.tight_layout()
    show(fig, fname, args)

def Plot_error(MSE_test, MSE_train, args):
    """Plot mean square error as a function of polynomial degree
    for test and train data

    Args:
        MSE_test (array): array of test mean square error
        MSE_train (array): array of train mean square error
        args (argparse): argparse containing information of method used
    """
    # Plot the data
    fig, ax = plt.subplots()
    ax.plot(args.polynomial, MSE_test, "bo--", label="test MSE")
    ax.plot(args.polynomial, MSE_train, "ro--", label="Train MSE")

    amin = np.argmin(MSE_test)
    print(f"Lowest error is {MSE_test[amin]}, for p = {args.polynomial[amin]}")

    # general formalities
    if args.dataset == "SRTM":
        fname = 'MSE_' + args.method \
                + '_n' + str(args.num_points) \
                + '_pol' + str(max(args.polynomial))

        title = 'Terrain data MSE for ' + args.method

    else:
        fname = 'MSE_' + args.method \
                + '_n' + str(args.num_points) \
                + '_eps' + str(args.epsilon) \
                + '_pol' + str(max(args.polynomial))

        title = 'MSE for ' + args.method

    fname = fname.replace('.', '')  # remove dots from fname

    if args.resampling != "None":
        title += " using " \
                 + args.resampling \
                 + f" iter = {args.resampling_iter}"
        if args.method != "OLS":
            title += r', $\lambda$ = ' + str(args.lmb[0])
        fname += "_" + args.resampling + "_" + "re" \
                 + str(args.resampling_iter)
    if args.method != "OLS":
        fname += '_lam_'+str(args.lmb[0])
        fname = fname.replace('.', '_')
    xlabel = 'Polynomial degree'
    if args.log:
        ylabel = 'log(MSE)'
    else:
        ylabel = 'MSE'

    set_ax_info(ax, xlabel, ylabel, title)
    if args.log:
        ax.set_yscale('log')
        fname += '_log'

    # save figure
    print('Plotting error: See ' + fname + '.pdf')
    show(fig, fname, args)

def Plot_R2(R2_test, R2_train, args):
    """Plot R^2 score as a function of polynomial degree
    for test and train data

    Args:
        R2_test (array): array of test R2 score
        R2_train (array): array of train R2 score
        args (argparse): argparse containing information of method used
    """
    if args.resampling != "None":
        return
    elif args.method != "OLS":
        return
    # Plot the data
    fig, ax = plt.subplots()
    ax.plot(args.polynomial, R2_test, "bo--", label="Test R2")
    ax.plot(args.polynomial, R2_train, "ro--", label="Train R2")
    # general formalitiesiscrete uniform”
    if args.dataset == "SRTM":
        fname = 'R2_' + args.method \
                + '_n' + str(args.num_points) \
                + '_eps' + str(args.epsilon) \
                + '_pol' + str(max(args.polynomial))
        title = "Terrain data R2 score for " \
                + args.method

    else:
        fname = 'R2_' + args.method \
                + '_n' + str(args.num_points) \
                + '_eps' + str(args.epsilon) \
                + '_pol' + str(max(args.polynomial))

        title = 'R2 score for ' \
                + args.method

    fname = fname.replace('.', '')  # remove dots from fname
    xlabel = 'Polynomial degree'
    ylabel = 'R2'
    set_ax_info(ax, xlabel, ylabel, title)

    # save figure
    print('Plotting R2: See ' + fname + '.pdf')
    show(fig, fname, args)

def Plot_bias_var_tradeoff(datas, args):
    """Plot mean square error, variance and error as a function of polynomial degree

    Args:
        datas (defaultdict) contains following arrays
                test_errors
                test_biases
                test_vars
                train_errors
                train_biases
                train_vars
        args (argparse): argparse containing information of method used
    """
    # make figure and plot data
    fig, ax = plt.subplots()

    P = args.polynomial
    ax.plot(P, datas["test_errors"], "bo-", label="test MSE")
    ax.plot(P, datas["test_biases"], "ro-", label="test Bias")
    ax.plot(P, datas["test_vars"], "go-", label="test Variance")
    ax.plot(P, datas["train_errors"], "bo--", label="train MSE")
    ax.plot(P, datas["train_biases"], "ro--", label="train Bias")
    ax.plot(P, datas["train_vars"], "go--", label="train Variance")
    # General formalities
    xlabel = 'Polynomial degree'
    ylabel = 'Bias, variance and MSE'
    title = 'Bias Variance Tradeoff for ' + args.method+', '\
        + args.resampling + ' iter = '+str(args.resampling_iter)
    if args.log:
        ylabel = '(log) Bias, variance and MSE'
    set_ax_info(ax, xlabel, ylabel, title)

    # Saving figure
    if args.dataset == "SRTM":
        fname = 'BVT_' + args.method \
                + '_n' + str(args.num_points)
        title = 'Terrain data: ' + title

    else:
        fname = 'BVT_' + args.method \
                + '_n' + str(args.num_points) \
                + '_eps' + str(args.epsilon)

    if args.method != "OLS":
        fname += '_lam_'+str(args.lmb[0])

    if args.log:
        ax.set_yscale('log')
        fname += '_log'

    fname = fname.replace('.', '_')
    print('Plotting Bias variance tradeoff: See %s.pdf' % (fname))
    show(fig, fname, args)

def Plot_lambda(results, args):
    """ Plot test MSE as function of lambda, for different polynomial degrees

    Args:
        results: (defaultdict) contains 1 2D array with test errors
                First index goes over poly-degree
                2nd index goes over lambda
    """
    fig, ax = plt.subplots()

    lmbs = args.lmb
    for i, p in enumerate(args.polynomial):
        r = results["test_MSE"][i]
        if args.log:
            r = np.log10(r)
        ax.plot(np.log10(lmbs), r, label=f"Polynomial degree: {p}")

    r = results["test_MSE"]
    min_val = np.min(r)
    mp, ml = np.where(r == min_val)
    mp = args.polynomial[mp[0]]
    ml = np.log10(lmbs[ml][0])
    print(f"Min MSE is {min_val} at p={mp}, l={ml}")

    xlabel = "log10(lambda-parameter)"
    ylabel = "log10(Error)" if args.log else "Error"
    title = f"Error using {args.method} and {args.resampling} iter = {args.resampling_iter}"

    if args.dataset == "SRTM":
        title = "Terrain data: " + title
    set_ax_info(ax, xlabel, ylabel, title)

    low = str(int(np.log10(args.lmb[0]))).replace("-", "m")
    high = str(int(np.log10(args.lmb[-1]))).replace("-", "m")
    fname = f"lambdaMSE_{args.method}_{args.resampling}{args.resampling_iter}_n{args.num_points}"
    fname += f"_eps{args.epsilon}_p{args.polynomial[-1]}_l{low}_{high}"
    fname = fname.replace(".", "")
    print('Plotting lambda_analysis: See %s.pdf' % (fname))
    show(fig, fname, args)

def Plot_VarOLS(args):
    """Here we plot the parameters for different polynomials
    with confidence intervals.
    """
    p = np.array([1, 3, 5])
    n = 30
    sigma_sq = 0.2
    x = np.sort(np.random.uniform(0, 1, n))
    y = np.sort(np.random.uniform(0, 1, n))
    x, y = np.meshgrid(x, y)
    z = utils.FrankeFunction(x, y, sigma_sq)
    X_ = utils.create_X(x, y, p[-1])
    for i in p:
        X = X_[:, :utils.get_features(i)]
        XTXinverse = np.linalg.pinv(X.T @ X)
        beta = XTXinverse @ X.T @ z
        variance_beta = sigma_sq*XTXinverse
        sigma_sq_beta = np.diag(variance_beta)
        upper = beta[:, 0] + 2*np.sqrt(sigma_sq_beta)
        lower = beta[:, 0] - 2*np.sqrt(sigma_sq_beta)
        beta_index = np.arange(0, len(beta), 1)
        fig, ax = plt.subplots()
        ax.scatter(beta_index, beta, label=r'$\beta$(index)')
        ax.fill_between(beta_index, lower, upper,
                        alpha=0.2, label=r'$\pm 2\sigma$')
        xlabel = r'index'
        ylabel = r'$\beta$'
        title = r'$\beta$-values with confidence interval for p = %i' % (i)
        set_ax_info(ax, xlabel, ylabel, title)
        fname = 'Var_OLS_poldeg_' + str(i)
        show(fig, fname, args)
        print('Plotting variance in beta: See ' + fname + '.pdf')

def Plot_BVT_lambda(result, args):
    """
    Plots BVT for as function of polynomial degree for different lambda
    """
    fig, ax = plt.subplots()

    P = args.polynomial

    # c = np.linspace(0, 1, len(args.lmb))
    c = [0.1, 0.3, 0.9]
    cmap = cm.jet
    for j, lmb in enumerate(args.lmb):
        ax.plot(P, result["test_errors"][:, j], c=cmap(
            c[j]), label=f"MSE, lambda: {lmb}")
        ax.plot(P, result["test_biases"][:, j],
                "--", c=cmap(c[j]), label="bias")
        ax.plot(P, result["test_vars"][:, j], "-.",
                c=cmap(c[j]), label="variance")

    xlabel = "Polynomial degree"
    ylabel = "Bias, variance and error"
    title = f"BV-tradeoff for {args.method} using {args.resampling} iter = {args.resampling_iter}"
    set_ax_info(ax, xlabel, ylabel, title)

    fname = f"LBVT_{args.method}_{args.resampling}_n{args.num_points}"
    fname += f"_eps{args.epsilon}_p{args.polynomial[-1]}"
    low = str(int(np.log10(args.lmb[0]))).replace("-", "m")
    high = str(int(np.log10(args.lmb[-1]))).replace("-", "m")
    fname += f"_lmb{low}_{high}"
    fname = fname.replace(".", "_")
    print("Plotting lambda BVT: see ", fname, ".pdf")

    fig.tight_layout()
    show(fig, fname, args)

def Plot_2D_MSE(results, args):
    """
    Plots contour map of MSE as function of polynomial degree and lambda
    """
    fig, ax = plt.subplots()
    P, lmb = np.meshgrid(args.polynomial, np.log10(args.lmb))
    MSE = results["test_MSE"].T
    if args.log:
        MSE = np.log10(MSE)
        title = "log10(MSE)"
    else:
        title = "MSE"

    min_val = np.min(MSE)
    min_l, min_P = np.where(MSE == min_val)
    min_P = args.polynomial[min_P[0]]
    min_l = np.log10(args.lmb[min_l[0]])
    print(f"Min MSE is {min_val} at p={min_P}, l={min_l}")

    F = ax.contourf(P, lmb, MSE, cmap="jet")
    norm = mpl.colors.Normalize(vmin=F.cvalues.min(), vmax=F.cvalues.max())
    bar = mpl.cm.ScalarMappable(norm=norm, cmap=F.cmap)
    bar.set_array([])
    cbar = fig.colorbar(bar, ticks=F.levels)
    cbar.set_label("log10(Error)" if args.log else "Error",
                   rotation=90, fontsize=20, position=(1, 0.5))

    ax.scatter(min_P, min_l, s=30, c="r", marker="x")

    xlabel = "Polynomial degree"
    ylabel = "log10(lambda)"
    title += f" for {args.method} using {args.resampling} iter = {args.resampling_iter}"
    set_ax_info(ax, xlabel, ylabel, title)

    fname = f"Contour_PL_{args.method}_{args.resampling}{args.resampling_iter}_n{args.num_points}_eps{args.epsilon}"
    ph = str(args.polynomial[-1])
    pl = str(args.polynomial[0])
    lh = str(int(np.log10(args.lmb[0]))).replace("-", "m")
    ll = str(int(np.log10(args.lmb[-1]))).replace("-", "m")
    fname += f"_p{pl}_{ph}_lmb{ll}_{lh}"

    show(fig, fname, args)

if __name__ == "__main__":
    """
    Here we can plot Franke function with or withot noise,
    simply change the self.epsilon value to the desired noise.
    Then run python3 plot.py, and it will generate a file with the appropriate
    name.
    """
    class Argparse:
        def __init__(self, eps=0):
            self.show = False
            self.epsilon = eps
            self.dataset = "Franke"

    N = 30
    x = np.sort(np.random.uniform(size=N))
    y = np.sort(np.random.uniform(size=N))
    x, y = np.meshgrid(x, y)
    args = Argparse()
    z = utils.FrankeFunction(x, y, eps=args.epsilon)


from flask_restful import Resource
from oeda.controller.experiment_results import get_all_stage_data
import matplotlib.pyplot as plt
import traceback
import json
import numpy as np
from io import BytesIO
import statsmodels.api as sm
import base64
from oeda.databases import db

# https://www.pythonanywhere.com/forums/topic/5017/
# https://stackoverflow.com/questions/38061267/matplotlib-graphic-image-to-base64
class QQPlotController(Resource):

    availableScales = ["normal", "log"]

    def get(self, experiment_id, stage_no, distribution, scale):
        try:
            if str(scale).lower() not in self.availableScales:
                return {"error": "Provided scale is not supported"}, 404

            pts = []
            # this case corresponds to all stage data
            if int(stage_no) == -1:
                stages_and_data_points = get_all_stage_data(experiment_id=experiment_id)
                if stages_and_data_points is None:
                    return {"error": "Data points cannot be retrieved for given experiment and/or stage"}, 404
                for entity in stages_and_data_points:
                    entity = json.loads(entity)
                    if len(entity['values']) == 0:
                        pass
                    for data_point in entity['values']:
                        pts.append(data_point["payload"]["overhead"])
            else:
                data_points = db().get_data_points(experiment_id=experiment_id, stage_no=stage_no)
                if data_points is None:
                    return {"error": "Data points cannot be retrieved for given experiment and/or stage"}, 404
                for data_point in data_points:
                    pts.append(data_point["payload"]["overhead"])

            # create the qq plot based on the retrieved data against normal distribution
            array = np.asarray(pts)
            sorted_array = np.sort(array)
            if str(scale).lower() == "log":
                sorted_array = np.log(sorted_array)

            fig1 = sm.qqplot(sorted_array, dist=str(distribution).lower(), line='45', fit=True)
            buf1 = BytesIO()
            fig1.savefig(buf1, format='png')
            buf1.seek(0)

            figure_data_png = base64.b64encode(buf1.getvalue())
            buf1.close()
            fig1.clf()
            del fig1
            plt.close('all')
            return figure_data_png

        except Exception as e:
            tb = traceback.format_exc()
            print(tb)
            return {"message": e.message}, 40


# -*- coding: utf-8 -*-

"""
    进度条插件
"""

from PyQt5.QtWidgets import QDialog, QLabel, QProgressBar, QHBoxLayout, QPushButton,QVBoxLayout

WINDOWS_WIDTH = 350
WINDOWS_HEIGHT = 100
BAR_MAXIMUM = 100

class ProgressBar(QDialog):
    ''' 进度条窗口 '''

    def __init__(self, title, parent=None):
        super(ProgressBar, self).__init__(parent)

        self.resize(WINDOWS_WIDTH, WINDOWS_HEIGHT)
        self.setWindowTitle(self.tr(title))

        # 设置label
        self.__bar_label = QLabel(self.tr(title + ":"))
        self.__tip_label = QLabel(self.tr("Processing:"))

        # 设置进度条
        self.__progress_bar = QProgressBar(self)
        self.__progress_bar.setMinimum(0)
        self.__progress_bar.setMaximum(BAR_MAXIMUM)
        self.__progress_bar.setValue(0)

        # 设置进度条框体
        __bar_layout = QHBoxLayout()
        __bar_layout.addWidget(self.__bar_label)
        __bar_layout.addWidget(self.__progress_bar)

        # 设置tip框体
        __tip_layout = QHBoxLayout()
        __tip_layout.addWidget(self.__tip_label)

        # 设置取消按钮
        self.__cancel_button = QPushButton('Cancel', self)
        __cancel_layout = QHBoxLayout()
        __cancel_layout.addStretch(1)
        __cancel_layout.addWidget(self.__cancel_button)

        # 框架展示
        layout = QVBoxLayout()
        layout.addLayout(__bar_layout)
        layout.addLayout(__tip_layout)
        layout.addLayout(__cancel_layout)
        self.setLayout(layout)
        self.show()

        # 取消按钮响应事件
        self.__cancel_button.clicked.connect(self.cancel)

    def set_value(self, index, total):
        ''' 即时进度 '''

        # 更新进度条
        process_value = index / total * BAR_MAXIMUM
        self.__progress_bar.setValue(process_value)
        # 更新tip
        self.__tip_label.setText(self.tr("Processing:  " + str(index) + " / " + str(total)))

    def cancel(self):
        ''' 取消按钮 '''

        self.close()


import typing as t
from dataclasses import dataclass

from docopt import docopt  # type: ignore

from glue.chain import Chain

@dataclass
class DocOptChain(Chain):
    name: str
    command_doc: str

class Generator:
    def __call__(self, chains: t.List[DocOptChain]) -> t.Dict[str, t.Callable]:
        commands = {}
        for chain in chains:
            command = create_command(chain)
            command_name = chain.name
            commands[command_name] = command
        return commands

def create_command(link: DocOptChain) -> t.Callable:
    def command(args):
        args = docopt(link.command_doc, argv=args)
        link.compose()(args)

    return command


import cv2
import numpy as np
import matplotlib.pyplot as plt
import math

def hanning2D(n):
    h = np.hanning(n)
    return np.sqrt(np.outer(h,h))

def highpassFilter(size):
    rows = np.cos(np.pi*np.matrix([-0.5 + x/(size[0]-1) for x in range(size[0])]))
    cols = np.cos(np.pi*np.matrix([-0.5 + x/(size[1]-1) for x in range(size[1])]))
    X = np.outer(rows,cols)
    return (1.0 - X) * (2.0 - X)

domek = cv2.cvtColor(cv2.imread('./obrazy_Mellin/domek_r30.pgm'), cv2.COLOR_BGR2GRAY)
wzor = cv2.cvtColor(cv2.imread('./obrazy_Mellin/domek_r0_64.pgm'), cv2.COLOR_BGR2GRAY)

# wzor w rozmiarze obrazka bez filtracji
wzor_NP = np.zeros(domek.shape, dtype=np.uint8) #  NP- non performance
# okno Hanninga dla wzoru
han = hanning2D(wzor.shape[0])

# centrowanie wzoru
wzor_ = np.zeros(domek.shape, dtype = np.uint8)
l = np.int((domek.shape[0]-wzor.shape[0])/2)
r = np.int((domek.shape[1]+wzor.shape[1])/2)
wzor_[l:r,l:r] = wzor * han
wzor_NP[l:r,l:r] = wzor
# FFT na obrazkach
wzor_FFT = np.fft.fft2(wzor_)
domek_FFT = np.fft.fft2(domek)

# shift na obrazkach
wzor_SHIFT = np.fft.fftshift(wzor_FFT)
domek_SHIFT = np.fft.fftshift(domek_FFT)

# filtrowanie obrazków filterm górnoprzepustowym
wzor_F = wzor_SHIFT * highpassFilter(wzor_SHIFT.shape)
domek_F = domek_SHIFT * highpassFilter(domek_SHIFT.shape)

# parametry transformaty LogPolar
R = int(wzor_F.shape[0]//2)
M = 2*R/np.log(R)
# przygotowanie obrazków moduł liczb zespolonych
wzor_ABS = np.abs(wzor_F)
domek_ABS = np.abs(domek_F)
# transformata LogPolar
wzor_LP = cv2.logPolar(wzor_ABS, (wzor_F.shape[0]//2, wzor_F.shape[1]//2), M, cv2.INTER_LINEAR + cv2.WARP_FILL_OUTLIERS)
domek_LP = cv2.logPolar(domek_ABS, (domek_F.shape[0]//2, domek_F.shape[1]//2), M, cv2.INTER_LINEAR + cv2.WARP_FILL_OUTLIERS)

# ponowna transformata FFT
wzor_FFT2 = np.fft.fft2(wzor_LP)
domek_FFT2 = np.fft.fft2(domek_LP)

# korelacja ########################

ccor = np.conj(wzor_FFT2) * domek_FFT2
ccor = ccor/np.abs(ccor)
ccor = np.fft.ifftshift(ccor)
ccor = np.fft.ifft2(ccor)

wsp_kata, wsp_logr = np.unravel_index(np.argmax(abs(ccor)), ccor.shape)

# przeliczanie na skalowanie i stopnie
if wsp_logr > wzor_FFT2.shape[1]//2:
    wykl = wzor_FFT2.shape[1] - wsp_logr
else:
    wykl = - wsp_logr
A = (wsp_kata * 360.0)/wzor_FFT2.shape[1]
skala = np.exp(1/M) ** wykl
kat1 = 360 - A
kat2 = 360 - A - 180

# przeksztalcenie afiniczne
srodekTrans = [math.floor((domek.shape[0] + 1) / 2), math.floor((domek.shape[1] + 1 ) / 2)]
macierz_translacji1 = cv2.getRotationMatrix2D((srodekTrans[0], srodekTrans[1]), kat1, skala)
macierz_translacji2 = cv2.getRotationMatrix2D((srodekTrans[0], srodekTrans[1]), kat2, skala)

d1 = cv2.warpAffine(wzor_NP, macierz_translacji1, (wzor_NP.shape[1], wzor_NP.shape[0]))
d2 = cv2.warpAffine(wzor_NP, macierz_translacji2, (wzor_NP.shape[1], wzor_NP.shape[0]))

plt.figure(1)
plt.gray()
plt.imshow(wzor_NP)

d1fft = np.fft.fftshift(d1)
d2fft = np.fft.fftshift(d2)
d1fft = np.fft.fft2(d1fft)
d2fft = np.fft.fft2(d2fft)
d1fft = np.conj(d1fft)
d2fft = np.conj(d2fft)

korelacja1 = d1fft * domek_FFT
korelacja2 = d2fft * domek_FFT
korelacja1 = korelacja1/abs(korelacja1)
korelacja2 = korelacja2/abs(korelacja2)

korelacja1 = np.fft.ifftshift(korelacja1)
korelacja2 = np.fft.ifftshift(korelacja2)

korelacja1 = np.fft.ifft2(korelacja1)
korelacja2 = np.fft.ifft2(korelacja2)
korelacja1 = abs(korelacja1)
korelacja2 = abs(korelacja2)

y1, x1 = np.unravel_index( np.argmax(korelacja1), korelacja1.shape)
y2, x2 = np.unravel_index( np.argmax(korelacja2), korelacja2.shape)

if korelacja1[y1][x1] > korelacja2[y2][x2]:
    xk = x1
    yk = y1
    wzorzeck = d1
else:
    xk = x2
    yk = y2
    wzorzeck = d2

dx = xk - wzorzeck.shape[0]//2
dy = yk - wzorzeck.shape[1]//2
macierz_translacji = np.float32([[1,0,dx],[0,1,dy]])
obraz_przesuniety = cv2.warpAffine(wzorzeck, macierz_translacji, (wzorzeck.shape[1], wzorzeck.shape[0]))

plt.figure(1)
plt.imshow(obraz_przesuniety)
plt.figure(2)
plt.imshow(domek)

10. Korelacyjne dopasowywanie wzorca/zad2.py


####################################################################################################################
####################################################################################################################
#########################                                                                  #########################
#########################            PLD Kinetic Monte Carlo Processes Module              #########################
#########################                                                                  #########################
####################################################################################################################
####################################################################################################################

"""
Kinetic Monte Carlo Processes Module

Contains code to create the rate catalog for thermal processes

Version: 3.4.1a

"""

# import modules
import numpy as np
from scipy.constants import physical_constants, convert_temperature

#################################
###   Process Catalog Class   ###
#################################
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------

class Process_Catalog:
    """
    Class to set up elementary processes and rates. Sets the order of Process Table and Process Lists.

    This class sets the order in the process table and corresponding moves.

    Future Goal is to be able to set processes here and have the main code automatically adapt. Not there yet.
    If changes are made here, the corresponding change must be made in corresponding "Processes" class.

    Inputs
    -------
    parameters:  activation energies and substrate temperature

    Methods:
    --------
    Create_Catalog:   Selects the process catalog based on parameter inputs
    Return_Rates:     Returns the rate_catalog, rates, moves, and keys
    Deposition_2D:
    Deposition_3D:

    Outputs
    -------
    rate_catalog:  Dictionary of Rate Names and Values
    rates:         Ordered Array of all elementary process rates
    moves:         Associated lattice moves that go with rates.
    keys:          Ordered list of keys corresponding to the rates array
    numProc:       Number of Processes for each direction (Primary and Secondary Moves)

    """

    def __init__(self,Parameters):
        self.Parameters = Parameters

        self.dim = Parameters['Dimension']
        self.type = Parameters['Simulation_Type']

        self.w0 = Parameters['w0']
        self.temperature = self.Parameters['Substrate_Temperature']

        self.diff = Parameters['Ea_diffusion']
        self.es = Parameters['Ea_ehrlich_schwoebel']
        self.detach = Parameters['Ea_detach']
        self.edge = Parameters['Ea_edge']
        self.corner = Parameters['Ea_corner']

        # constants
        self.tk = convert_temperature(self.temperature,'Celsius', 'Kelvin')
        self.kB = physical_constants[ 'Boltzmann constant in eV/K' ][0]
        self.kT = self.tk*self.kB  # in eV

        self.Create_Catalog()

    def Create_Catalog(self):
        """ Determine which catalog to create and create it """
        if self.type == 'Deposition' or self.type == 'Diffusion' or self.type == 'Density':
            if self.dim == '2D':
                self.Simulation_2D()
            elif self.dim == '3D':
                self.Simulation_3D()
        elif self.type == 'Ising':
            self.Ising_2D()

    def Return_Rates(self):
        """ Return the calculated rates """

        return (self.rate_catalog,self.rates,self.moves,self.keys,self.num_barriers_per_move)

    def Simulation_2D(self):
        """ Set up Process Table for 2D Deposition (1+1) """

        # Set Primary and Secondary Moves (order based on first secondary move being to the left of the primary move)
        Moves = ['Left','Left_Down','Left_Up',\
                 'Up','Up_Left','Up_Right',\
                 'Right','Right_Up','Right_Down',\
                 'Down','Down_Right','Down_Left']
        Moves_X = [-1,-1,-1,\
                   0,-1,1,\
                   1,1,1,\
                   0,1,-1]
        Moves_Z = [0,-1,1,\
                   1,1,1,\
                   0,1,-1,\
                   -1,-1,-1]

        # Set Barriers for Primary and Secondary Moves (name should include each barrier present)
        Primary = ['Diff','Diff_Detach1','Diff_Detach2','Edge2','Edge2_Detach1']
        Secondary = ['Diff_ES', 'Diff_Detach1_ES', 'Diff_Detach2_ES']

        Barriers = [Primary,Secondary,Secondary,Primary,Secondary,Secondary,Primary,Secondary,Secondary,Primary,Secondary,Secondary]

        self.num_barriers_per_move = len(Primary)+len(Secondary)+len(Secondary) # number of processes per primary move

        # Create Rate Catalog (keys must match the Barriers)
        self.rate_catalog = {}
        self.rate_catalog['Diff'] = self.w0*np.exp(-self.diff/self.kT)   # free edge diffusion
        self.rate_catalog['Diff_Detach1'] = self.w0*np.exp(-(self.detach+self.diff)/self.kT)  # detach from kink
        self.rate_catalog['Diff_Detach2'] = self.w0*np.exp(-(2*self.detach+self.diff)/self.kT)  # detach from edge
        self.rate_catalog['Edge2'] = self.w0*np.exp(-(self.edge+self.diff)/self.kT)  # two edge diffusion
        self.rate_catalog['Edge2_Detach1'] = self.w0*np.exp(-(self.detach+self.edge+self.diff)/self.kT)  # two edge diffusion + detach
        self.rate_catalog['Diff_ES'] = self.w0*np.exp(-(self.diff+self.es)/self.kT)   # erhlich-schwoebel barrier
        self.rate_catalog['Diff_Detach1_ES'] = self.w0*np.exp(-(self.detach+self.diff+self.es)/self.kT)   # erhlich-schwoebel barrier plus break 1 bond
        self.rate_catalog['Diff_Detach2_ES'] = self.w0*np.exp(-(2*self.detach+self.diff+self.es)/self.kT)   # erhlich-schwoebel barrier plus break 2 bonds

        # Create corresponding Process Table, Rate Table, and Move Table to send to solver
        self.rates = []  # Rate Table
        self.moves = []   # Move Table
        self.keys = []  # Table of Keys corresponding to each entry (Move_Barriers)

        for i in range(len(Moves)):
            for j in range(len(Barriers[i])):
                self.rates.append(self.rate_catalog[Barriers[i][j]])
                self.moves.append([Moves_X[i],Moves_Z[i]])
                self.keys.append((Moves[i]+'_'+Barriers[i][j]))

        self.rates = np.array(self.rates,dtype=np.double)
        self.moves = np.array(self.moves,dtype=np.int32)

    def Simulation_3D(self):
        """ Set up Process Table for 3D Deposition (2+1) """

        # Set order of moves in the process table (straight,straight-down,straight-up,straight-left,straight-right)
        Moves = ['North','North_Down','North_Up','North_West','North_East',\
                 'East','East_Down','East_Up','East_North','East_South',\
                 'South','South_Down','South_Up','South_East','South_West',\
                 'West','West_Down','West_Up','West_South','West_North']

        # set corresponding  coordinate moves
        Moves_X = [0,0,0,-1,1,\
                   1,1,1,1,1,\
                   0,0,0,1,-1,\
                   -1,-1,-1,-1,-1]
        Moves_Y = [1,1,1,1,1,\
                   0,0,0,1,-1,\
                   -1,-1,-1,-1,-1,\
                   0,0,0,-1,1]
        Moves_Z = [0,-1,1,0,0,\
                   0,-1,1,0,0,\
                   0,-1,1,0,0,\
                   0,-1,1,0,0]

        # Set Barriers for Primary and Secondary Moves (name should include each barrier present)
        Barrier_Primary = ['Diff','Diff_Detach1','Diff_Detach2','Diff_Detach3','Edge','Edge_Detach1','Edge_Detach2']
        Barrier_Down = ['Diff_ES','Diff_Detach1_ES','Diff_Detach2_ES','Diff_Detach3_ES']
        Barrier_Up = ['Diff_Detach1_ES','Diff_Detach2_ES','Diff_Detach3_ES','Diff_Detach4_ES']
        Barrier_Corner = ['Corner','Corner_Detach1','Corner_Detach2']

        # Set Barriers for each move (can have multiple barriers for each move)
        Barriers = [Barrier_Primary,Barrier_Down,Barrier_Up,Barrier_Corner,Barrier_Corner,\
                    Barrier_Primary,Barrier_Down,Barrier_Up,Barrier_Corner,Barrier_Corner,\
                    Barrier_Primary,Barrier_Down,Barrier_Up,Barrier_Corner,Barrier_Corner,\
                    Barrier_Primary,Barrier_Down,Barrier_Up,Barrier_Corner,Barrier_Corner,]

        # number of processes per primary move
        self.num_barriers_per_move = len(Barrier_Down)+len(Barrier_Up)+len(Barrier_Primary)+len(Barrier_Corner)+len(Barrier_Corner)

        # Create Rate Catalog (keys must match the Barriers)
        self.rate_catalog = {}
        self.rate_catalog['Diff'] = self.w0*np.exp(-self.diff/self.kT)   # free surface diffusion
        self.rate_catalog['Diff_Detach1'] = self.w0*np.exp(-(self.detach+self.diff)/self.kT)  # detach from island, break 1 bond
        self.rate_catalog['Diff_Detach2'] = self.w0*np.exp(-(2*self.detach+self.diff)/self.kT)  # detach from island, break 2 bonds
        self.rate_catalog['Diff_Detach3'] = self.w0*np.exp(-(3*self.detach+self.diff)/self.kT)  # detach from island, break 3 bonds
        self.rate_catalog['Diff_ES'] = self.w0*np.exp(-(self.diff+self.es)/self.kT)   # downhill barrier
        self.rate_catalog['Diff_Detach1_ES'] = self.w0*np.exp(-(self.detach+self.diff+self.es)/self.kT)   # detach, break 1 bond, down
        self.rate_catalog['Diff_Detach2_ES'] = self.w0*np.exp(-(2*self.detach+self.diff+self.es)/self.kT)   # detach, break 2 bonds, up/down
        self.rate_catalog['Diff_Detach3_ES'] = self.w0*np.exp(-(3*self.detach+self.diff+self.es)/self.kT)   # detach, break 3 bonds, up/down
        self.rate_catalog['Diff_Detach4_ES'] = self.w0*np.exp(-(4*self.detach+self.diff+self.es)/self.kT)   # detach, break 4 bonds, up only
        self.rate_catalog['Edge'] = self.w0*np.exp(-(self.diff+self.edge)/self.kT)   # free edge diffusion
        self.rate_catalog['Edge_Detach1'] = self.w0*np.exp(-(self.diff+self.edge+self.detach)/self.kT)  # detach from kink plus edge diffusion, break 1 bond
        self.rate_catalog['Edge_Detach2'] = self.w0*np.exp(-(self.diff+self.edge+2*self.detach)/self.kT)  # detach from kink plus edge diffusion, break 2 bonds
        self.rate_catalog['Corner'] = self.w0*np.exp(-(self.diff+self.edge+self.corner)/self.kT)   # free corner barrier
        self.rate_catalog['Corner_Detach1'] = self.w0*np.exp(-(self.diff+self.edge+self.corner+self.detach)/self.kT)   # corner barrier, break 1 bond
        self.rate_catalog['Corner_Detach2'] = self.w0*np.exp(-(self.diff+self.edge+self.corner+2*self.detach)/self.kT)   # corner barrier, break 2 bonds

        # Create corresponding Process Table, Rate Table, and Move Table to send to solver
        self.rates = []  # Rate Table
        self.moves = []   # Move Table
        self.keys = []  # Table of Keys corresponding to each entry (Move_Barriers)

        for i in range(len(Moves)):
            for j in range(len(Barriers[i])):
                self.rates.append(self.rate_catalog[Barriers[i][j]])
                self.moves.append([Moves_X[i],Moves_Y[i],Moves_Z[i]])
                self.keys.append((Moves[i]+'_'+Barriers[i][j]))

        self.rates = np.array(self.rates,dtype=np.double)
        self.moves = np.array(self.moves,dtype=np.int32)

    def Downward_Funneling_Moves(self):
        """ Define Downward Funneling Moves. Only in-plane coordinate changes. """

        # Array is 1D and defines move of x coordinate. Left or Right
        if self.dim == '2D':
            DF_Moves = np.array([-1,1],dtype=np.int8)

        # Array is 2D, first index is x-position, 2nd index is y-position. Order is north, east, south, west
        elif self.dim == '3D':
            DF_Moves = np.array([[0,1],[1,0],[0,-1],[-1,0]],dtype=np.int8)

        return DF_Moves

# End of Module


import streamlit as st
import pandas as pd

DATA_URL = 'uber-raw-data-sep14.csv'
DATE_COLUMN ='Date/Time'

st.title("Activate  Caso UBER A01702048")
st.subheader("Desarrollar un código para una aplicación web que permita visualizar en un mapa de la ciudad de Nueva York los viajes realizados por la empresa Uber. ")
#filter

@st.cache
def load_data(nrows):
    data = pd.read_csv(DATA_URL, nrows=nrows)
    lowercase = lambda x: str(x).lower()
    data.rename(lowercase, axis='columns', inplace=True)
    data['date/time'] = pd.to_datetime(data['date/time'])
    data['time_hour'] = pd.to_datetime(data['date/time']).dt.hour

    return data

data = load_data(1000)

Horas = data['time_hour'].unique().tolist()
hora = st.selectbox('¿Que hora de viajes quieres ver?', Horas,0)
data = data[data['time_hour']==hora]

st.dataframe(data)
st.map(data)


"""
utils.py contains helper function to deal with creating founders,
processing genetic map and processing sample maps.
"""

import numpy as np
import allel
import pandas as pd
import os
import scipy.interpolate

from .person import Person, create_new, create_new_non_rec

def get_chm_info(genetic_map,vcf_data):

    """
    get chromosome length in morgans from genetic map.
    Assumes genetic_map is sorted.
    """

    # getting chm number...
    chm = list(set(vcf_data["variants/CHROM"]))
    if len(chm) == 1:
        chm = chm[0]
    else:
        raise Exception("Multiple chromosomes in this file!!!")
    chm = chm.lstrip("chr") # in some reference files we have this instead of just chr number. 22 or chr22

    # read in genetic map and subset to chm number.
    genetic_df = pd.read_csv(genetic_map,delimiter="\t",header=None,comment="#",dtype=str)
    genetic_df.columns = ["chm","pos","cM"]
    genetic_chm = genetic_df[genetic_df["chm"]==chm]

    if len(genetic_chm) == 0:
        genetic_chm = genetic_df[genetic_df["chm"]=="chr"+chm] # sometimes it is called chr22 instead of 22

    genetic_chm = genetic_chm.astype({"chm":str,"pos":int,"cM":float})

    # get length of chm.
    chm_length_morgans = max(genetic_chm["cM"])/100.0

    # get snp info - snps in the vcf file and their cm values.
    # then compute per position probability of being a breapoint.
    # requires some interpolation and finding closest positions.
    """
    # 1: Minimum in a sorted array approach and implemented inside admix().
        - O(logn) every call to admix. Note that admix() takes O(n) anyway.
    # 2: Find probabilities using span. - One time computation.

    """
    # This adds 0 overhead to code runtime.
    # get interpolated values of all reference snp positions
    genomic_intervals = scipy.interpolate.interp1d(x=genetic_chm["pos"].to_numpy(), y=genetic_chm["cM"].to_numpy(),fill_value="extrapolate")
    genomic_intervals = genomic_intervals(vcf_data["variants/POS"])
    # Method 1: a bit incorrect - to ise midpoints
    # # get their midpoints. goal is to get length spanned by each reference snp in the chromosome
    # midpts = (genomic_intervals[0:-1] + genomic_intervals[1:])/2
    # start_end_pts = np.concatenate((np.array([0]),midpts,np.array([genomic_intervals[-1]])))
    # lengths = start_end_pts[1:] - start_end_pts[0:-1]
    # # normalize the lengths after removing first since first point is not a breaking point by design
    # bp = lengths[1:] / lengths[1:].sum()
    # Method 2: simpler and more accurate
    lengths = genomic_intervals[1:] - genomic_intervals[0:-1]
    bp = lengths / lengths.sum()

    genetic_map_data = {}
    genetic_map_data["chm"] = chm
    genetic_map_data["chm_length_snps"] = vcf_data["calldata/GT"].shape[0]
    genetic_map_data["chm_length_morgans"] = chm_length_morgans
    genetic_map_data["breakpoint_probability"] = bp

    return genetic_map_data

def get_sample_map_data(sample_map, vcf_data, sample_weights = None):

    """
    Inputs:
    sample_map: tab delimited file with sample, population and no header.
    vcf_data: allel.read_vcf output. It is the reference vcf file information.

    Returns:
    sample_map_data: dataframe with sample, population, population_code and index in vcf_data referecnce.

    """

    # reading sample map
    sample_map_data = pd.read_csv(sample_map,delimiter="\t",header=None,comment="#")
    sample_map_data.columns = ["sample","population"]

    # creating ancestry map into integers from strings
    # id is based on order in sample_map file.
    ancestry_map = {}
    curr = 0
    for i in sample_map_data["population"]:
        if i in ancestry_map.keys():
            continue
        else:
            ancestry_map[i] = curr
            curr += 1
    # print("Ancestry map",ancestry_map)
    sample_map_data["population_code"] = np.vectorize(ancestry_map.get)(sample_map_data["population"])

    # getting index of samples in the reference files

    b = vcf_data["samples"]
    a = np.array(list(sample_map_data["sample"]))

    sorter = np.argsort(b)
    indices = sorter[np.searchsorted(b, a, sorter=sorter)]
    sample_map_data["index_in_reference"] = indices

    if sample_weights is not None:
        sample_weights_df = pd.read_csv(sample_weights,delimiter="\t",header=None,comment="#")
        sample_weights_df.columns = ["sample","sample_weight"]
        sample_map_data = pd.merge(sample_map_data, sample_weights_df, on='sample')

    else:
        sample_map_data["sample_weight"] = [1.0/len(sample_map_data)]*len(sample_map_data)

    return sample_map_data

def build_founders(vcf_data, genetic_map_data, sample_map_data):

    """
    Returns founders - a list of Person datatype.
    founders_weight - a list with a weight for each sample in founders

    Inputs
    genetic_map_data - output of get_chm_info

    """

    # information about snps, chm, lengths from reference, genetic map.
    chm = genetic_map_data["chm"]
    chm_length_morgans = genetic_map_data["chm_length_morgans"]
    chm_length_snps = genetic_map_data["chm_length_snps"]

    # building founders
    founders = []

    for i in sample_map_data.iterrows():

        # first get the index of this sample in the vcf_data.
        # if not there, skip and print to log.

        index = i[1]["index_in_reference"]

        name = i[1]["sample"]

        # when creating maternal, paternal make sure it has same keys

        maternal = {}
        paternal = {}

        # let us use the first for maternal in the vcf file...
        maternal["snps"] = vcf_data["calldata/GT"][:,index,0]
        paternal["snps"] = vcf_data["calldata/GT"][:,index,1]

        # single ancestry assumption.
        maternal["anc"] = np.array([i[1]["population_code"]]*chm_length_snps)
        paternal["anc"] = np.array([i[1]["population_code"]]*chm_length_snps)

        # any more info like coordinates, prs can be added here.

        p = Person(chm,chm_length_morgans,chm_length_snps,maternal,paternal,name)

        founders.append(p)

    founders_weight = sample_map_data["sample_weight"]

    return founders,founders_weight

def create_dataset(founders,founders_weight,num_samples_per_gen,gens_to_ret,breakpoint_probability=None,random_seed=42,verbose=True):

    np.random.seed(random_seed)

    if breakpoint_probability is None:
        print("Warning! Genetic map not taken into account when sampling breakpoints.")
        print("Uniformly sampling from given snps.")

    max_gen = max(gens_to_ret)

    overall = {}
    overall[0] = founders

    # could be greater than or equal to 100 to avoid founder bias.
    number_of_admixed_samples = max(400,num_samples_per_gen)

    for gen in range(1,max_gen+1):
        if verbose:
            print("Simulating generation ",gen)
        this_gen_people = []

        for i in range(number_of_admixed_samples):
            # select any 2 parents from prev. gen.
            # if 1st generation, choose from founders
            if gen == 1:
                id1,id2 = np.random.choice(len(founders),size=2,replace=False, p=founders_weight)
            # else choose from num_samples_per_gen
            else:
                id1,id2 = np.random.choice(number_of_admixed_samples,size=2,replace=False)
            p1 = overall[gen-1][id1]
            p2 = overall[gen-1][id2]

            adm = create_new(p1,p2,breakpoint_probability)
            this_gen_people.append(adm)

        overall[gen] = this_gen_people

        if gen-1 not in gens_to_ret and gen-1 !=0:
            del overall[gen-1]

    # for every requested generation, only keep num_samples_per_gen
    for key in overall.keys():
        if key == 0:
            continue
        overall[key] = overall[key][0:num_samples_per_gen]
        # overall[key] = list(np.random.choice(overall[key], \
        #                            size=num_samples_per_gen,replace=False))

    return overall

def create_non_rec_dataset(founders,founders_weight,num_samples_per_gen,gens_to_ret,breakpoint_probability=None,random_seed=42,verbose=True):

    np.random.seed(random_seed)

    if breakpoint_probability is None:
        print("genetic map not taken into account when sampling breakpoints.")
        print("Uniformly sampling from given snps.")

    max_gen = max(gens_to_ret)

    overall = {}
    overall[0] = founders

    # could be greater than or equal to 100 to avoid founder bias.
    number_of_admixed_samples = num_samples_per_gen

    for gen in gens_to_ret:

        # BUG fix: Gen ==0 means do nothing.
        if gen == 0:
            continue

        if verbose:
            print("Simulating generation ",gen)
        this_gen_people = []

        for i in range(number_of_admixed_samples):
            # use the new admixture tool for non-recursive simulation

            adm = create_new_non_rec(founders,founders_weight,gen,breakpoint_probability)
            this_gen_people.append(adm)

        overall[gen] = this_gen_people

    return overall

def write_output(root,dataset,verbose=True):

    """
    creates output numpy files in root directory - under that, we will have gen_{}
    folders and then we have the npy files.
    Make sure root has chm number in it.

    """

    if not os.path.isdir(root):
        os.makedirs(root)

    for gen in dataset.keys():

        if verbose:
            print("Writing generation: {}".format(gen))

        if gen not in dataset.keys():
            if verbose:
                print("Did not simulate gen {}".format(gen))
            continue

        # make directory
        gen_dir = os.path.join(root, "gen_{}".format(gen))
        if not os.path.isdir(gen_dir):
            os.makedirs(gen_dir)

        snps = []
        anc = []
        for person in dataset[gen]:
            snps.append(person.maternal["snps"])
            snps.append(person.paternal["snps"])
            anc.append(person.maternal["anc"])
            anc.append(person.paternal["anc"])

        # create npy files.
        snps = np.stack(snps)
        np.save(gen_dir+"/mat_vcf_2d.npy",snps)

        # create map files.
        anc = np.stack(anc)
        np.save(gen_dir+"/mat_map.npy",anc)


#!/usr/bin/env python3
# -*- coding utf-8 -*-

from PIL import Image
from transformers import CLIPProcessor, CLIPModel

"""

"""

if __name__ == '__main__':
    # 加载clip-vit-base-patch32模型
    model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
    processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")

    # 加载了一张猫的图片
    image_file = "./data/cat.jpg"
    image =  Image.open(image_file)

    # 用文本标识分成四类
    categories = ["cat", "dog", "truck", "couch"]
    categories_text = list(map(lambda x: f"a photo of a {x}", categories))

    # 用softmax算法来计算图片分类到某一类的概率
    inputs = processor(text=categories_text, images=image, return_tensors="pt", padding=True)
    outputs = model(**inputs)
    logits_per_image = outputs.logits_per_image
    probs = logits_per_image.softmax(dim=1)

    # 输出概率
    for i in range(len(categories)):
        print(f"{categories[i]}\t{probs[0][i].item():.2%}")

"""
输出：
cat  74.51%         # 图片中有两只猫
dog  0.39%
truck  0.04%
couch  25.07%       # 图片中有沙发
"""


import pytest
from middle_of_the_linked_list import ListNode, middle_node

def list_from_array(nums):
    if not nums:
        return None
    head = ListNode(nums[0])
    current = head
    for num in nums[1:]:
        current.next = ListNode(num)
        current = current.next
    return head

def array_from_list(node):
    result = []
    while node:
        result.append(node.val)
        node = node.next
    return result

@pytest.mark.parametrize("head, expected", [
    ([0], [0]),
    ([6], [6]),
    ([0, 1], [1]),
    ([1, 2, 3], [2, 3]),
    ([0, 1], [1]),
    ([1, 0], [0]),
    ([1, 0, 2, 0, 3, 0, 4], [0, 3, 0, 4]),
    ([1, 0, -2, 6, 3, 0, 5, 4, 0], [3, 0, 5, 4, 0]),
    ([1, 0, -2, 6, 3, 7, 5, 4, 0, 8], [7, 5, 4, 0, 8]),
    ([1, 2, 3, 4, 5], [3, 4, 5]),
    ([1, 2, 3, 4, 5, 6], [4, 5, 6])
])
def test_middle_of_the_linked_list(head, expected):
    linked_list = list_from_array(head)
    middle_nodes = middle_node(linked_list)
    assert array_from_list(middle_nodes) == expected


from future_diffusion.disco import *
import subprocess
import torch
import cv2
import gc
import math
import requests
from glob import glob
from types import SimpleNamespace
from torch.nn import functional as F
import torchvision.transforms as T
from CLIP import clip
import torchvision.transforms.functional as TF
from guided_diffusion.script_util import create_model_and_diffusion, model_and_diffusion_defaults
import random
if is_colab:
    os.chdir('/content')
    from google.colab import files
else:
    os.chdir(f'{PROJECT_DIR}')
from IPython.display import Image as ipyimg
from numpy import asarray
import torch
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
import GPUtil
from pynvml import *

nvmlInit()
# SimpleNamespace(**self.args)

def getClipModels(args):
    print('cip models', args)
    clip_models = []
    if args.ViTB32 is True: clip_models.append(clip.load('ViT-B/32', jit=False)[0].eval().requires_grad_(False).to(device))
    if args.ViTB16 is True: clip_models.append(clip.load('ViT-B/16', jit=False)[0].eval().requires_grad_(False).to(device) )
    if args.ViTL14 is True: clip_models.append(clip.load('ViT-L/14', jit=False)[0].eval().requires_grad_(False).to(device) )
    if args.RN50 is True: clip_models.append(clip.load('RN50', jit=False)[0].eval().requires_grad_(False).to(device))
    if args.RN50x4 is True: clip_models.append(clip.load('RN50x4', jit=False)[0].eval().requires_grad_(False).to(device))
    if args.RN50x16 is True: clip_models.append(clip.load('RN50x16', jit=False)[0].eval().requires_grad_(False).to(device))
    if args.RN50x64 is True: clip_models.append(clip.load('RN50x64', jit=False)[0].eval().requires_grad_(False).to(device))
    if args.RN101 is True: clip_models.append(clip.load('RN101', jit=False)[0].eval().requires_grad_(False).to(device))

    return clip_models

class DiscoModel():
    def __init__(self):
        self.intermediate_saves = 0#@param{type: 'raw'}
        self.intermediates_in_subfolder = True #@param{type: 'boolean'}
        self.diffusion_sampling_mode = 'ddim'
        self.use_secondary_model = True
        self.use_checkpoint = True
        self.diffusion_model = "512x512_diffusion_uncond_finetune_008100"
        self.model_config = model_and_diffusion_defaults()
        self.model_config.update({
            'attention_resolutions': '32, 16, 8',
            'class_cond': False,
            'diffusion_steps': 1000, #No need to edit this, it is taken care of later.
            'rescale_timesteps': True,
            'timestep_respacing': 250, #No need to edit this, it is taken care of later.
            'image_size': 512,
            'learn_sigma': True,
            'noise_schedule': 'linear',
            'num_channels': 256,
            'num_head_channels': 64,
            'num_res_blocks': 2,
            'resblock_updown': True,
            'use_checkpoint': True,
            'use_fp16': True,
            'use_scale_shift_norm': True,
        })

        self.models = {
            'ViTB32': True,
            'ViTB16': False,
            'ViTL14': False,
            'RN101': False,
            'RN50': False,
            'RN50x4': False,
            'RN50x16': False,
            'RN50x64': False
        }
        self.models = SimpleNamespace(**self.models)

        self.clip_models = getClipModels(self.models)

        model_default = self.model_config['image_size']
        self.animation_mode = 'None'
        #@markdown ####**Basic Settings:**
        self.batch_name = 'TimeToDisco' #@param{type: 'string'}
        self.steps = 250 #@param [25,50,100,150,250,500,1000]{type: 'raw', allow-input: true}
        self.width_height = [512, 512]#@param{type: 'raw'}
        self.clip_guidance_scale = 5000 #@param{type: 'number'}
        self.tv_scale =  0#@param{type: 'number'}
        self.range_scale =   150#@param{type: 'number'}
        self.sat_scale =   0#@param{type: 'number'}
        self.cutn_batches = 1  #@param{type: 'number'}
        self.skip_augs = False#@param{type: 'boolean'}

        #@markdown ---

        #@markdown ####**Init Settings:**
        self.init_image = None #@param{type: 'string'}
        self.init_scale = 2000 #@param{type: 'integer'}
        self.skip_steps = 125 #@param{type: 'integer'}
        #@markdown *Make sure you set skip_steps to ~50% of your steps if you want to use an init image.*

        #Get corrected sizes
        self.side_x = (self.width_height[0]//64)*64;
        self.side_y = (self.width_height[1]//64)*64;
        if self.side_x != self.width_height[0] or self.side_y != self.width_height[1]:
          print(f'Changing output size to {self.side_x}x{self.side_y}. Dimensions must by multiples of 64.')

        #Update Model Settings
        self.timestep_respacing = f'ddim{self.steps}'
        self.diffusion_steps = (1000//self.steps)*self.steps if self.steps < 1000 else self.steps
        self.model_config.update({
            'timestep_respacing': self.timestep_respacing,
            'diffusion_steps': self.diffusion_steps,
        })

        #Make folder for batch
        self.batchFolder = f'{outDirPath}/{self.batch_name}'
        createPath(self.batchFolder)

        if type(self.intermediate_saves) is not list:
          if self.intermediate_saves:
            self.steps_per_checkpoint = math.floor((self.steps - self.skip_steps - 1) // (self.intermediate_saves+1))
            self.steps_per_checkpoint = self.steps_per_checkpoint if self.steps_per_checkpoint > 0 else 1
            print(f'Will save every {self.steps_per_checkpoint} steps')
          else:
            self.steps_per_checkpoint = self.steps+10
        else:
          self.steps_per_checkpoint = None

        if self.intermediate_saves and self.intermediates_in_subfolder is True:
          partialFolder = f'{self.batchFolder}/partials'
          createPath(partialFolder)

        self.args = None

        self.perlin_init = False  #@param{type: 'boolean'}
        self.perlin_mode = 'mixed' #@param ['mixed', 'color', 'gray']
        self.set_seed = 'random_seed' #@param{type: 'string'}
        self.eta = 0.8#@param{type: 'number'}
        self.clamp_grad = True #@param{type: 'boolean'}
        self.clamp_max = 0.05 #@param{type: 'number'}

        ### EXTRA ADVANCED SETTINGS:
        self.randomize_class = True
        self.clip_denoised = False
        self.fuzzy_prompt = False
        self.rand_mag = 0.05

        self.cut_overview = "[12]*400+[4]*600" #@param {type: 'string'}
        self.cut_innercut ="[4]*400+[12]*600"#@param {type: 'string'}
        self.cut_ic_pow = 1#@param {type: 'number'}
        self.cut_icgray_p = "[0.2]*400+[0]*600"#@param {type: 'string'}

        self.text_prompts = {
            0: None
        }

        self.image_prompts = {
            # 0:['ImagePromptsWorkButArentVeryGood.png:2',],
        }

        """# 4. Diffuse!"""

        #@title Do the Run!
        #@markdown `n_batches` ignored with animation modes.
        self.display_rate =  15 #@param{type: 'number'}
        self.n_batches =  1 #@param{type: 'number'}

        random.seed()
        self.seed = random.randint(0, 2**32)

        #Update Model Settings
        self.timestep_respacing = f'ddim{self.steps}'
        self.diffusion_steps = (1000//self.steps)*self.steps if self.steps < 1000 else self.steps
        self.model_config.update({
            'timestep_respacing': self.timestep_respacing,
            'diffusion_steps': self.diffusion_steps,
        })

        self.batch_size = 1

        print('Prepping model...')
        self.model, self.diffusion = create_model_and_diffusion(**self.model_config)
        self.model.load_state_dict(torch.load(f'{model_path}/{diffusion_model}.pt', map_location='cpu'))
        self.model.requires_grad_(False).eval().to(device)
        self.isBusy = False
        for name, param in self.model.named_parameters():
            if 'qkv' in name or 'norm' in name or 'proj' in name:
                param.requires_grad_()
        if self.model_config['use_fp16']:
            self.model.convert_to_fp16()

        self.resume_run = False #@param{type: 'boolean'}
        self.run_to_resume = 'latest' #@param{type: 'string'}
        self.resume_from_frame = 'latest' #@param{type: 'string'}
        self.retain_overwritten_frames = False #@param{type: 'boolean'}
        if self.retain_overwritten_frames is True:
          self.retainFolder = f'{self.batchFolder}/retained'
          createPath(self.retainFolder)

        self.skip_step_ratio = int(frames_skip_steps.rstrip("%")) / 100
        self.calc_frames_skip_steps = math.floor(self.steps * self.skip_step_ratio)

        self.base_init_path = "init_images"

        if self.steps <= self.calc_frames_skip_steps:
          sys.exit("ERROR: You can't skip more steps than your total steps")

    def move_files(self, start_num, end_num, old_folder, new_folder):
        for i in range(start_num, end_num):
            old_file = old_folder + f'/{self.batch_name}({self.batchNum})_{i:04}.png'
            new_file = new_folder + f'/{self.batch_name}({self.batchNum})_{i:04}.png'
            os.rename(old_file, new_file)

  #@markdown ---

    def showUtilizationGPU(self):
        h = nvmlDeviceGetHandleByIndex(0)
        info = nvmlDeviceGetMemoryInfo(h)
        print(f'total    : {info.total}')
        print(f'free     : {info.free}')
        print(f'used     : {info.used}')
        print()
        print()
        GPUtil.showUtilization()

    def isModelBusy(self):
      return self.isBusy

    def settings(self, id, prompt, init_image,init_scale ,skip_steps ,width ,height ,clip_guidance_scale ,tv_scale ,sat_scale,range_scale, cutn_batches):
      import re
      import urllib

      urls = []

      print("INIT IMAHE:: ", init_image, type(init_image), init_image == "")

      if init_image != "":
          print("USING RE", init_image != "")
          urls = re.findall('https?://+', init_image)

      if urls==[]:
        init_image = None

      counter = 0
      retrieved_image = False

      print("After all, init image:", init_image, urls)

      while True and counter!=3 and init_image:
        try:
          imgURL = init_image
          response = requests.get(imgURL)
          if response.status_code == 200:
              file_path = os.path.join(self.base_init_path, f"{id}.png")
              Path(file_path).touch()
              with open(file_path, 'wb') as f:
                  f.write(response.content)
              retrieved_image = True
              break
          else:
            raise Exception
        except Exception as e:
          print("Failure to download {} : {}".format(imgURL, e))
          print("Retrying download: {}/3".format(counter))
          print()
          try:
            open(file_path, 'a').close()
          except Exception as e:
            print("opening file failed.. skipping.")
            pass

        counter+=1

      if not retrieved_image and init_image:
        raise Exception("Failed to retrieved init image... passing.")

      if init_image:
        init_image = f"init_images/{id}.png"

      image_prompts = {}

      self.text_prompts[0] = prompt

      start_frame = 0
      batchNum = len(glob(self.batchFolder+"/*.txt"))
      while os.path.isfile(f"{self.batchFolder}/{self.batch_name}({batchNum})_settings.txt") is True or os.path.isfile(f"{self.batchFolder}/{self.batch_name}-{batchNum}_settings.txt") is True:
        batchNum += 1

      print(f'Starting Run: {self.batch_name}({batchNum}) at frame {start_frame}')

      print("SKIP_STEPS == ", skip_steps)

      self.args = {
          'batchNum': batchNum,
          'id' : id,
          'model' : self.model,
          'diffusion' : self.diffusion,
          'clip_models': self.clip_models,
          'prompts_series':split_prompts(self.text_prompts) if self.text_prompts else None,
          'image_prompts_series':split_prompts(image_prompts) if image_prompts else None,
          'seed': self.seed,
          'display_rate':self.display_rate,
          'n_batches':self.n_batches if self.animation_mode == 'None' else 1,
          'batch_size':self.batch_size,
          'batch_name': self.batch_name,
          'steps': self.steps,
          'diffusion_sampling_mode': self.diffusion_sampling_mode,
          'width_height': [width,height],
          'clip_guidance_scale': clip_guidance_scale,
          'tv_scale': tv_scale,
          'range_scale': range_scale,
          'sat_scale': sat_scale,
          'cutn_batches': cutn_batches,
          'init_image': init_image,
          'init_scale': init_scale,
          'skip_steps': skip_steps,
          'side_x': (int(width)//64)*64,
          'side_y':(int(height)//64)*64,
          'timestep_respacing': self.timestep_respacing,
          'diffusion_steps': self.diffusion_steps,
          'animation_mode': self.animation_mode,
          'video_init_path': video_init_path,
          'extract_nth_frame': extract_nth_frame,
          'video_init_seed_continuity': video_init_seed_continuity,
          'key_frames': key_frames,
          'max_frames': max_frames if self.animation_mode != "None" else 1,
          'interp_spline': interp_spline,
          'start_frame': start_frame,
          'midas_depth_model': midas_depth_model,
          'midas_weight': midas_weight,
          'near_plane': near_plane,
          'far_plane': far_plane,
          'fov': fov,
          'padding_mode': padding_mode,
          'sampling_mode': sampling_mode,
          'frames_scale': frames_scale,
          'calc_frames_skip_steps': self.calc_frames_skip_steps,
          'skip_step_ratio': self.skip_step_ratio,
          'calc_frames_skip_steps': self.calc_frames_skip_steps,
          'text_prompts': self.text_prompts,
          'image_prompts': image_prompts,
          'use_secondary_model': self.use_secondary_model,
          'cut_overview': eval(self.cut_overview),
          'cut_innercut': eval(self.cut_innercut),
          'cut_ic_pow': self.cut_ic_pow,
          'cut_icgray_p': eval(self.cut_icgray_p),
          'intermediate_saves': self.intermediate_saves,
          'intermediates_in_subfolder': self.intermediates_in_subfolder,
          'steps_per_checkpoint': self.steps_per_checkpoint,
          'perlin_init': self.perlin_init,
          'perlin_mode': self.perlin_mode,
          'set_seed': self.set_seed,
          'eta': self.eta,
          'clamp_grad': self.clamp_grad,
          'clamp_max': self.clamp_max,
          'skip_augs': self.skip_augs,
          'randomize_class': self.randomize_class,
          'clip_denoised': self.clip_denoised,
          'fuzzy_prompt': self.fuzzy_prompt,
          'rand_mag': self.rand_mag,
      }

      # print(self.args)

      # print("**", self.args['init_image'])

      self.args = SimpleNamespace(**self.args)

    def runModel(self):
        self.showUtilizationGPU()
        print("Run model.. setting is busy to True!")
        self.isBusy = True
        gc.collect()
        torch.cuda.empty_cache()

        print(f"Running model with width: {self.args.width_height[0]} and height {self.args.width_height[0]}")
        print("About to run model..")
        do_run(self.args)
        print("Setting is busy to false!")
        self.showUtilizationGPU()
        self.isBusy = False
        print('Seed used:', self.seed)
        gc.collect()
        torch.cuda.empty_cache()
        res = subprocess.check_output(["nvidia-smi",  "-L"])
        print(res)


import sys

from django.core.management.base import BaseCommand
from django.http import Http404

from health_check.mixins import CheckMixin

class Command(CheckMixin, BaseCommand):
    help = "Run health checks and exit 0 if everything went well."

    def add_arguments(self, parser):
        parser.add_argument("-s", "--subset", type=str, nargs=1)

    def handle(self, *args, **options):
        # perform all checks
        subset = options.get("subset", [])
        subset = subset[0] if subset else None
        try:
            errors = self.check(subset=subset)
        except Http404 as e:
            self.stdout.write(str(e))
            sys.exit(1)

        for plugin_identifier, plugin in self.filter_plugins(subset=subset).items():
            style_func = self.style.SUCCESS if not plugin.errors else self.style.ERROR
            self.stdout.write(
                "{:<24} ... {}\n".format(
                    plugin_identifier, style_func(plugin.pretty_status())
                )
            )

        if errors:
            sys.exit(1)


import numpy as np
from astropy import units as u
from astropy.coordinates import Angle
from astropy.time import Time

from ctapipe.instrument.guess import guess_telescope
from ctapipe.instrument import TelescopeDescription, CameraGeometry, \
    OpticsDescription, SubarrayDescription
from ctapipe.io.eventsource import EventSource

from target_io import WaveformArrayReader
from target_calib import CameraConfiguration

from .containers import TargetIODataContainer
from .calibration import TargetIOR1Calibrator

__all__ = [
    'TargetIOEventSource',
    'TargetIOR1Calibrator',
]

class TargetIOEventSource(EventSource):
    """
    EventSource for the targetio unofficial data format, the data
    format used by cameras containing TARGET modules, such as CHEC for
    the GCT SST.

    Extract waveform information from `target_io` to store them
    into `ctapipe.io.containers`.

    This Extractor can fill either the R0 or R1 event container, depending on
    the file being read (The header of the file is checked for a flag
    indicating that it has had R1 calibration applied to it).

    This reader requires the TARGET libraries. The instructions to install
    these libraries can be found here:
    https://forge.in2p3.fr/projects/gct/wiki/Installing_CHEC_Software

    Attributes
    ----------
    _tio_reader : target_io.TargetIOEventReader()
        C++ event reader for TargetIO files. Handles the event building into
        an array of (n_pixels, n_samples) in C++, avoiding loops in Python
    _n_events : int
        number of events in the fits file
    _n_samples : int
        number of samples in the waveform
    _r0_samples : ndarray
        three dimensional array to store the R0 level waveform for each pixel
        (n_channels, n_pixels, n_samples)
    _r1_samples : ndarray
        three dimensional array to store the R1 level waveform for each pixel
        (n_channels, n_pixels, n_samples)
    _samples : ndarray
        pointer to the first index of either r0_samples or r1_samples
        (depending if the file has been R1 calibrated) for passing to
        TargetIO to be filled
    """

    def __init__(self, config=None, parent=None, **kwargs):
        super().__init__(config=config, parent=parent, **kwargs)

        self._data = None
        self._event_index = None
        self._event_id = 0
        self._time_tack = None
        self._time_sec = None
        self._time_ns = None

        self._reader = WaveformArrayReader(self.input_url, 2, 1)

        self._n_events = self._reader.fNEvents
        self._first_event_id = self._reader.fFirstEventID
        self._last_event_id = self._reader.fLastEventID
        self._obs_id = self._reader.fRunID
        n_modules = self._reader.fNModules
        n_pix = self._reader.fNPixels
        n_samples = self._reader.fNSamples
        self.camera_config = CameraConfiguration(self._reader.fCameraVersion)
        self._n_cells = self.camera_config.GetNCells()
        m = self.camera_config.GetMapping(n_modules == 1)

        self._optical_foclen = u.Quantity(2.15, u.m)
        self._mirror_area = u.Quantity(14.126, u.m ** 2)
        self._n_pixels = m.GetNPixels()
        self._xpix = np.array(m.GetXPixVector()) * u.m
        self._ypix = np.array(m.GetYPixVector()) * u.m
        self._refshape = np.zeros(10)  # TODO: Get correct values for CHEC-S
        self._refstep = 0  # TODO: Get correct values for CHEC-S
        self._time_slice = 0  # TODO: Get correct values for CHEC-S
        self._chec_tel = 0

        # Init fields
        self._r0_samples = None
        self._r1_samples = None
        self._first_cell_ids = np.zeros(n_pix, dtype=np.uint16)

        # Check if file is already r1 (Information obtained from a flag
        # in the file's header)
        is_r1 = self._reader.fR1
        if is_r1:
            self._r1_samples = np.zeros(
                (1, n_pix, n_samples),
                dtype=np.float32
            )
            self._get_tio_event = self._reader.GetR1Event
            self._samples = self._r1_samples[0]
        else:
            self._r0_samples = np.zeros(
                (1, n_pix, n_samples),
                dtype=np.uint16
            )
            self._get_tio_event = self._reader.GetR0Event
            self._samples = self._r0_samples[0]

        self._init_container()

    @staticmethod
    def is_compatible(file_path):
        return file_path.endswith('.tio')

    def _init_container(self):
        """
        Prepare the ctapipe event container, and fill it with the information
        that does not change with event, including the instrument information.
        """
        chec_tel = 0

        data = TargetIODataContainer()
        data.meta['origin'] = "targetio"

        data.meta['input'] = self.input_url
        data.meta['max_events'] = self.max_events

        # Instrument information

        camera = CameraGeometry(
            "CHEC",
            pix_id=np.arange(self._n_pixels),
            pix_x=self._xpix,
            pix_y=self._ypix,
            pix_area=None,
            pix_type='rectangular',
        )

        optics = OpticsDescription(
            name="ASTRI",
            num_mirrors=2,
            equivalent_focal_length=self._optical_foclen,
            mirror_area=self._mirror_area,
            num_mirror_tiles=2,
        )

        tel_descriptions = {
            chec_tel: TelescopeDescription(
                name="ASTRI",
                type="SST",
                camera=camera,
                optics=optics,
            )
        }
        tel_positions = {
            chec_tel: u.Quantity(0, u.m)
        }

        data.inst.subarray =SubarrayDescription(
            "CHECMonoArray",
            tel_positions=tel_positions,
            tel_descriptions=tel_descriptions,
        )

        self._data = data

    def _update_container(self):
        """
        Update the ctapipe event containers with the information from the
        current event being pointed to in TargetIO.
        """
        data = self._data
        chec_tel = 0

        obs_id = self._obs_id
        event_id = self._event_id
        tels = {self._chec_tel}

        data.r0.obs_id = obs_id
        data.r0.event_id = event_id
        data.r0.tels_with_data = tels
        data.r1.obs_id = obs_id
        data.r1.event_id = event_id
        data.r1.tels_with_data = tels
        data.dl0.obs_id = obs_id
        data.dl0.event_id = event_id
        data.dl0.tels_with_data = tels

        data.trig.tels_with_trigger = [chec_tel]

        data.meta['tack'] = self._time_tack
        data.meta['sec'] = self._time_sec
        data.meta['ns'] = self._time_ns
        data.trig.gps_time = Time(self._time_sec * u.s, self._time_ns * u.ns,
                                  format='unix', scale='utc', precision=9)

        data.count = self._event_index

        data.r0.tel.clear()
        data.r1.tel.clear()
        data.dl0.tel.clear()
        data.dl1.tel.clear()
        data.mc.tel.clear()
        data.targetio.tel.clear()

        # load the data per telescope/chan
        data.r0.tel[chec_tel].waveform = self._r0_samples
        data.r1.tel[chec_tel].waveform = self._r1_samples

        # Load the TargetIO specific data per telescope/chan
        data.targetio.tel[chec_tel].first_cell_ids = self._first_cell_ids
        data.r0.tel[chec_tel].num_samples = self._samples.shape[-1]

        # Some information that currently exists in the mc container, but is
        # useful for real data (essentially the reference pulse shape,
        # which may be used in charge extraction methods)
        data.mc.tel[chec_tel].reference_pulse_shape = self._refshape
        data.mc.tel[chec_tel].meta['refstep'] = self._refstep
        data.mc.tel[chec_tel].time_slice = self._time_slice

    @property
    def _current_event_index(self):
        return self._event_index

    @_current_event_index.setter
    def _current_event_index(self, val):
        """
        Setting the event index will cause the event to be saught from
        TargetIO, and the Containers to point to
        the correct event. The ctapipe event containers are then updated
        with this new event's information.
        """
        self._event_index = val
        self._get_tio_event(val, self._samples, self._first_cell_ids)
        self._event_id = self._reader.fCurrentEventID
        self._time_tack = self._reader.fCurrentTimeTack
        self._time_sec = self._reader.fCurrentTimeSec
        self._time_ns = self._reader.fCurrentTimeNs
        self._update_container()

    def _generator(self):
        for self._current_event_index in range(self._n_events):
            yield self._data
        return

    def __len__(self):
        num = self._n_events
        if self.max_events and self.max_events < num:
            num = self.max_events
        return num

    def _get_event_by_index(self, index):
        self._current_event_index = index
        return self._data

    def _get_event_by_id(self, event_id):
        if ((event_id < self._first_event_id) |
                (event_id > self._last_event_id)):
            raise IndexError(f"Event id {event_id} not found in file")
        index = self._reader.GetEventIndex(event_id)
        return self._get_event_by_index(index)


#!/usr/bin/env python3
from collections import deque
def main():
    N = int(input())
    g = [[] for _ in range(N)]

    for _ in range(N-1):
        A, B = map(int,input().split())
        A, B = A-1, B-1
        g[A].append(B)
        g[B].append(A)

    colors = [-1] * N
    colors[0] = 0
    q = deque()
    q.append(0)

    while q:
        v = q.popleft()

        for nv in g[v]:
            if colors[nv] != -1:
                continue

            colors[nv] = not colors[v]
            q.append(nv)

    zero, one = [], []
    for idx, itm in enumerate(colors):
        if itm == 0:
            zero.append(idx+1)
        else:
            one.append(idx+1)

        if len(zero) == N//2:
            return zero

        if len(one) == N//2:
            return one

print(*main())


import pandas as pd
import numpy as np
import string

def stripStr(name):
	return name.rstrip(string.digits)

#read csv file
file = pd.read_csv("./ahs2017n.csv", quotechar = "'")

# Get all the valid column name:
columnNameSet = set()
f = open('./column_name')
for line in f.readlines():
	line = line.strip()
	if len(line) > 3 and line[-3:] == 'XXX':
		columnNameSet.add(line[:-3])
	else:
		columnNameSet.add(line)

#Get all the data with valid column from the csv
columnList = []
for i in range(len(file.columns)):
	if file.columns[i] in columnNameSet or stripStr(file.columns[i]) in columnNameSet:
		columnList.append(file.columns[i])

#replace all the non-applicable data
for j in range(-9, -5):
	file.replace(j, np.nan, inplace = True)

#drop columns where all the values are NaN
df = file[columnList]
df.dropna(axis = 1, how = 'all', thresh = 40051, inplace = True)

#drop columns which only contain 1 unique values. (variance ＝ 0)
df = df[[col for col in df if not df[col].nunique()==1]]

df.to_csv('./cleanData.csv', index = False)

#drop columns with low variance
import pandas
from sklearn.feature_selection import VarianceThreshold
sel =VarianceThreshold(threshold=1)
sel.fit_transform(data)

m=0
for i in sel.get_support(True):
	print(data.columns[i])
	m+=1
print(m)

#use selectFromModel
data=pandas.read_csv('/Users/sherry/Desktop/python/energyProject/cleanData.csv',quotechar="'")
data=data.astype('float64', inplace = True)
data.dropna(inplace=True)

feature = data[[col for col in data.columns if col!="GASAMT"]]
lrModel = LinearRegression()
selectFromModel = SelectFromModel(lrModel)
selectFromModel.fit_transform(feature,data['GASAMT'])

nums = selectFromModel.get_support(True)
lists=[]
for i in nums:
	lists.append(data.columns[i])
print(lists)

#use RFE
import pandas
from sklearn.feature_selection import RFE
from sklearn.linear_model import LinearRegression

data=pandas.read_csv('/Users/sherry/Desktop/python/energyProject/cleanData.csv',quotechar="'")
data=data.astype('float64', inplace = True)
data.dropna(inplace=True)
data=data.loc[1:9]

feature =data[['TOTROOMS','TOTHCAMT','PERPOVLVL','DIVISION','BLD','HHMAR','HHAGE','HHGRAD','HHNATVTY']]

rfe =RFE(
    estimator=LinearRegression(),
    n_features_to_select=5
)
sFeature = rfe.fit_transform(
    feature,
    data['ELECAMT']
)

rfe.get_support()


from PyQt5.QtWidgets import QGraphicsRectItem, QGraphicsScene
from PyQt5.QtGui import QColor

class BlockDiagram(QGraphicsScene):
    def __init__(self, blocks, block_labels, title):
        super(BlockDiagram, self).__init__()
        self.blocks = blocks
        self.block_labels = block_labels
        self.title = title

        self.scene = QGraphicsScene()
        self.scene.setSceneRect(0, 0, 750, 700)

    def add_tree_items(self, block_type): # switch colour depending on type (expeneses / income)
        for it in self.blocks: # loop through dict list
            # Construct a square object
            rect = QGraphicsRectItem(it["x"], it["y"], it["dx"], it["dy"])

            if block_type == "income":
                rect.setBrush(QColor.fromRgb(97, 247, 255))

            elif block_type == "expenses":
                rect.setBrush(QColor.fromRgb(255, 105, 97))

            self.scene.addItem(rect)

        return self.scene


# Using While Loop
n = input("") #input() function takes input as string type

n = int(n) # convert it to integer type
fact = 1
i = 1
while i <= n :
    fact = fact * i
    i += 1
print(fact)

#Using For Loop
n = int(input())

fact = 1
for i in range(1,n+1):
    fact = fact * i
print(fact)

#Using Lambda Function
n = int(input())
def shortfact(x):
    return 1 if x <= 1 else x*shortfact(x-1)
print(shortfact(n))

#Using reduce
from functools import reduce

def fun(acc, item):
    return acc*item

num = int(input())
print(reduce(fun,range(1, num+1), 1))

#Reduce how work

#The reduce function is used to perform a specific operation on a sequence of values.
#  In this case, it will be used to multiply all the numbers from 1 to num together to calculate the factorial.

#A custom function fun is defined, which will be used as the operation to apply to the values in the sequence.
#  This function takes two arguments, acc (short for "accumulator") and item. It returns the product of acc and item, which effectively accumulates the product of all the items in the sequence.

#The reduce function is called as follows:

print(reduce(fun, range(1, num + 1), 1))
#reduce takes three arguments:
#The first argument is the function (fun) to apply cumulatively to the items in the sequence.
#The second argument is the sequence itself, which is generated using range(1, num + 1). This creates a sequence of numbers from 1 to num.
#The third argument is the initial value for the accumulator, which is set to 1. This is the starting point for the multiplication.
#The reduce function then performs the following operations:

#Initially, acc is set to 1 (the initial value), and item is set to the first element of the sequence (1).
#It applies the fun function to these values, so acc becomes 1 * 1 = 1.
#Then, acc is set to 1 (the result of the previous step) and item is set to the next element in the sequence (2).
#This process continues, accumulating the product of the numbers from 1 to num through the fun function.
#The result is printed using print, which will be the factorial of the input num.


import datetime
import os
import re

import da.util

_DIRNAME_EXPIRATION    = 'expiration'
_FILEEXT_EXPIRATION    = 'expiration_date'
_DATEFMT_EXPIRATION    = '%Y%m%d%H%M'
_REGEX_EXPIRATION_FILE = r'^[0-9]{12}\.[a-z_]{3,60}\.%s$' % _FILEEXT_EXPIRATION

# -----------------------------------------------------------------------------
def _filename(current_time, timedelta_expiration, identifier):
    """
    Return the expiration marker filename for the specified "current" datetime.

    """
    current_hour = datetime.datetime(
                                year   = current_time.year,
                                month  = current_time.month,
                                day    = current_time.day,
                                hour   = current_time.hour,
                                minute = 0,
                                second = 0,
                                tzinfo = current_time.tzinfo)
    datetime_expiration    = current_hour + timedelta_expiration
    sz_datetime_expiration = datetime_expiration.strftime(_DATEFMT_EXPIRATION)
    filename_expiration    = '{expiration}.{identifier}.{fileext}'.format(
                                expiration = sz_datetime_expiration,
                                identifier = identifier,
                                fileext    = _FILEEXT_EXPIRATION)
    return filename_expiration

# -----------------------------------------------------------------------------
def set_expiration_date(dirpath_build_cms,
                        identifier,
                        current_time,
                        timedelta_expiration):
    """
    Set the expiration date for the specified CMS entry.

    """
    # Make sure the expiration-dates directory exists.
    dirpath_expiration = os.path.join(dirpath_build_cms, _DIRNAME_EXPIRATION)
    da.util.ensure_dir_exists(dirpath_expiration)

    # Set the new expiration date.
    filename_expiration = _filename(
                                current_time, timedelta_expiration, identifier)
    filepath_expiration = os.path.join(dirpath_expiration, filename_expiration)
    with open(filepath_expiration, 'wb'):
        pass

    # Delete existing expiration dates with the same owner.
    for (path, _, file_id) in _iter_expiration_files(dirpath_expiration):

        is_same_file = (path == filepath_expiration)
        if is_same_file:
            continue

        # ID indicates who set the expiration - so they can reset too.
        is_same_id = (file_id == identifier)
        if is_same_id:
            os.remove(path)

# -----------------------------------------------------------------------------
def has_expired(dirpath_build_cms, time_now):
    """
    Return true if the specified build has expired and may be safely deleted.

    Builds which are no longer required by any test
    or archival process are deleted to reduce waste.
    Processes may indicate if they require a build
    to be kept alive for a time by setting an
    expiration date.

    A process is only deemed to have expired if
    all expiration dates which have been set are
    in the past.

    """
    for (_, expiration, _) in _iter_expiration_files(
                                            os.path.join(dirpath_build_cms,
                                                         _DIRNAME_EXPIRATION)):
        if (expiration is not None) and (expiration > time_now):
            return False
    return True

# -----------------------------------------------------------------------------
def _iter_expiration_files(dirpath_expiration):
    """
    Yield tuples with expiration data for all files in the specified directory.

    Expiration dates are set by creating files in
    a specified expiration directory in the configuration
    management system. This function iterates over
    all such files in the specified expiration directory,
    yielding a tuple for each expiration file found.

    The tuple contains (path, expiration_date, owner).

    """
    if not os.path.isdir(dirpath_expiration):
        return
    regex_expiration_file = re.compile(_REGEX_EXPIRATION_FILE)
    for name in os.listdir(dirpath_expiration):
        path = os.path.join(dirpath_expiration, name)
        if not (    os.path.isfile(path)
                and re.match(regex_expiration_file, name)):
            raise RuntimeError(
                   'Unexpected file found in expiration folder: %s.', path)
        (sz_file_expiration, file_owner, _) = name.split('.')
        file_expiration = datetime.datetime.strptime(
                                    sz_file_expiration, _DATEFMT_EXPIRATION)
        yield (path, file_expiration, file_owner)


from fastapi import APIRouter, Depends
from starlette import status

from app.db.base import customers_collection, favourite_books_collection
from app.models.customers import CustomerModel
from app.utils.utils import get_current_active_user, get_error_response

router = APIRouter(
    prefix="/customer",
    tags=["Customer"],
    responses={404: {
        "description": "Not found"
    }},
)

@router.get('/customer-list')
async def customer_list(user: object = Depends(get_current_active_user)):
    find_query = {
        "is_deleted": False
    }
    if user is None:
        return get_error_response("Unauthorized",
                                  status.HTTP_401_UNAUTHORIZED)
    customers_list = list(customers_collection.find(find_query))
    customers = [CustomerModel(**x).customer_list_response() for x in customers_list]
    response = {
        "customers": customers
    }
    return response

@router.get('/customers-with-favorite-books')
async def get_customers_with_favourite_books_count(user: object = Depends(get_current_active_user)):
    find_query = {
        "is_deleted": False
    }
    customers_list = list(customers_collection.find(find_query))

    favourite_books = list(favourite_books_collection.find())
    customers_response_list = []
    for customer in customers_list:
        favourite_book = next((a.get("favourite_books_list") for a in favourite_books
                               if a.get('customer_id') == customer.get('_id')), None)
        customer_response = CustomerModel(**customer).customer_list_response()
        customer_response['favourite_book_count'] = 0
        if favourite_book is not None:
            customer_response['favourite_book_count'] = len(favourite_book.split(','))
        customers_response_list.append(customer_response)
    response = {
        "customer_response": customers_response_list
    }
    return response


import os, pyautogui, time, datetime, json

# subprocess.Popen([r"C:\Program Files (x86)\AgencyAp\SIMMS v10.4.0\simms.exe"])

CurrentPath = os.getcwd()
#print(CurrentPath)

import webbrowser

url = 'https://i5.readyop.com/fs/4cim/9fb7'

try:
    chrome_path = 'open -a /Applications/Google\ Chrome.app %s'
    webbrowser.get(chrome_path).open(url)
    Mac = True
except:
    Mac = False
print(Mac)
try:
    chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
    webbrowser.get(chrome_path).open(url)
    Windows = True
except:
    Windows = False
print(Windows)
try:
    chrome_path = '/usr/bin/google-chrome %s'
    webbrowser.get(chrome_path).open(url)
    Linux = True
except:
    Linux = FalseWRS
print(Linux)

os.chdir(CurrentPath)
WFile = 'PersonalInfomration.JSON'
W_File_Open = open(WFile)
WDict_ST = W_File_Open.read()
WDict = json.loads(WDict_ST)
print(WDict)

LName = WDict['LName']
FName = WDict['FName']
EmployeeID = WDict['EmployeeID']
OrgCode = WDict['OrgCode']

TMC = False
Remote = True
NWR = True
enter = [LName, FName, EmployeeID, OrgCode]
print(enter)
time.sleep(6)
k = 0
while k < len(enter):
        pyautogui.write('\t')
        time.sleep(.05)
        PN = enter[k]
        WAns = str(PN)
        WAns = WAns.capitalize()
        print(WAns)
        if k < 2:
            pyautogui.keyDown('shift')
            pyautogui.typewrite(WAns[0], interval=0.005)
            pyautogui.keyUp('shift')
            pyautogui.typewrite(WAns[1:], interval=0.005)
        else:
            pyautogui.typewrite(WAns, interval=0.005)
        time.sleep(.05)
        k = k + 1
print('Complete')


#!/usr/bin/env python3
import sys, pandas, numpy, itertools, logging, argparse

parser = argparse.ArgumentParser()
parser.add_argument('open')
parser.add_argument('turnout')
parser.add_argument('incD')
parser.add_argument('incR')
parser.add_argument('votes')
args = parser.parse_args()

logging.basicConfig(format='%(asctime)s - %(filename)s - %(message)s',
    stream=sys.stderr, level=logging.INFO)

logging.info('Reading input files...')

# Democratic vote share in file 1,
# absolute turnout in file 2 scaled by .01 in upstream R code
dem_share = pandas.read_csv(args.open)
input_sims = len([c for c in dem_share.columns if c.startswith('v.')])
output_sims, rows = 350, dem_share.shape[0]

turnout = pandas.read_csv(args.turnout)
turnout.iloc[:, -input_sims:] *= 100

incd_share = pandas.read_csv(args.incD)
incr_share = pandas.read_csv(args.incR)

logging.info('Creating output frame...')

# Ignore the blank first column of input to prepare votes output,
# build an empty frame with 6x output_sims columns for DEM and REP votes
# in three candidate scenarios, concatenate both into a complete votes frame.
votes1 = dem_share.iloc[:, 1:-input_sims]
votes2 = pandas.DataFrame(data=numpy.empty((rows, output_sims*6)), dtype=float,
    columns=[f'{inc}:{party}{sim:03d}' for (inc, sim, party)
        in itertools.product(('O', 'D', 'R'), range(output_sims), ('DEM', 'REP'))])

votes = pandas.concat((votes1, votes2), axis=1)

# Iterate over simulations populating Democratic and Republican vote shares
s_cols, t_cols = dem_share.columns[-input_sims:], turnout.columns[-input_sims:]
d_cols, r_cols = incd_share.columns[-input_sims:], incr_share.columns[-input_sims:]

for (sim, s_col, t_col, d_col, r_col) in zip(range(output_sims), s_cols, t_cols, d_cols, r_cols):
    turnout_val, share_val = turnout[t_col], dem_share[s_col]
    incd_val, incr_val = incd_share[d_col], incr_share[r_col]

    # Calculate shares for open seat and incumbent scenarios
    open_dem_share = share_val
    open_rep_share = (1 - share_val)
    incd_dem_share = (share_val + incd_val)
    incd_rep_share = (1 - (share_val + incd_val))
    incr_dem_share = (share_val + incr_val)
    incr_rep_share = (1 - (share_val + incr_val))

    # Assign raw votes counts rounded to one decimal place
    votes[f'O:DEM{sim:03d}'] = round(turnout_val * open_dem_share, 1)
    votes[f'O:REP{sim:03d}'] = round(turnout_val * open_rep_share, 1)
    votes[f'D:DEM{sim:03d}'] = round(turnout_val * incd_dem_share, 1)
    votes[f'D:REP{sim:03d}'] = round(turnout_val * incd_rep_share, 1)
    votes[f'R:DEM{sim:03d}'] = round(turnout_val * incr_dem_share, 1)
    votes[f'R:REP{sim:03d}'] = round(turnout_val * incr_rep_share, 1)

logging.info('Writing output file...')
votes.to_csv(args.votes, index=False, compression='gzip')
logging.info('Done.')


from PyQt5.QtWidgets import QApplication
from mainWindow import MainWindow
from inicial_window import InicialWindow
from final_window import FinalWindow
from entidad_dcc import DCC
from el_que_perdio_es_malo import TheLoserGuy
import sys

app = QApplication([])
lista_mapa = []
window = MainWindow()
inicial_window = InicialWindow()
final_window = FinalWindow()
dcc = DCC(window.display_x, window.display_y, lista_mapa)
loser = TheLoserGuy()
inicial_window.senal_nuevo_juego.connect(dcc.nuevo_juego)
inicial_window.senal_comenzar_ronda.connect(window.comienzo_ronda)
dcc.senal_nuevo_juego_armar.connect(window.iniciar_ventana_nueva)
window.senal_extender_lista_dcc.connect(dcc.actualizar_lista_extendida)
window.senal_nueva_reputacion.connect(dcc.mas_reputacion)

inicial_window.senal_continuar_juego.connect(dcc.cargar_mapa_seguir_juego) #se continua juego con datos anterioresa
dcc.senal_mandar_mapa.connect(window.cargar_mapa) #se envía mapa de datos anteriores. acá me falta actualizar el mapa y datos

dcc.senal_el_chef_cocina.connect(window.chef_trabajando)
dcc.senal_plato_recogido.connect(window.chef_trabajando)

window.senal_comenzar_ronda.connect(dcc.comienzo_ronda_dcc)
window.senal_traer_clientes.connect(dcc.arma_lista_clientes)
dcc.senal_nuevos_clientes.connect(window.llegada_clientes)
dcc.senal_aparece_bocadillo.connect(window.cliente_come)
dcc.senal_nuevo_estado_cliente.connect(window.metodo_cliente)
dcc.senal_termino.connect(window.cliente_termino)
window.senal_fin_ronda.connect(dcc.ronda_terminada)

window.senal_mover_jugador.connect(dcc.gamer.mover)
dcc.gamer.senal_nueva_posicion.connect(window.nueva_posicion)

window.senal_nuevo_drop.connect(dcc.revisar_cruce)
dcc.senal_comprobacion.connect(window.compra)
window.senal_acutalizar_lista.connect(dcc.actualizar_lista)
window.senal_eliminar_objeto.connect(dcc.eliminar_objeto)

dcc.senal_abrir_post_ronda.connect(final_window.armemos_y_abramos)
dcc.senal_perdiste.connect(loser.goodbye_my_friend)
dcc.senal_perdiste.connect(window.salir_perdedor)
final_window.senal_enviar_datos_guardar.connect(dcc.guardar_datos)

inicial_window.show()
sys.exit(app.exec_())