from re import M
import sys, getopt, os
# from mpi4py import MPI
# rank = MPI.COMM_WORLD.Get_rank()
# # set only one visible device
# os.environ["CUDA_VISIBLE_DEVICES"] = f"{rank}"
# os.environ["JAX_PLATFORM_NAME"] = "cpu"
sys.path.append('/storage/praha1/home/mezic/.local/lib/python3.7/site-packages')
import netket as nk
import numpy as np
import jax, flax, optax
# import mpi4jax
import time
import json
print("Python version: {}".format(sys.version))
print("NetKet version: {}".format(nk.__version__))
print("NumPy version: {}".format(np.__version__))
print("Jax devices: {}".format(jax.devices()))
print("MPI utils available: {}".format(nk.utils.mpi.available))

file = sys.argv[-1]
if len(sys.argv) == 1:
    file = "config_benchmark_table"
print(file)
fq = __import__(file)
from lattice_and_ops import Lattice
from lattice_and_ops import Operators
from lattice_and_ops import HamOps
from lattice_and_ops import log_results
from pRBM import pRBM
ho = HamOps()

PREFIX = "logs/"
if not os.path.exists(PREFIX):
    os.mkdir(PREFIX)
OUT_NAME = PREFIX+"models"+str(fq.SITES) # output file name
OUT_LOG_NAME = PREFIX+"out.txt"            # filename for output logging
print("N = ",fq.SITES, ", samples = ",fq.SAMPLES,", iters = ",fq.NUM_ITER, ", sampler = ",fq.SAMPLER, ", TOTAL_SZ = ", fq.TOTAL_SZ, ", machine = ", fq.MACHINE, ", dtype = ", fq.DTYPE, ", alpha = ", fq.ALPHA, ", eta = ", fq.ETA, sep="")
with open(OUT_LOG_NAME,"a") as out_log_file:
    out_log_file.write("Jax devices: {}".format(jax.devices()))
    out_log_file.write("MPI utils available: {}".format(nk.utils.mpi.available))
    out_log_file.write("N = {}, samples = {}, iters = {}, sampler = {}, TOTAL_SZ = {}, machine = {}, dtype = {}, alpha = {}, eta = {}\n".format(fq.SITES,fq.SAMPLES,fq.NUM_ITER,fq.SAMPLER, fq.TOTAL_SZ,fq.MACHINE, fq.DTYPE, fq.ALPHA, fq.ETA))
with open('out-models_table.txt','a') as table_file:
    table_file.write("# N = {}, samples = {}, iters = {}, sampler = {}, TOTAL_SZ = {}, machine = {}, dtype = {}, alpha = {}, eta = {}\n".format(fq.SITES,fq.SAMPLES,fq.NUM_ITER,fq.SAMPLER, fq.TOTAL_SZ,fq.MACHINE, fq.DTYPE, fq.ALPHA, fq.ETA))
    table_file.write("# i       name      params  time    eta  DS: min_s  avg_err  min_err  MSR: min_s  avg_err  min_err  AF: min_s  avg_err  min_err  MSR: min_s  avg_err  min_err\n")

lattice = Lattice(fq.SITES)

# Define custom graph
edge_colors = []
for node in range(fq.SITES):
    edge_colors.append([node,lattice.rt(node), 1]) #horizontal connections
    edge_colors.append([node,lattice.bot(node), 1]) #vertical connections
    row, column = lattice.position(node)
    if column%2 == 0:
        if row%2 == 0:
            edge_colors.append([node,lattice.lrt(node),2])
        else:
            edge_colors.append([node,lattice.llft(node),2])

# Define the netket graph object
g = nk.graph.Graph(edges=edge_colors)

hilbert = nk.hilbert.Spin(s=.5, N=g.n_nodes, total_sz=fq.TOTAL_SZ)

# This part is only relevant for GCNN or pRBM machine
print("There are", len(g.automorphisms()), "full symmetries.")
# deciding point between DS and AF phase is set to 0.5
#####################""" assume only the fully symmetric models """##########################
characters_1 = np.ones((len(g.automorphisms()),), dtype=complex)
characters_2 = characters_1

# Hamiltonian definition
hd_1 = nk.operator.GraphOperator(hilbert, graph=g, bond_ops=ho.bond_operator(fq.JEXCH1D,fq.JEXCH2, use_MSR=False), bond_ops_colors=ho.bond_color)
hd_2 = nk.operator.GraphOperator(hilbert, graph=g, bond_ops=ho.bond_operator(fq.JEXCH1D,fq.JEXCH2, use_MSR=True), bond_ops_colors=ho.bond_color)
ha_1 = nk.operator.GraphOperator(hilbert, graph=g, bond_ops=ho.bond_operator(fq.JEXCH1A,fq.JEXCH2, use_MSR=False), bond_ops_colors=ho.bond_color)
ha_2 = nk.operator.GraphOperator(hilbert, graph=g, bond_ops=ho.bond_operator(fq.JEXCH1A,fq.JEXCH2, use_MSR=True), bond_ops_colors=ho.bond_color)

# Exact diagonalization
if g.n_nodes < 20:
    evals = nk.exact.lanczos_ed(ha_1, k=2, compute_eigenvectors=False)
    exact_ground_energy_AF = evals[0]
    evals = nk.exact.lanczos_ed(hd_1, k=2, compute_eigenvectors=False)
    exact_ground_energy_DS = evals[0]
else:
    raise Exception("System too large for ED :-(.")
if fq.VERBOSE:
    print("Exact DS energy:",exact_ground_energy_DS)
    print("Exact AF energy:",exact_ground_energy_AF)
exact_ground_energies = np.array([exact_ground_energy_DS,exact_ground_energy_DS,exact_ground_energy_AF,exact_ground_energy_AF])
threshold_energies = 0.995*exact_ground_energies

# extract translations from the full symmetry group
if not (fq.SITES in [4,16]):
    raise NotImplementedError("Extraction of translations from the group of automorphisms is not implemented yet.")
translations = []
for perm in g.automorphisms():
    aperm = np.asarray(perm)
    if fq.SITES == 4:
        if (aperm[0],aperm[1]) in ((0,1),(1,0),(2,3),(3,2)):
            translations.append(nk.utils.group._permutation_group.Permutation(aperm))
    elif fq.SITES == 16:
        if (aperm[0],aperm[1],aperm[3]) in ((0,1,3),(2,3,1),(8,9,11),(10,11,9)):
            translations.append(nk.utils.group._permutation_group.Permutation(aperm))
translation_group = nk.utils.group._permutation_group.PermutationGroup(translations,degree=fq.SITES)
characters_trans_1 = np.ones((len(translation_group),), dtype=complex)
characters_trans_2 = characters_trans_1

# some parameters of the benchmarking of the models
ETAS = [0.01,0.05,0.2]
no_etas = len(ETAS)
no_of_runs = 4 # types of runs: DS_norm, DS_MSR, AF_norm, AF_MSR
no_repeats = 4 # repeats of the same run due to inconsistent results
name = "none"

for m in fq.INDICES:
    steps_until_conv = np.zeros((no_repeats,no_of_runs))
    min_energy_error = np.zeros((no_repeats,no_of_runs))
    average_final_energy = np.zeros((no_repeats,no_of_runs))
    start = time.time()
    ETA = ETAS[m%no_etas]
    for j in range(no_repeats):
        # model definition
        if m//no_etas == 0:
            name = "RBM_2"
            machine_1 = nk.models.RBM(dtype=fq.DTYPE, alpha=2)
            machine_2 = nk.models.RBM(dtype=fq.DTYPE, alpha=2)
            machine_3 = nk.models.RBM(dtype=fq.DTYPE, alpha=2)
            machine_4 = nk.models.RBM(dtype=fq.DTYPE, alpha=2)
        elif m//no_etas == 1:
            name = "RBM_16"
            machine_1 = nk.models.RBM(dtype=fq.DTYPE, alpha=16)
            machine_2 = nk.models.RBM(dtype=fq.DTYPE, alpha=16)
            machine_3 = nk.models.RBM(dtype=fq.DTYPE, alpha=16)
            machine_4 = nk.models.RBM(dtype=fq.DTYPE, alpha=16)
        elif m//no_etas == 2:
            name = "RBM_2notVisible"
            machine_1 = nk.models.RBM(dtype=fq.DTYPE, alpha=2,use_visible_bias=False)
            machine_2 = nk.models.RBM(dtype=fq.DTYPE, alpha=2,use_visible_bias=False)
            machine_3 = nk.models.RBM(dtype=fq.DTYPE, alpha=2,use_visible_bias=False)
            machine_4 = nk.models.RBM(dtype=fq.DTYPE, alpha=2,use_visible_bias=False)
        elif m//no_etas == 3:
            name = "RBM_16notVisible"
            machine_1 = nk.models.RBM(dtype=fq.DTYPE, alpha=16,use_visible_bias=False)
            machine_2 = nk.models.RBM(dtype=fq.DTYPE, alpha=16,use_visible_bias=False)
            machine_3 = nk.models.RBM(dtype=fq.DTYPE, alpha=16,use_visible_bias=False)
            machine_4 = nk.models.RBM(dtype=fq.DTYPE, alpha=16,use_visible_bias=False)
        elif m//no_etas == 4:
            name = "sRBM_16aut"
            alpha= 16
            machine_1 = nk.models.RBMSymm(g.automorphisms(), dtype=fq.DTYPE, alpha=alpha)
            machine_2 = nk.models.RBMSymm(g.automorphisms(), dtype=fq.DTYPE, alpha=alpha)
            machine_3 = nk.models.RBMSymm(g.automorphisms(), dtype=fq.DTYPE, alpha=alpha)
            machine_4 = nk.models.RBMSymm(g.automorphisms(), dtype=fq.DTYPE, alpha=alpha)
        elif m//no_etas == 5:
            name = "sRBM_128aut"
            alpha = 128
            machine_1 = nk.models.RBMSymm(g.automorphisms(), dtype=fq.DTYPE, alpha=alpha)
            machine_2 = nk.models.RBMSymm(g.automorphisms(), dtype=fq.DTYPE, alpha=alpha)
            machine_3 = nk.models.RBMSymm(g.automorphisms(), dtype=fq.DTYPE, alpha=alpha)
            machine_4 = nk.models.RBMSymm(g.automorphisms(), dtype=fq.DTYPE, alpha=alpha)
        elif m//no_etas == 6:
            name = "sRBM_16trans"
            alpha = 16
            machine_1 = nk.models.RBMSymm(translation_group, dtype=fq.DTYPE, alpha=alpha)
            machine_2 = nk.models.RBMSymm(translation_group, dtype=fq.DTYPE, alpha=alpha)
            machine_3 = nk.models.RBMSymm(translation_group, dtype=fq.DTYPE, alpha=alpha)
            machine_4 = nk.models.RBMSymm(translation_group, dtype=fq.DTYPE, alpha=alpha)
        elif m//no_etas == 7:
            name = "sRBM_2trans"
            alpha = 2
            machine_1 = nk.models.RBMSymm(translation_group, dtype=fq.DTYPE, alpha=alpha)
            machine_2 = nk.models.RBMSymm(translation_group, dtype=fq.DTYPE, alpha=alpha)
            machine_3 = nk.models.RBMSymm(translation_group, dtype=fq.DTYPE, alpha=alpha)
            machine_4 = nk.models.RBMSymm(translation_group, dtype=fq.DTYPE, alpha=alpha)
        elif m//no_etas == 8:
            name = "pRBM_32"
            alpha=32
            machine_1 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_1, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
            machine_2 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_2, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
            machine_3 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_1, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
            machine_4 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_2, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
        elif m//no_etas == 9:
            name = "pRBM_8"
            alpha=8
            machine_1 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_1, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
            machine_2 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_2, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
            machine_3 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_1, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
            machine_4 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_2, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
        elif m//no_etas == 10:
            name = "pRBM_8trans"
            alpha=8
            machine_1 = pRBM(symmetries=translation_group, dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_trans_1, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
            machine_2 = pRBM(symmetries=translation_group, dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_trans_2, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
            machine_3 = pRBM(symmetries=translation_group, dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_trans_1, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
            machine_4 = pRBM(symmetries=translation_group, dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_trans_2, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=True)
        elif m//no_etas == 11:
            name = "pRBM_8notVisible"
            alpha=8
            machine_1 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_1, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=False)
            machine_2 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_2, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=False)
            machine_3 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_1, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=False)
            machine_4 = pRBM(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=1, features=alpha, characters=characters_2, output_activation=nk.nn.log_cosh, use_bias=True, use_visible_bias=False)
        elif m//no_etas == 12:
            name = "GCNN_aut[8,4]"
            num_layers = 2
            feature_dims = (8,4)
            machine_1 = nk.models.GCNN(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=num_layers, features=feature_dims, characters=characters_1)
            machine_2 = nk.models.GCNN(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=num_layers, features=feature_dims, characters=characters_2)
            machine_3 = nk.models.GCNN(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=num_layers, features=feature_dims, characters=characters_1)
            machine_4 = nk.models.GCNN(symmetries=g.automorphisms(), dtype=fq.DTYPE, layers=num_layers, features=feature_dims, characters=characters_2)
        elif m//no_etas == 13:
            name = "GCNN_trans[8,4]"
            num_layers = 2
            feature_dims = (8,4)
            machine_1 = nk.models.GCNN(symmetries=translation_group, dtype=fq.DTYPE, layers=num_layers, features=feature_dims, characters=characters_trans_1)
            machine_2 = nk.models.GCNN(symmetries=translation_group, dtype=fq.DTYPE, layers=num_layers, features=feature_dims, characters=characters_trans_2)
            machine_3 = nk.models.GCNN(symmetries=translation_group, dtype=fq.DTYPE, layers=num_layers, features=feature_dims, characters=characters_trans_1)
            machine_4 = nk.models.GCNN(symmetries=translation_group, dtype=fq.DTYPE, layers=num_layers, features=feature_dims, characters=characters_trans_2)
        elif m//no_etas == 14:
            name = "Jastrow"
            machine_1 = nk.models.Jastrow(dtype=fq.DTYPE)
            machine_2 = nk.models.Jastrow(dtype=fq.DTYPE)
            machine_3 = nk.models.Jastrow(dtype=fq.DTYPE)
            machine_4 = nk.models.Jastrow(dtype=fq.DTYPE)
        elif m//no_etas == 15:
            name = "pmRBM_2"
            machine_1 = nk.models.RBMModPhase(alpha=2, use_hidden_bias=True, dtype=np.float64)
            machine_2 = nk.models.RBMModPhase(alpha=2, use_hidden_bias=True, dtype=np.float64)
            machine_3 = nk.models.RBMModPhase(alpha=2, use_hidden_bias=True, dtype=np.float64)
            machine_4 = nk.models.RBMModPhase(alpha=2, use_hidden_bias=True, dtype=np.float64)
        elif m//no_etas == 16:
            name = "pmRBM_8"
            machine_1 = nk.models.RBMModPhase(alpha=8, use_hidden_bias=True, dtype=np.float64)
            machine_2 = nk.models.RBMModPhase(alpha=8, use_hidden_bias=True, dtype=np.float64)
            machine_3 = nk.models.RBMModPhase(alpha=2, use_hidden_bias=True, dtype=np.float64)
            machine_4 = nk.models.RBMModPhase(alpha=2, use_hidden_bias=True, dtype=np.float64)
        elif m//no_etas == 17:
            name = "Jastrow+b"
            from lattice_and_ops import Jastrow_b
            machine_1 = Jastrow_b()
            machine_2 = Jastrow_b()
            machine_3 = Jastrow_b()
            machine_4 = Jastrow_b()
        else:
            raise Exception(str("undefined MACHINE: ")+str(fq.MACHINE))

        # Selection of sampler type
        if fq.SAMPLER == 'local':
            sampler_1 = nk.sampler.MetropolisLocal(hilbert=hilbert)
            sampler_2 = nk.sampler.MetropolisLocal(hilbert=hilbert)
            sampler_3 = nk.sampler.MetropolisLocal(hilbert=hilbert)
            sampler_4 = nk.sampler.MetropolisLocal(hilbert=hilbert)
        elif fq.SAMPLER == 'exact':
            sampler_1 = nk.sampler.ExactSampler(hilbert=hilbert)
            sampler_2 = nk.sampler.ExactSampler(hilbert=hilbert)
            sampler_3 = nk.sampler.ExactSampler(hilbert=hilbert)
            sampler_4 = nk.sampler.ExactSampler(hilbert=hilbert)
        else:
            sampler_1 = nk.sampler.MetropolisExchange(hilbert=hilbert, graph=g)
            sampler_2 = nk.sampler.MetropolisExchange(hilbert=hilbert, graph=g)
            sampler_3 = nk.sampler.MetropolisExchange(hilbert=hilbert, graph=g)
            sampler_4 = nk.sampler.MetropolisExchange(hilbert=hilbert, graph=g)
            if fq.SAMPLER != 'exchange':
                print("Warning! Undefined fq.SAMPLER:", fq.SAMPLER, ", dafaulting to MetropolisExchange fq.SAMPLER")

        # Optimzer
        if name[:5] != "pmRBM":
            optimizer_1 = nk.optimizer.Sgd(learning_rate=ETA)
            optimizer_2 = nk.optimizer.Sgd(learning_rate=ETA)
            optimizer_3 = nk.optimizer.Sgd(learning_rate=ETA)
            optimizer_4 = nk.optimizer.Sgd(learning_rate=ETA)
        else:
            final_ETA = 2/7*ETA
            optm_1=optax.sgd(optax.linear_schedule(0,final_ETA,fq.NUM_ITER))
            optm_2=optax.sgd(optax.linear_schedule(0,final_ETA,fq.NUM_ITER))
            optm_3=optax.sgd(optax.linear_schedule(0,final_ETA,fq.NUM_ITER))
            optm_4=optax.sgd(optax.linear_schedule(0,final_ETA,fq.NUM_ITER))
            optp_1=optax.sgd(optax.linear_schedule(5*final_ETA,final_ETA,fq.NUM_ITER))
            optp_2=optax.sgd(optax.linear_schedule(5*final_ETA,final_ETA,fq.NUM_ITER))
            optp_3=optax.sgd(optax.linear_schedule(5*final_ETA,final_ETA,fq.NUM_ITER))
            optp_4=optax.sgd(optax.linear_schedule(5*final_ETA,final_ETA,fq.NUM_ITER))
            # The multi-transform optimizer uses different optimisers for different parts of the parameters.
            optimizer_1 = optax.multi_transform({'o1': optm_1, 'o2': optp_1}, flax.core.freeze({"Dense_0":"o1", "Dense_1":"o2"}))
            optimizer_2 = optax.multi_transform({'o1': optm_2, 'o2': optp_2}, flax.core.freeze({"Dense_0":"o1", "Dense_1":"o2"}))
            optimizer_3 = optax.multi_transform({'o1': optm_3, 'o2': optp_3}, flax.core.freeze({"Dense_0":"o1", "Dense_1":"o2"}))
            optimizer_4 = optax.multi_transform({'o1': optm_4, 'o2': optp_4}, flax.core.freeze({"Dense_0":"o1", "Dense_1":"o2"}))

        # Stochastic Reconfiguration
        sr_1 = nk.optimizer.SR(diag_shift=0.01)
        sr_2 = nk.optimizer.SR(diag_shift=0.01)
        sr_3 = nk.optimizer.SR(diag_shift=0.01)
        sr_4 = nk.optimizer.SR(diag_shift=0.01)

        # The variational state (drive to byla nk.variational.MCState)
        vs_1 = nk.vqs.MCState(sampler_1, machine_1, n_samples=fq.SAMPLES)
        vs_2 = nk.vqs.MCState(sampler_2, machine_2, n_samples=fq.SAMPLES)
        vs_1.init_parameters(jax.nn.initializers.normal(stddev=0.01))
        vs_2.init_parameters(jax.nn.initializers.normal(stddev=0.01))
        vs_3 = nk.vqs.MCState(sampler_3, machine_3, n_samples=fq.SAMPLES)
        vs_4 = nk.vqs.MCState(sampler_4, machine_4, n_samples=fq.SAMPLES)
        vs_3.init_parameters(jax.nn.initializers.normal(stddev=0.01))
        vs_4.init_parameters(jax.nn.initializers.normal(stddev=0.01))

        gs_1d = nk.VMC(hamiltonian=hd_1 ,optimizer=optimizer_1,preconditioner=sr_1,variational_state=vs_1)   # DS
        gs_2d = nk.VMC(hamiltonian=hd_2 ,optimizer=optimizer_2,preconditioner=sr_2,variational_state=vs_2)   # DS+MSR
        gs_1a = nk.VMC(hamiltonian=ha_1 ,optimizer=optimizer_3,preconditioner=sr_3,variational_state=vs_3)   # AF
        gs_2a = nk.VMC(hamiltonian=ha_2 ,optimizer=optimizer_4,preconditioner=sr_4,variational_state=vs_4)   # AF+MSR

        ops = Operators(lattice,hilbert,ho.mszsz,ho.exchange)

        MODEL_LOG_NAME = OUT_NAME+"_"+name+"_"+str(ETA)+"_"+str(j)+"_"
        no_of_runs = 4
        for i,gs in enumerate([gs_1d,gs_2d,gs_1a,gs_2a]):
            start = time.time()
            if fq.VERBOSE:
                print("Running",name,"in the", "DS" if i < 2 else "AF", "phase", "without" if i%2==0 else "with", "MSR, learning rate =",ETA)
            gs.run(out=MODEL_LOG_NAME+["DS_normal","DS_MSR","AF_normal","AF_MSR"][i], n_iter=int(fq.NUM_ITER),show_progress=fq.VERBOSE)
            end = time.time()
            if m == 0:
                print("The type {} of {} calculation took {} min".format(i,fq.MACHINE ,(end-start)/60))

        # finding the number of steps needed to converge
        data = []
        for i in range(no_of_runs):
            data.append(json.load(open(MODEL_LOG_NAME+["DS_normal","DS_MSR","AF_normal","AF_MSR"][i]+".log")))
        if type(data[0]["Energy"]["Mean"]) == dict:
            energy_convergence = np.asarray([data[i]["Energy"]["Mean"]["real"] for i in range(no_of_runs)])
        else:
            energy_convergence = np.asarray([data[i]["Energy"]["Mean"] for i in range(no_of_runs)])

        average_final_energy[j] = np.average(energy_convergence[:,-50:],axis=1)
        steps_until_convergence = [next((i for i,v in enumerate(energy_convergence[j]) if v < threshold_energies[j]), np.inf) for j in range(no_of_runs)]
        min_energy_error[j] = [np.min(np.abs(energy_convergence[j] - exact_ground_energies[j])) for j in range(no_of_runs)]
        steps_until_conv[j] = np.asarray(steps_until_convergence)

        if no_of_runs==2:
            log_results(m,gs_1d,gs_2d,ops,fq.SAMPLES,fq.NUM_ITER,exact_energy = m,steps_until_convergence=steps_until_convergence,filename=OUT_LOG_NAME)
        elif no_of_runs==4:
            log_results(m,gs_1d,gs_2d,ops,fq.SAMPLES,fq.NUM_ITER,exact_energy = m,steps_until_convergence=steps_until_convergence,filename=OUT_LOG_NAME)
            log_results(m,gs_1a,gs_2a,ops,fq.SAMPLES,fq.NUM_ITER,exact_energy = m,steps_until_convergence=steps_until_convergence,filename=OUT_LOG_NAME)
        else:
            log_results(m,gs_1d,gs_1d,ops,fq.SAMPLES,fq.NUM_ITER,exact_energy = m,steps_until_convergence=steps_until_convergence,filename=OUT_LOG_NAME)
    end = time.time()

    min_steps = np.min(steps_until_conv,axis=0)
    min_avg_final_energy = np.min(average_final_energy,axis=0)
    min_avg_final_energy_relative_error = -np.log10((exact_ground_energies-min_avg_final_energy)/exact_ground_energies)
    min_min_energy_error = np.min(min_energy_error,axis=0)
    with open('out_benchmark-table.txt','a') as table_file:
                        # i       name      params  time    eta  DS: min_s avg_err min_err  MSR: min_s avg_err min_err  AF: min_s avg_err min_err  MSR: min_s avg_err min_err
        table_file.write("{:2.0f}  {:<17}  {:5.0f}  {:5.1f}  {:4.3f}  {:5.0f} {:5.2f} {:8.3}   {:5.0f} {:5.2f} {:8.3f}   {:5.0f} {:5.2f} {:8.3}  {:5.0f} {:5.2f} {:8.3f}\n".format(m,name,vs_1.n_parameters,(end-start)/60,ETA,min_steps[0],min_avg_final_energy_relative_error[0],min_min_energy_error[0],min_steps[1],min_avg_final_energy_relative_error[1],min_min_energy_error[1],min_steps[2],min_avg_final_energy_relative_error[2],min_min_energy_error[2],min_steps[3],min_avg_final_energy_relative_error[3],min_min_energy_error[3]))


# Траспонирование матрица и композиция одной и той же функции

matrix =[[1, 2, 3,], [4, 5, 6], [7, 8, 9]]

for row in matrix:
	print(*row)

def transpose(matrix):
	transposed_matrix = [list(tpl) for tpl in zip(*matrix)][::-1]
	return transposed_matrix

def repeater(function, power):
	def tmp(x):
		for _ in range(power):
			x = function(x)
		return x
	return tmp

matrix = repeater(transpose, 2)(matrix)

for row in matrix:
	print(*row)

транспонирование и композиция.py

транспонирование и композиция.py


# owner Daine.H
# Modify:2016-01-04

# Logging Level
LOGGINGSWITCH = 1
ALLLOGGING = 100
CMDONLY = 50
NOLOGGING = 0

# Logging Path
CMDLOGPATH = 'wintercome/log/cmdlog/'
APPLOGPATH = 'wintercome/log/applog/'

# Group Level
ADMINISTRATOR = 100
MANAGER = 50
LEADER = 30
PROJECTOWNER = 20
VIEWER = 1

# TIME FORMAT
#LOGTIME='%Y%m%d_%H%M_.log'
CMDLOGFILETIME = '%Y%m%d_%H_.log'
APPLOGFILETIME = '%Y%m%d_.log'
APPLOGTIME = '%Y%m%d_%H%M%S'

# Error Type
RUNLEVELTOOLOW = "用户运行级别不够. \n"


import unittest
import os
from empty_folder_deleter import folder_deleter

class EmptyFolderDeleterTest(unittest.TestCase):

    def test_folder_deleter_1(self):
        folder_deleter("test_files/test_01", verbose_mode=False)
        testcase = "test_01" not in os.listdir("test_files")
        self.assertTrue(testcase)
        os.mkdir("test_files/test_01")

    def test_folder_deleter_2(self):
        folder_deleter("test_files/test_02", verbose_mode=False)
        testcase = "test_02" in os.listdir("test_files")
        self.assertTrue(testcase)

    def test_folder_deleter_3(self):
        folder_deleter("test_files/test_03", verbose_mode=False)
        testcase = "test_03" in os.listdir("test_files")
        self.assertTrue(testcase)

    def test_folder_deleter_4(self):
        folder_deleter("test_files/test_04", verbose_mode=False)
        testcase = "test_04" in os.listdir("test_files")
        self.assertTrue(testcase)

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


from django.db import models
import idutils # for DOI validation: https://idutils.readthedocs.io/en/latest/
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from django.core.validators import MaxValueValidator, MinValueValidator
from django.contrib.auth import get_user_model
import datetime

# Create your models here.

# any model can inherit this one to add these columns
class BaseModel(models.Model):
   name =  models.CharField(max_length=100, unique=True)
   owner = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, blank=True)
   created_on = models.DateField(auto_now_add=True)
   accepted = models.BooleanField(default=False)
   class Meta:
      abstract=True  # this tells Django not to create a table for this model - it's an abstract base class
   def __str__(self):
      return self.name

class DOIField(models.CharField):
   description = "Digital Object Identifier (DOI)"
   def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 128;
        super(DOIField, self).__init__(*args, **kwargs)
   def validate(self, value, obj):
        super().validate(value, obj)
        if not idutils.is_doi(value):
           raise ValidationError(
              _('%(value)s is not a valid DOI'),
               params={'value': value},
           )
   def get_url(self):
     pass      # implement a method to resolve the URL of a DOI
   def get_name(self):
     pass      # implement a method to fetch the document name

class YearField(models.IntegerField):
   def __init__(self, *args, **kwargs):
      super(YearField, self).__init__(*args, **kwargs)
   def validate(self, value, obj):
      super().validate(value, obj)
      if (value < 1791 or value > datetime.date.today().year):
         raise ValidationError("Invalid year")

# I think this would be unnecessary for now - we don't need a full contact info DB!
#class Person(models.Model):
#   first_name = models.CharField()
#   last_name  = models.CharField()
#   institution = models.ForteignKey( ... )

class Paper(models.Model):
  DOI = DOIField(unique=True)  # use our custom DOI field
  paper_tag = models.SlugField(max_length=100, unique=True)  # SlugField is a CharField with validation, like my DOIField
  year = YearField(default=datetime.date.today().year)
  title = models.CharField(max_length=300)
  #authors = models.ManyToManyField(Person) # no - see above
  authors = models.CharField(max_length=500)
  url = models.URLField(null=True, blank=True) # blank=true means not a required field in forms. null=True means don't set NOT NULL in SQL
  created_on = models.DateField(auto_now_add=True)
  accepted = models.BooleanField(default=False)
  def __str__(self):
    return self.paper_tag

# I don't like Material having a column for each chemical compound - that seems nasty.
# Instead I will use one of Django's special ManyToManyFields with a "Through" relationship.
# This specifies the table a mapping table with an extra column for the composition number
# actually, don't even need a "through" for this.

class Compound(models.Model):
  formula = models.CharField(max_length=20, unique=True) #  "Li"
  name = models.CharField(max_length=100, unique=True) #  "Lithium"
  def __str__(self):
    return "%s (%s)"%(self.name,self.formula)

class CompositionPart(models.Model):
  compound = models.ForeignKey(Compound, on_delete=models.CASCADE)
  amount = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(100)])
  def __str__(self):
    return "%s%d"%(self.compound.formula,self.amount)

class Material(BaseModel):
  composition = models.ManyToManyField(CompositionPart)
  polymer = models.IntegerField(default=0,validators=[MinValueValidator(0)])
  def __str__(self):
    return self.name

class Method(BaseModel):
  # what does the 'method class' do??
  # in any case it's a bad idea to have something called class, it gets confusing
  # Using 'choices' will cause Django to use multiple choice validators & drop down menus
  # see: https://docs.djangoproject.com/en/3.1/ref/models/fields/#choices
  METHOD_TYPE_CHOICES = [
    (1, 'MethodType1'),
    (2, 'MethodType2'),
  ]
  type = models.IntegerField(choices=METHOD_TYPE_CHOICES)
  description =  models.TextField(blank=True)

class QuantityUnit(models.Model):
  name = name = models.CharField(max_length=100, unique=True) # Voltage
  symbol = models.CharField(max_length=40, unique=True)  # V
  symbolName = models.CharField(max_length=40, blank=True) # Volts
  def __str__(self):
    return "%s/%s"%(self.name,self.symbol)

class Parameter(BaseModel):
  symbol = models.CharField(max_length=40, unique=True)
  PARAM_TYPE_CHOICES = [
    (1, 'ParamType1'),
    (2, 'ParamType2'),
  ]
  type = models.IntegerField(choices=PARAM_TYPE_CHOICES)
  unit = models.ForeignKey(QuantityUnit, blank=True, null=True, on_delete=models.SET_NULL)
  notes = models.TextField(blank=True)
  def __str__(self):
    return "%s: %s"%(self.name,self.symbol)

class Data(BaseModel):
  paper = models.ForeignKey(Paper, on_delete=models.CASCADE)
  parameter = models.ForeignKey(Parameter, on_delete=models.CASCADE)
  material = models.ForeignKey(Material, on_delete=models.CASCADE)
  DATA_TYPE_CHOICES = [
    (1, 'DataType1'),
    (2, 'DataType2'),
  ]
  type = models.IntegerField(choices=DATA_TYPE_CHOICES)
  data = models.TextField()
  # other fields to be added... I'm not sure what the numrange fields are for?
  class Meta:
     verbose_name_plural="Data"  # don't pluralise to "Datas"


from django.contrib import admin
from .models import *

class MaterialInline(admin.TabularInline):
    model = Material
    fields = ['tipoMaterial','autor','titulo','año','status']
    extra = 0

class PersonaInline(admin.TabularInline):
    model = Persona
    fields = ['tipoPersona','nombre','apellido','correo','telefono','numLibros','adeudo']

class PrestamoAdmin(admin.ModelAdmin):
    inlines = [MaterialInline, PersonaInline]

class LibroAdmin(admin.ModelAdmin):
    list_display = ['tipoMaterial','codigo','autor','titulo','año','status','editorial', 'portada',]
    list_display_links = ['tipoMaterial','codigo','autor','titulo','año','status','editorial',]
    search_fields = ['tipoMaterial','codigo','autor','titulo','año','status','editorial',]
    fieldsets = (
        ('Descripcion', {
            'fields':('tipoMaterial','autor','titulo','año','editorial', 'portada',)
        }),
        ('Variables', {
            'fields':('status','prestamo',)
        }),
    )

class RevistaAdmin(admin.ModelAdmin):
    list_display = ['tipoMaterial','codigo','autor','titulo','año','status',]
    list_display_links = ['tipoMaterial','codigo','autor','titulo','año','status',]
    search_fields = ['tipoMaterial','codigo','autor','titulo','año','status',]
    fieldsets = (
        ('Descripcion', {
            'fields':('tipoMaterial','autor','titulo','año',)
        }),
        ('Variables', {
            'fields':('status','prestamo',)
        }),
    )

class AlumnoAdmin(admin.ModelAdmin):
    list_display = ['matricula','tipoPersona','nombre','apellido','correo','telefono','numLibros', 'adeudo',]
    list_display_links = ['matricula','tipoPersona','nombre','apellido','correo','telefono','numLibros', 'adeudo',]
    search_fields = ['matricula','tipoPersona','nombre','apellido','correo','telefono','numLibros', 'adeudo',]
    fieldsets = (
        ('Descripcion', {
            'fields':('tipoPersona','nombre','apellido','correo','telefono',)
        }),
        ('Variables', {
            'fields':('numLibros', 'adeudo',)
        }),
    )

class ProfesorAdmin(admin.ModelAdmin):
    list_display = ['numEmpleado', 'tipoPersona', 'nombre', 'apellido', 'correo', 'telefono', 'numLibros', 'adeudo',]
    list_display_links = ['numEmpleado', 'tipoPersona', 'nombre', 'apellido', 'correo', 'telefono', 'numLibros', 'adeudo',]
    search_fields = ['numEmpleado', 'tipoPersona', 'nombre', 'apellido', 'correo', 'telefono', 'numLibros', 'adeudo',]
    fieldsets = (
        ('Descripcion', {
            'fields':('tipoPersona', 'nombre', 'apellido', 'correo', 'telefono',)
        }),
        ('Variables', {
            'fields':('numLibros', 'adeudo',)
        }),
    )

# Register your models here.

admin.site.register(Prestamo, PrestamoAdmin)
admin.site.register(Persona)
admin.site.register(Profesor, ProfesorAdmin)
admin.site.register(Alumno, AlumnoAdmin)
admin.site.register(Material)
admin.site.register(Libro, LibroAdmin)
admin.site.register(Revista, RevistaAdmin)


"""
Caesar Cipher Encryption Program
"""
import sys

def textEncryption(plain_txt, n):   ## encrypts the text entered by user
    txt_result = ""

    for char in plain_txt:
        if (char.isupper()):  ## encrypts uppercase characters
            txt_result += chr((ord(char) - 65 + n) % 26 + 65)

        elif (char.islower()): ## encrypts lowercase characters
            txt_result += chr((ord(char) - 97 + n) % 26 + 97)

        else:
            txt_result += char

    return txt_result

if (len(sys.argv)) == 2:     ## there must be no more than two arguments in the system: the name of  the file and the first argument
    num = int(sys.argv[1])
    usr_txt = input("plaintext: ")

    encrypt_result = textEncryption(usr_txt, num)
    print("ciphertext: " + encrypt_result + "\n")

else:
    print("ERROR. EXIT CODE 1. -- Non-negative number 'n' as argument is required.")
    print("Usage: python caesar.py n")


import sys

"""
{4, 2, -3, 6, 1} is sorted to {-3, 1, 2, 4, 6}
"""

class SelectionSort:
    def solve(self, arr: list) -> list:
        # write your solution here
        for i in range(len(arr) - 1):  # 设置成[), 可能就是为了这里len方便？
            # min = -sys.maxsize - 1
            mini = i  # min 是python关键词，用mini maxi
            for j in range(i, len(arr)):
                if arr[j] < arr[mini]:
                    mini = j
            arr[i], arr[mini] = arr[mini], arr[i]
        return arr

def test():
    tmp = SelectionSort()
    arr = [4, 2, -3, 6, 1]
    tmp.solve(arr)
    print(arr)

if __name__ == '__main__':
    test()


# images.py
# work with images, including embedding images into
# Paragraph flowables
from electos.ballotmaker.ballots.files import FileTools

# from reportlab.platypus import Image
from reportlab.lib import utils

PROJECT_NAME = "ballotlab"

class EmbeddedImage:
    """
    EmbeddedImage creates a text string with
    markup to add to a Paragraph flowable
    """

    def __init__(self, image_name, new_width=240) -> None:
        self.image_name = image_name
        self.new_width = new_width
        self.rel_img_path = "assets/img"
        self.embed_text = ""

        image_file = FileTools(self.image_name, self.rel_img_path)
        self.file_check(image_file)
        # retrieve the image and measure it
        self.image_full_path = image_file.abs_path_to_file
        img = utils.ImageReader(self.image_full_path)
        img_width, img_height = img.getSize()
        aspect = img_height / float(img_width)
        # resize based on the new width
        self.new_height = round(new_width * aspect)
        self.embed_text = f'<para leading="{round(self.new_height / 1.9)}" spaceBefore="6" spaceAfter="12">
</para>'

    def file_check(self, image_file):
        if image_file.file_found is False:
            file_error = (
                f"File {self.image_name} not found in {self.rel_img_path}"
            )
            raise FileNotFoundError(file_error)

if __name__ == "__main__":
    embed_img = EmbeddedImage("filled_bubble.png")
    print(embed_img.embed_text)
    # not_an_img = EmbeddedImage("fake_file.png")


from __future__ import print_function, division
import argparse
import random
import numpy as np
from sklearn.cluster import KMeans
from sklearn.metrics.cluster import normalized_mutual_info_score as nmi_score
from sklearn.metrics import adjusted_rand_score as ari_score

from sklearn.metrics import silhouette_score
import torch
from time import time
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter
from sklearn.cluster import Birch
from torch.optim import Adam
from torch.utils.data import DataLoader
from torch.nn import Linear
from utils import load_data, load_graph
from GNN import GNNLayer
from evaluation import eva
from collections import Counter

nb_dimension = 768
class AE(nn.Module):

    def __init__(self, n_enc_1, n_dec_1,
                 n_input, n_z):
        super(AE, self).__init__()
        self.enc_1 = Linear(n_input, n_enc_1)
        self.z_layer = Linear(n_enc_1, n_z)

        self.dec_1 = Linear(n_z, n_dec_1)
        self.x_bar_layer = Linear(n_dec_1, n_input)

    def forward(self, x):
        enc_h1 = F.relu(self.enc_1(x))
        z = self.z_layer(enc_h1)

        dec_h1 = F.relu(self.dec_1(z))
        x_bar = self.x_bar_layer(dec_h1)

        return x_bar, enc_h1,  z
# set seed
random.seed(555)
np.random.seed(555)
torch.manual_seed(555)

class SDCN(nn.Module):

    def __init__(self, n_enc_1,  n_dec_1,
                n_input, n_z, n_clusters, v=1):
        super(SDCN, self).__init__()

        # autoencoder for intra information
        self.ae = AE(
            n_enc_1=n_enc_1,
            n_dec_1=n_dec_1,
            n_input=n_input,
            n_z=n_z)
        self.ae.load_state_dict(torch.load(args.pretrain_path, map_location='cpu'))

        # GCN for inter information
        self.gnn_1 = GNNLayer(n_input, n_enc_1)
        self.gnn_2 = GNNLayer(n_enc_1, n_z)
        self.gnn_3 = GNNLayer(n_z, n_clusters)

        # cluster layer
        self.cluster_layer = Parameter(torch.Tensor(n_clusters, n_z))
        torch.nn.init.xavier_normal_(self.cluster_layer.data)

        # degree
        self.v = v

    def forward(self, x, adj):
        # DNN Module
        x_bar, tra1, z = self.ae(x)

        sigma = 0.5

        # GCN Module
        h = self.gnn_1(x, adj)
        h = self.gnn_2((1-sigma)*h + sigma*tra1, adj)
        h = self.gnn_3((1-sigma)*h + sigma*z, adj, active=False)
        predict = F.softmax(h, dim=1)

         # Dual Self-supervised Module
        q = 1.0 / (1.0 + torch.sum(torch.pow(z.unsqueeze(1) - self.cluster_layer, 2), 2) / self.v)
        q = q.pow((self.v + 1.0) / 2.0)
        q = (q.t() / torch.sum(q, 1)).t()

        return x_bar, q, predict, z

def target_distribution(q):
    weight = q**2 / q.sum(0)
    return (weight.t() / weight.sum(1)).t()

def train_sdcn(dataset): #only : dataset
    print(len(dataset.x[0]))
    model = SDCN( 1000, 1000,
                n_input=args.n_input,
                n_z=args.n_z,
                n_clusters=args.n_clusters,
                v=1.0).to(device)
    print(model)
    print(len(dataset.x[0]))

    optimizer = Adam(model.parameters(), lr=args.lr)

    # KNN Graph
    adj = load_graph(args.name, args.k)
    adj = adj.cuda()

    # cluster parameter initiate
    data = torch.Tensor(dataset.x).to(device)
    y = dataset.y
    with torch.no_grad():
        _, _, z = model.ae(data)

#   for kmeans initialization
#    kmeans = KMeans(n_clusters=args.n_clusters, n_init=2, random_state= 2)
#    y_pred = kmeans.fit_predict(z.data.cpu().numpy())
#    y_pred_last = y_pred
#    model.cluster_layer.data = torch.tensor(kmeans.cluster_centers_).to(device)

# for initialization
    brc = Birch(n_clusters=args.n_clusters)
    y_pred = brc.fit_predict(z.data.cpu().numpy())
    y_pred_last = y_pred
    n_top_clusters = len(set(y_pred))
    cluster_centers = np.zeros((n_top_clusters, z.shape[1]), dtype=np.float32)
    np.add.at(cluster_centers, y_pred, z.cpu().float().numpy())
    cluster_centers /= np.bincount(y_pred, minlength=n_top_clusters)[:, None].astype(np.float32)
    model.cluster_layer.data = torch.tensor(cluster_centers).to(device)
#

    eva(y, y_pred,0, 'pae')
    nb_epochs = 100
    for epoch in range(nb_epochs):
        if epoch % 1 == 0:
        # update_interval
            _, tmp_q, pred, _ = model(data, adj)
            tmp_q = tmp_q.data
            p = target_distribution(tmp_q)

            res1 = tmp_q.cpu().numpy().argmax(1)       #Q

            res2 = pred.data.cpu().numpy().argmax(1)   #Z
            res3 = p.data.cpu().numpy().argmax(1)      #P
            sil= silhouette_score(tmp_q.cpu().numpy(), tmp_q.cpu().numpy().argmax(1))
            eva(y, res1,sil, str(epoch) + 'Q')
            ari, acc = eva(y, res2, sil, str(epoch) + 'Z')
            eva(y, res3,sil, str(epoch) + 'P')

        x_bar, q, pred, _ = model(data, adj)

        kl_loss = F.kl_div(q.log(), p, reduction='batchmean')
        ce_loss = F.kl_div(pred.log(), p, reduction='batchmean')
        re_loss = F.mse_loss(x_bar, data)

        loss = 0.1 * kl_loss + 0.01 * ce_loss + re_loss
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='train',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--name', type=str, default='X') # 'reut' - 'X'
    parser.add_argument('--k', type=int, default=0) # version(number) of graph generated for this dataset
    parser.add_argument('--lr', type=float, default=1e-3)
    parser.add_argument('--n_clusters', default=26, type=int)
    parser.add_argument('--n_z', default=100, type=int)
    parser.add_argument('--pretrain_path', type=str, default='pkl')

    args = parser.parse_args()
    args.cuda = torch.cuda.is_available()
    print("use cuda: {}".format(args.cuda))
    device = torch.device("cuda" if args.cuda else "cpu")

    args.pretrain_path = 'data/{}.pkl'.format(args.name)
    dataset = load_data(args.name)
    print(len(dataset.x[0]))

    if args.name == 'X':
        args.n_clusters = 26 #number of class
        args.n_input = nb_dimension

    print(args)
    start = time()
    train_sdcn(dataset)
    end = time()
    print("Training took ",{end-start}," sec to run.")


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

from openstack import connection
import requests
import sys
from openstack import utils

# open debug mode
# utils.enable_logging(debug=True, stream=sys.stdout)

# igore ssl warnning
requests.packages.urllib3.disable_warnings()

domain = "myhuaweicloud.com"  # cdn use: domain = "myhuaweicloud.com"
region = "cn-north-1"  # example: region = "cn-north-1"

def handler(event, context):
    ak = context.getAccessKey()
    sk = context.getSecretKey()
    projectId = context.getProjectID()

    logger = context.getLogger()

    if ak == "" or sk == "":
        logger.error(
            "Failed to access EVS because no temporary AK, SK, or token has been obtained. Please set an agency.")
        return ("Failed to access EVS because no temporary AK, SK, or token has been obtained. Please set an agency.")

    if projectId == "":
        logger.error("Failed to access EVS because projectId is empty")
        return ("Failed to access EVS because projectId is empty.")

    conn = connection.Connection(verify=False,
                                 project_id=projectId,
                                 domain=domain,
                                 region=region,
                                 ak=ak,
                                 sk=sk)
    create_volume_by_args(conn)

# create volume
def create_volume_by_args(_conn):
    data = {
        "availability_zone": "az1.dc1",
        "size": 10,
        "name": "volume_by_no_resource_key",
        "volume_type": "SSD",
        "count": 1,
        "metadata": {
            "__system__encrypted": "0",
            "hw:passthrough": "false"
        },
        "multiattach": True
    }

    ff = _conn.evs.create_volume(**data)
    print(ff)

# get volume
def get_volume(_conn, volume_id):
    detail = _conn.evs.get_volume(volume_id)
    print(detail.name)

# list volume
def list_volumes_with_limit(_conn, limit):
    generator = _conn.evs.volumes(paginated=False, limit=limit)
    for volumes_list in generator:
        for volume in volumes_list.volumes:
            print(volume['name'])

# update volume
def update_volume_data(_conn, volume_id, data=None):
    if data == None:
        data = {
            "name": "update_name_by_sdk",
            "description": "update_description_by_sdk",
        }
    cls = _conn.evs.update_volume(volume_id, **data)
    print(cls)

# versions
def versions(_conn):
    print(type(_conn.evs.get_version("v2")))
    for index in _conn.evs.get_version("v2"):
        print(index)


from sklearn.externals import joblib
import pandas as pd
from tqdm import tqdm
import numpy as np
from lightgbm import LGBMClassifier, plot_importance, plot_metric, plot_tree, LGBMRanker
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn.metrics import accuracy_score, confusion_matrix, roc_auc_score
import itertools
import gc
import collections
import time
from sklearn.model_selection import train_test_split, KFold, StratifiedKFold
import category_encoders as ce
from sklearn.ensemble import RandomForestClassifier
import json
import warnings
warnings.filterwarnings('ignore')
from sklearn.preprocessing import StandardScaler
from utils import *

credit_card_balance_df = pd.read_csv('../data/credit_card_balance.csv')
all_application_df = pd.read_csv('application.csv')
aggrated_df = all_application_df[['SK_ID_CURR']]

# Engineering
credit_card_balance_df['FE_TOTAL_DPD'] = credit_card_balance_df['SK_DPD_DEF'] + credit_card_balance_df['SK_DPD']
credit_card_balance_df['[FE_CB]SAME_DPD'] = (credit_card_balance_df['SK_DPD_DEF'] == credit_card_balance_df['SK_DPD']).astype(int)

credit_card_balance_df['[FE_CB]LATE'] = (credit_card_balance_df['SK_DPD'] > 0).astype(int)
credit_card_balance_df['[FE_CB]LATE_T'] = (credit_card_balance_df['SK_DPD_DEF'] > 0).astype(int)

credit_card_balance_df['FE_TOTAL_AMT_DRAW'] = credit_card_balance_df[[
'AMT_DRAWINGS_ATM_CURRENT',
'AMT_DRAWINGS_CURRENT',
'AMT_DRAWINGS_OTHER_CURRENT',
'AMT_DRAWINGS_POS_CURRENT',
]].fillna(0).apply(sum, axis=1)

credit_card_balance_df['FE_TOTAL_CNT_DRAW'] = credit_card_balance_df[[
'CNT_DRAWINGS_ATM_CURRENT',
'CNT_DRAWINGS_CURRENT',
'CNT_DRAWINGS_OTHER_CURRENT',
'CNT_DRAWINGS_POS_CURRENT',
]].fillna(0).apply(sum, axis=1)

credit_card_balance_df['FE_USED_RATIO'] = credit_card_balance_df['FE_TOTAL_AMT_DRAW'] / credit_card_balance_df['AMT_CREDIT_LIMIT_ACTUAL']

credit_card_balance_df['FE_PAID_RATIO']= credit_card_balance_df['AMT_PAYMENT_TOTAL_CURRENT']\
    / (np.maximum(credit_card_balance_df['AMT_TOTAL_RECEIVABLE'], credit_card_balance_df['AMT_INST_MIN_REGULARITY']) - 0)
credit_card_balance_df['FE_FLAG_FULL_PAY'] = credit_card_balance_df['FE_PAID_RATIO'].apply(lambda x: 1 if x >= 0.95 else 0)

k_configs = [0.1, 0.2, 0.3, 0.4]

# k = 0.4
# gy_cols = ['SK_ID_CURR']
# print(k)
# groupby = credit_card_balance_df.groupby(gy_cols)
# cuted_first_k_df = groupby.apply(get_first_k(k, gy_cols)).reset_index()
# cuted_last_k_df = groupby.apply(get_last_k(k, gy_cols)).reset_index()
# print(cuted_first_k_df.head(1))
# cuted_first_k_df.to_csv('credit_bal_first_'+str(k)+'.csv', index=False)
# cuted_last_k_df.to_csv('credit_bal_last_'+str(k)+'.csv', index=False)
# exit()

numerical_features_config = [
    ('FE_TOTAL_AMT_DRAW', ['mean', 'max' ]),
    ('FE_TOTAL_CNT_DRAW', ['mean', 'max']),
    ('FE_USED_RATIO', ['trend', 'mean', 'max', ]),
    ('FE_TOTAL_DPD', ['mean', 'max', ]),
    ('[FE_CB]SAME_DPD', ['mean', 'max', ]),
    ('[FE_CB]LATE', ['mean', 'max', ]),
    ('[FE_CB]LATE_T', ['mean', 'max', ]),
    ('FE_PAID_RATIO', ['mean', 'max', ]),
    ('FE_FLAG_FULL_PAY', ['mean', 'max', 'sum']),
    ('SK_DPD', ['trend', 'mean', 'max', ]),
    ('SK_DPD_DEF', ['trend','mean', 'max', ]),
    ('AMT_BALANCE', ['mean', 'max']),
    ('AMT_CREDIT_LIMIT_ACTUAL', ['mean', 'max', ]),
    ('AMT_DRAWINGS_ATM_CURRENT', ['trend', 'mean', 'max', ]),
    ('AMT_DRAWINGS_CURRENT', ['trend', 'mean', 'max', ]),
    ('AMT_DRAWINGS_OTHER_CURRENT', ['mean', 'max', ]),
    ('AMT_DRAWINGS_POS_CURRENT', ['mean', 'max', ]),
    ('AMT_INST_MIN_REGULARITY', ['mean', 'max', ]),
    ('AMT_PAYMENT_CURRENT', ['trend', 'mean', 'max',]),
    ('AMT_PAYMENT_TOTAL_CURRENT', ['mean', 'max', ]),
    ('AMT_RECEIVABLE_PRINCIPAL', ['mean', 'max', ]),
    ('AMT_RECIVABLE', ['mean', 'max', ]),
    ('AMT_TOTAL_RECEIVABLE', ['mean', 'max', ]),
    ('CNT_DRAWINGS_ATM_CURRENT', ['mean', 'max', ]),
    ('CNT_DRAWINGS_CURRENT', [ 'mean', 'max', ]),
    ('CNT_DRAWINGS_OTHER_CURRENT', ['mean', 'max', ]),
    ('CNT_DRAWINGS_POS_CURRENT', ['mean', 'max', ]),
    ('CNT_INSTALMENT_MATURE_CUM', ['mean', 'max', ]),
]

temp_list = []
for k in k_configs:
    print(k)
    cuted_first_k_df = (pd.read_csv('credit_bal_first_'+str(k)+'.csv'))
    cuted_last_k_df = (pd.read_csv('credit_bal_last_'+str(k)+'.csv'))
    for n_c in numerical_features_config:
        # print('', n_c[0])
        for agg in n_c[1]:
            # print('', '', agg)
            new_name = n_c[0]+'_'+agg+'_LAST_'+str(k)
            if agg == 'mean':
                t = cuted_first_k_df.groupby(['SK_ID_CURR'])[n_c[0]].mean()
            if agg == 'max':
                t = cuted_first_k_df.groupby(['SK_ID_CURR'])[n_c[0]].max()
            if agg == 'min':
                t = cuted_first_k_df.groupby(['SK_ID_CURR'])[n_c[0]].min()
            if agg == 'sum':
                t = cuted_first_k_df.groupby(['SK_ID_CURR'])[n_c[0]].sum()
            if agg == 'trend':
                continue
                # t = cuted_first_k_df.groupby(['SK_ID_CURR'])[n_c[0]].agg(get_trend)
            # print('', '', '', 'agg done')
            t = t.reset_index().rename(columns={n_c[0]:new_name})
            temp_list.append(t[['SK_ID_CURR', new_name]])

            new_name = n_c[0]+'_'+agg+'_FIRST_'+str(k)
            if agg == 'mean':
                t = cuted_last_k_df.groupby(['SK_ID_CURR'])[n_c[0]].mean()
            if agg == 'max':
                t = cuted_last_k_df.groupby(['SK_ID_CURR'])[n_c[0]].max()
            if agg == 'min':
                t = cuted_last_k_df.groupby(['SK_ID_CURR'])[n_c[0]].min()
            if agg == 'sum':
                t = cuted_last_k_df.groupby(['SK_ID_CURR'])[n_c[0]].sum()
            if agg == 'trend':
                t = cuted_last_k_df.groupby(['SK_ID_CURR'])[n_c[0]].agg(get_trend)
            # print('', '', '', 'agg done')
            t = t.reset_index().rename(columns={n_c[0]:new_name})
            temp_list.append(t[['SK_ID_CURR', new_name]])

for t in tqdm(temp_list):
    aggrated_df = pd.merge(aggrated_df, t, how='left', on=[ 'SK_ID_CURR'])

# Trending Features (group by previous application)
trends_df = credit_card_balance_df[['SK_ID_PREV', 'SK_ID_CURR']]
trends_df = trends_df.drop_duplicates()

credit_card_balance_df.sort_values('MONTHS_BALANCE', ascending=False, inplace=True)

# features = []
features = ['FE_USED_RATIO', 'SK_DPD', 'AMT_DRAWINGS_ATM_CURRENT', 'AMT_DRAWINGS_CURRENT', 'AMT_PAYMENT_CURRENT']

print('Getting Trends')
time_range = [-3, -6, -18]
ts = []
for time in time_range:
    df = credit_card_balance_df[credit_card_balance_df['MONTHS_BALANCE'] >= time]
    print(time, df.shape)
    groupby = df.groupby(['SK_ID_PREV', 'SK_ID_CURR'])
    for feature in tqdm(features):
        ts.append(groupby[feature].agg(get_trend).reset_index().rename(columns={feature:feature+'_TREND_'+str(time)}))
        ts.append(groupby[feature].agg('skew').reset_index().rename(columns={feature:feature+'_SKEW_'+str(time)}))

for t in tqdm(ts):
    trends_df = pd.merge(trends_df, t, how='left', on=['SK_ID_PREV', 'SK_ID_CURR'])

ts = []
for time in time_range:
    for feature in tqdm(features):
        ts += (agg_num(trends_df.groupby('SK_ID_CURR'), 'CB', feature+'_TREND_'+str(time), ['mean', 'max', 'min']))
        ts += (agg_num(trends_df.groupby('SK_ID_CURR'), 'CB', feature+'_SKEW_'+str(time), ['mean', 'max', 'min']))

for t in tqdm(ts):
    aggrated_df = pd.merge(aggrated_df, t, how='left', on='SK_ID_CURR')

print(aggrated_df.head(10))

print(aggrated_df.shape)
training_pdf = pd.merge(all_application_df, aggrated_df, how='left', on='SK_ID_CURR')
params = {
    'boosting_type':'gbdt',
    'objective':'binary',
    'n_estimators':20000,
    'learning_rate':0.05,
    'num_leaves': 123, #34,
    'feature_fraction': 1,
    'subsample':0.8715623,
    'max_depth': 3,
    'reg_alpha': 10,
    'reg_lambda':0.0735294,
    'min_child_weight': 20,
    'verbose': False,
}

categorical_features= ['EMERGENCYSTATE_MODE', 'ORGANIZATION_TYPE', 'CODE_GENDER', 'REGION_RATING_CLIENT', 'WALLSMATERIAL_MODE', 'NAME_CONTRACT_TYPE', 'NAME_FAMILY_STATUS', 'FONDKAPREMONT_MODE', 'FLAG_OWN_CAR', 'NAME_TYPE_SUITE', 'OCCUPATION_TYPE', 'NAME_EDUCATION_TYPE', 'NAME_INCOME_TYPE', 'FLAG_OWN_REALTY', 'REGION_RATING_CLIENT_W_CITY', 'NAME_HOUSING_TYPE', 'HOUSETYPE_MODE', 'WEEKDAY_APPR_PROCESS_START']

path = 'result/allF_'
a, b, c, feature_importances = validate_model_lgb(training_pdf, categorical_features, params, path, 5, 850, 10)
json.dump(sorted(feature_importances, key=lambda x: x[1], reverse=True), open('sorted_features.json', 'w'))

for k, v in sorted(feature_importances, key=lambda x: x[1], reverse=True):
    if v == 0 and k in aggrated_df.columns:
        aggrated_df.drop(columns=[k], inplace=True)

print(aggrated_df.shape)
aggrated_df.to_csv('credit_balance_a.csv', index=False)

# A only:
# Fold 0.8189543169343193 0.7730767322155367 0.04587758471878256 2086
# Fold 0.8236743619603927 0.7715934352074618 0.052080926752930856 2391
# Fold 0.813845406006931 0.7749161013380519 0.03892930466887912 1799
# Fold 0.8169069786922333 0.7765162654669496 0.04039071322528365 2008
# Fold 0.8087987937993646 0.775840861800686 0.03295793199867858 1518
#          AUC mean: 0.7743886792057373 std: 0.0018137378867200235 min: 0.7715934352074618 max: 0.7765162654669496
#          OOF AUC: 0.7743377142754992

# Fold 0.821517004317735 0.7740357321726814 0.0474812721450536 2110
# Fold 0.8158944777190782 0.7728806030490134 0.04301387467006479 1777
# Fold 0.8225976484494486 0.7755551110813529 0.047042537368095716 2174
# Fold 0.8166777905700106 0.776507174111096 0.04017061645891451 1857
# Fold 0.8191496875013875 0.7761128579256023 0.04303682957578525 1967
#          AUC mean: 0.7750182956679492 std: 0.0013592760645063345 min: 0.7728806030490134 max: 0.776507174111096
#          OOF AUC: 0.7749989789716756


import pandas as pd

# pd.set_option('display.max_columns', None)

abvenues = ['TRENDS ECOL EVOL',
            'NAT ECOL EVOL',
            'ANNU REV ECOL EVOL S',
            'FRONT ECOL ENVIRON',
            'GLOBAL CHANGE BIOL',
            'ECOL MONOGR',
            'ISME J'
            ]

i = 42

# 删除文件前几行
def delete_first_lines(filename, count):
    fin = open(filename, 'r')
    a = fin.readlines()
    fout = open(filename, 'w')
    b = ''.join(a[count:])
    fout.write(b)

# 删除文件后几行
def delete_last_lines(filename, count):
    file = open(filename)
    lines = file.readlines()
    lines = lines[:-count]
    file.close()
    w = open(filename, 'w')
    w.writelines(lines)

for ve in abvenues:
    delete_first_lines('D:/study/毕设/journal/test2/' + str(i) + '3.csv', 5)
    delete_last_lines('D:/study/毕设/journal/test2/' + str(i) + '3.csv', 4)
    delete_first_lines('D:/study/毕设/journal/test2/' + str(i) + '1.csv', 5)
    delete_first_lines('D:/study/毕设/journal/test2/' + str(i) + '2.csv', 5)
    delete_last_lines('D:/study/毕设/journal/test2/' + str(i) + '1.csv', 3)
    delete_last_lines('D:/study/毕设/journal/test2/' + str(i) + '2.csv', 3)
    i = i + 1

print(i)


#GUI which configures settings
import sys
import json
import PySimpleGUI as sg
import name_generator

layout = [
#Name entry

    [sg.Text("Welcome! There's just a few more details you have to enter before you can get some silly names\n")],
    [sg.Text("Enter your name. If you don't have an middle name, leave the field blank")],
    [
        sg.Text("First name: ", font = "Courier 12"),
        sg.In(size = (30, 1), enable_events = True, key = "first_name")
    ],
    [
        sg.Text("Middle name:", font = "Courier 12"),
        sg.In(size = (30, 1), enable_events = True, key = "middle_name")
    ],
    [
        sg.Text("Last name:  ", font = "Courier 12"),
        sg.In(size = (30, 1), enable_events = True, key = "last_name")
    ],
    [sg.HorizontalSeparator()],
#Gender choice
    [sg.Text("Choose what genders you want to include for your names.")],
    [sg.Text("Genderless titles like 'Alien' or 'Big Chungus' will be included regardless of your choice")],
    [
        sg.Checkbox("Male", key="m"),
        sg.Checkbox("Female", key="f")
    ],
#Audio choice

    [sg.HorizontalSeparator()],
    [
        sg.Checkbox(
            "Make sound when a name is copied from the notification area",
            key="sound",
            default=True
        )
    ],
#Save and quit
    [sg.Text("Once you save, a red circle will appear in the taskbar.")],
    [sg.Text("Click it for a name to be copied to your clipboard")],
    [sg.Text("Right click it for a menu to close the program or change settings")],
    [sg.Button("Save and close", key="save", button_color=("black", "red"))]
]

with open("config.json") as read_file:
    config = json.load(read_file)

if config["first_time"] == False:
    title = name_generator.name()
else:
    title = "Settings"

window = sg.Window(title, layout, finalize=True, icon="icon.ico")

#Load in all the values
window["first_name"].update(config["first_name"])
window["middle_name"].update(config["middle_name"])
window["last_name"].update(config["last_name"])

window["m"].update(config["m"])
window["f"].update(config["f"])
window["sound"].update(config["sound"])

while True:
    event, values = window.read()
    if event == "Exit":
        break
    if event == sg.WIN_CLOSED:
        sys.exit()

    if event == "save":

        config["first_name"] = values["first_name"]
        config["middle_name"] = values["middle_name"]
        config["last_name"] = values["last_name"]

        config["m"] = values["m"]
        config["f"] = values["f"]
        config["sound"] = values["sound"]

        config["first_time"] = False

        with open("config.json", "w") as write_file:
            json.dump(config, write_file)

        break

window.close()


#!/usr/bin/python2.7
#DIGITAL TWIN MOBILE ROBOT
#Modelo processamento de imagens para OpenCV - Canny Edge Detection
import rospy

from sensor_msgs.msg import Image

import cv2
import numpy as np #biblioteca para computacao cientifica
import time

#ROS image message -> OpenCV2 image converter
from cv_bridge import CvBridge, CvBridgeError #pacote do ROS com diferentes tipos de formatacao de imagens

#for delay measure
delay=0

delta=0

bridge = CvBridge()

import sys
import signal
def signal_term_handler(signal, frame):
	rospy.logger('User KeyboardInterrupt')
	sys.exit(0)
signal.signal(signal.SIGINT, signal_term_handler)

#callback function funcao principal
def img_callback(ros_data):
	global delta
	print(int(round(time.time()*1000)) - delta)
	delta = int(round(time.time()*1000))

	try:
		image_np = bridge.imgmsg_to_cv2(ros_data, "mono8") #tipagem do open cv rgb
		#image_np = bridge.imgmsg_to_cv2(ros_data, "bgr8") #tipagem do open cv em escala de cinza
		flipVertical_image_np = cv2.flip(image_np, 0)
	except CvBridgeError as e:
		print(e)

	edges = cv2.Canny(image_np, 50, 150, apertureSize=3) #basicamente um novo frame. opera em escala de cinza
	flipVertical_edges = cv2.flip(edges, 0)

	#cv2.imshow("output", image_np)
	#cv2.imshow("output", np.hstack([image_np, edges])) #concatenacao de matrizes
	cv2.imshow("output", np.hstack([flipVertical_image_np, flipVertical_edges]))
	cv2.waitKey(1) #sleep de 1ms

def main():
	rospy.init_node('csf_robotCS_cannyKinect')

	#rospy.Subscriber("camera/rgb/image_raw", Image, img_callback, queue_size=1)
	rospy.Subscriber("/kinect/rgb_image", Image, img_callback, queue_size=1)

	try:
		rospy.spin()
	except KeyboardInterrupt:
		print('Shutting down ...')
	cv2.destroyAllWindows()

if __name__ == '__main__':
	main()


import unittest
from request_handler import DataHandler, TrainingRequest
from services import TaskService

from task_management import TaskManager

class task_management_test(unittest.TestCase):

    def test_get_uuid(self):
        task_manager: TaskManager = TaskManager()
        uuid: str = task_manager.get_uuid()
        self.assertTrue(len(uuid) == 36)
        self.assertTrue(isinstance(uuid, str))

    def test_get_status(self):
        task_manager: TaskManager = TaskManager()
        training_request: TrainingRequest = TrainingRequest("1234", "test_model", {
                                                            "binaryName": "testBin", "functionsMap": {"functions": [{"tokenList": ["testToken"]}]}})
        TaskService().service_queue.put(training_request)
        status: str = task_manager.get_status("1234")
        self.assertEqual("starting", status)

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


"""
Generator for carbon nano ribbons with zig-zag edges (ZGNRs).
"""

import textwrap
import argparse
import numpy as np
import lattice

def _define_parser():
    "Define command line argument parser for AGNRgen."

    parser = argparse.ArgumentParser(prog="latgraph --generate agnr",
                                     formatter_class=argparse.RawDescriptionHelpFormatter,
                                     description=textwrap.dedent("""\
                Generate a zig-zag carbon nanoribbon of given number of zig-zag lines (width)
                and number of unit cells along the longitudinal direction (length).

                Currently only periodic boundary conditions are supported (in the longitudinal direction).
                                     """))
    parser.add_argument("n_zig", type=int, help="Number of dimer lines that go in the longitudinal direction. Determines the width of the ribbon.")
    parser.add_argument("n_uc", type=int, help="Number of hexagons along the longitudinal direction. Determines the length of the ribbon.")
    parser.add_argument("--spacing", type=float, default="1", help="Lattice spacing")
    parser.add_argument("--name", help="Name for the lattice. Default is N_ZIGzgnrN_UC")
    parser.add_argument("--comment", default="", help="Comment on the lattice")
    return parser

def run(in_args):
    "Run ZGNRgen from command line arguments."
    parser = _define_parser()
    args = parser.parse_args(in_args)

    gen = ZGNRgen(args.n_zig, args.n_uc, args.spacing)

    lat = gen.make_lattice()

    if args.name:
        lat.name = args.name
    else:
        lat.name = "{}zgnr{}".format(args.n_zig, args.n_uc)
    if args.comment:
        lat.comment = args.comment

    return lat

class ZGNRgen:
    """
    Class to make zig-zag graphene nanoribbons (ZGNRs), with variable width and length.

    The width and length are determined via the following input arguments:
    n_zig: number of zig-zag lines (determines width)
    n_uc: number of unit cells in the longitudinal direction (determines length)
    spacing: lattice spacing

    Currently only periodic boundary conditions (along the longitudinal direction) are implemented.
    Boundary conditions along the transverse direction are open, otherwise we would call it a tube.
    """
    def __init__(self, n_zig, n_uc, spacing):
        self.n_zig = n_zig #Number of zigzag lines
        self.n_uc = n_uc #Number of hexagons in longitudinal direction
        self.spacing = spacing

    def get_coordinates(self, idx):
        """Returns:
            - zig: The index of the zig-zag line, counted from the bottom up)
            - l: The index in the zig-zag line counted as
            zig even:
                1       3
            0       2       4
            zig odd:
            1       3
                0       2
            """
        zig=idx//(2*self.n_uc)
        l=idx%(self.n_uc*2)
        return zig, l

    def get_neighbours(self, idx):
        """"Return the indices of all neighbours in an array, applying periodic
        boundary conditions"""

        zig, l = self.get_coordinates(idx)
        neigh=[]

        lmax=self.n_uc*2-1

        if idx%2==0: #Site has two upper neighbors
            #lower neighbour:
            if zig>0:               #Site isn't on the bottom boundary
                neigh.append(idx-lmax)

            #upper left:
            if zig%2==0 and l==0:   #Periodic boundary
                neigh.append(idx+lmax)
            else:
                neigh.append(idx+(-1+2*(zig%2)))

            #upper right:
            if zig%2==1 and (l==lmax-1):   #Periodic boundary
                neigh.append(idx-lmax+2)
            else:
                neigh.append(idx+1+(zig%2)*2)

        else:   #Site has two lower neighbours
             #lower left:
            if zig%2==1 and l==1: #Periodic boundary
                neigh.append(idx+lmax-2)
            else:
                neigh.append(idx-1-(zig%2)*2)

            #lower right:
            if zig%2==0 and l==lmax:
                neigh.append(idx-lmax)
            else:
                neigh.append(idx+1-2*(zig%2))

            #upper neighbor:
            if zig<(self.n_zig-1):       #Site isn't on the top boundary
                neigh.append(idx+lmax)
        return neigh

    def make_lattice(self):
        #Generate sites (with positions, but no neighbours)
        lat=lattice.Lattice()

        for zig in range(0, self.n_zig):
            if zig%2==0:
                for l in range(self.n_uc*2):
                    xpos=l//2*np.sqrt(3) + np.sqrt(3)/2*(l%2)
                    ypos=zig*3/2+1/2*(l%2)
                    idx1 = zig*self.n_uc*2+l
                    lat.sites.append(lattice.Site(idx1, np.array([xpos*self.spacing, ypos*self.spacing])))
            else:
                for l in range(self.n_uc*2):
                    xpos=l//2*np.sqrt(3) + np.sqrt(3)/2*((l+1)%2)
                    ypos=zig*3/2+1/2*(l%2)
                    idx1 = zig*self.n_uc*2+l
                    lat.sites.append(lattice.Site(idx1, np.array([xpos*self.spacing, ypos*self.spacing])))

        #Fill in neighbours for each site with periodic boundary conditions and set hopping to 1
        for site in lat.sites:
            neigh=self.get_neighbours(site.idx)
            site.neighbours=neigh
            site.hopping=[1]*len(neigh)
        return lat


# Import all the necessary packages
import numpy as np
import os
import cv2
import pandas as pd
import PySimpleGUI as sg

# Directory and Filename
MAIN_DIR = r"C:\Users\Argarot\images"
DANIIL_DIR = MAIN_DIR + "\calibration\\"
filename = 'IMG_20210727_181600.jpg'

# PySimpleGUI
sg.theme("LightGreen")

layout = [
    # 1. Threshold for Segmentation
    [
        sg.Text("Threshold for Segmentation:", size=(25, 1), justification="left"),
        sg.Slider(
            (0, 100),
            0,
            1,
            orientation="h",
            size=(40, 15),
            key="-SEGMENT THRESH SLIDER-",
        ),
    ],

    # 2. Morphological Transformations - Noise Reduction
    [
        sg.Text("Iterations for Opening:", size=(25, 1), justification="left"),
        sg.Slider(
            (0, 5),
            2,
            1,
            orientation="h",
            size=(40, 15),
            key="-OPEN ITER SLIDER-",
        ),
    ],
    [
        sg.Text("Iterations for Closing:", size=(25, 1), justification="left"),
        sg.Slider(
            (0, 5),
            2,
            1,
            orientation="h",
            size=(40, 15),
            key="-CLOSE ITER SLIDER-",
        ),
    ],

    # 3. Morphological Transformations - Dilation
    [
        sg.Text("Iterations for Dilation:", size=(25, 1), justification="left"),
        sg.Slider(
            (0, 5),
            2,
            1,
            orientation="h",
            size=(40, 15),
            key="-DILATE ITER SLIDER-",
        ),
    ],

    # 4. Options: DistanceTransform Mask Size
    [
        sg.Text("MaskSize for Distance Transform:", size=(25, 1), justification="left"),
        sg.Radio('3x3 MaskSize', 'Radio1', size=(10, 1), key="-3x3 MASK SIZE-", default=True),
        sg.Radio('5x5 MaskSize', 'Radio1', size=(10, 1), key="-5x5 MASK SIZE-"),
        sg.Radio('0x0 MaskSize', 'Radio1', size=(10, 1), key="-0x0 MASK SIZE-"),
    ],

    [sg.Button("OK", size=(10, 1))],
]

window = sg.Window("OpenCV Watershed Algorithm GUI", layout, location=(600, 400))

# __main__()
while (1):
    event, values = window.read(timeout=20)
    if event == "OK" or event == sg.WIN_CLOSED or event is None:
        break

    # Load the image
    img = cv2.imread(DANIIL_DIR + filename)
    img = cv2.resize(img, (600, 768))
    img_copy = img.copy()
    img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    # values from PysimpleGUI
    threshold = int(values["-SEGMENT THRESH SLIDER-"])

    opening_iter = int(values["-OPEN ITER SLIDER-"])

    closing_iter = int(values["-CLOSE ITER SLIDER-"])

    dilate_iter = int(values["-DILATE ITER SLIDER-"])

    if values["-3x3 MASK SIZE-"]:
        maskSize = 3
    elif values["-5x5 MASK SIZE-"]:
        maskSize = 5
    elif values["-0x0 MASK SIZE-"]:
        maskSize = 0

    # Image Thresholding (Inverse Binary + Otsu Method)
    ret, thresh = cv2.threshold(src=img_gray, thresh=127, maxval=255, type=cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

    # Morphological Transformations / Noise Reduction
    kernel = np.ones((3,3),np.uint8)
    thresh = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel,iterations=opening_iter)     # Opening to reduce background noise
    thresh = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel,iterations=closing_iter)    # Closing to reduce foreground noise

    # Dilation: to find sure background area
    kernel = np.ones((3,3),np.uint8)
    sure_bg = cv2.dilate(thresh,kernel,iterations=dilate_iter) # Adjust until desired result is achieved

    # Calculate distance transformation
    dist_transform = cv2.distanceTransform(src=sure_bg,distanceType=cv2.DIST_L2,maskSize=maskSize)                                    # Adjust until desired result is achieved
    ret, sure_fg = cv2.threshold(src=dist_transform,thresh=(threshold/100 * dist_transform.max()),maxval=255,type=cv2.THRESH_BINARY)  # Adjust to get desired separation

    # Find unknown regions
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg,sure_fg)

    # Markers Labelling
    ret, markers = cv2.connectedComponents(sure_fg)
    markers = markers+1             # Add one to all labels so that sure background is not 0, but 1
    markers[unknown==255] = 0       # Mark the region of unknown with zero

    # Watershed Algorithm to find markers
    markers = cv2.watershed(img,markers)
    markers_copy = markers.copy()
    markers_black = markers.astype(np.uint8)

    # Finding Contours on Markers
    contours, hierarchy = cv2.findContours(markers_copy, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

    # Find coordinates of the centroids, area of the droplets
    cx = []
    cy = []
    area_cv = []

    for i,c in enumerate(contours):
        M = cv2.moments(c)
        if M["m00"] == 0:
            continue
        else:
            cx.append(int(M["m10"] / M["m00"])) # x-coordinates of centroids
            cy.append(int(M["m01"] / M["m00"])) # y-coordinates of centroids
            area_cv.append(cv2.contourArea(c))

    data = pd.DataFrame({'x': cx, 'y': cy, 'area': area_cv})
    data_contours = pd.DataFrame(list(contours))
    data_contours.rename(columns={0: 'contours'},inplace=True)
    data = data.merge(data_contours, left_index=True, right_index=True)
    # the following are the columns in 'data' df: x, y, area, contours

    data = data[(data['area'] < 15000.0) & (data['area'] > 100.0)]     # Filter contours with 100.0 < area < 1500.0  (adjustable)
    data.drop_duplicates(subset=['x','y'],inplace=True)               # Remove duplicate centroids

    # Sorting left-to-right, top-to-bottom
    data.sort_values(by=['y'], inplace = True)
    data.reset_index(inplace = True, drop = True)

        # Initialize the column 'row'
    data['row'] = np.ones(len(data), dtype='int64')

        # Assign row number
    row_num = 1
    for i in np.arange(1,len(data)):
        if (abs(data.loc[i,'y'] - data.loc[i-1,'y']) > 30):             # If diff in y-coordinates > 30: next row (adjustable)
            row_num += 1
            data.loc[i,'row'] = row_num
        else:
            data.loc[i,'row'] = row_num

        # Sort by row first, then sort by x-axis
    data.sort_values(by=['row','x'], ascending=[True,True], inplace=True)
    data.reset_index(inplace = True, drop = True)

    for i in np.arange(len(data)):
        if i == 0:
            continue
        elif (abs(data.loc[i,'x'] - data.loc[i-1,'x']) < 5):           # If diff in x-coordinates < 5 (overlap of circles): remove (adjustable)
            data.loc[i,'row'] = 999

    data = data[data['row'] != 999]
    data.reset_index(inplace = True, drop = True)

    # Sorted contours
    new_contours = list(data['contours'])

    # Draw contours and annotate on the image
    for i in list(data.index.values):
        cx = data['x'][i].astype('int64')
        cy = data['y'][i].astype('int64')
        cv2.drawContours(img, new_contours, -1, (0,0,255), 1) # red contours
        cv2.circle(img, (cx,cy), 2, (255,0,0), 1)             # blue circle of centroid
        cv2.putText(img, "{}".format(i),                      # black text to annotate
                (cx - 20, cy - 20),
                cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0,0,0), 1)

    cv2.imshow("Contours",img)
    cv2.imshow("Watershed Markers",markers_black)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:         # wait for ESC key to exit
        break
cv2.destroyAllWindows()

cv2.imshow("Final Result",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite(os.path.join(DANIIL_DIR,"{}_contours.jpg".format(filename[:-4])),img)

print('\nNumber of contours: {}'.format(len(data)))

data.to_csv(os.path.join(DANIIL_DIR,"{}_data.csv".format(filename[:-4])))
print("{}_data.csv created.\n".format(filename[:-4]))


# encoding=utf-8
import time
import xmlrpc.client
import logging,sys

import common
from common import *
import FBLandmarkHelper
from utility import OurFBAccount
# from utility import FBHelper,FBBaseUser
from loghelper.loghelper import logHelper

#远程服务代理
serverconfig = common.getDispatchServerConfig()  # 3
serProxy = xmlrpc.client.ServerProxy('http://{0}:{1}'.format(serverconfig[0], serverconfig[1]))

def getFBLandmarkTask(tasktype):
   return serProxy.getATaskLandmark(tasktype)
def reportFBTaskLandmarkComplete(taskid,runningState,completeDescription):
   return serProxy.reportFBTaskLandmarkComplete(taskid,runningState,completeDescription)

def main():
    fblandmarkhelper = FBLandmarkHelper.FBLandmarkHelper()
    # # 登录
    # # fbAccount = ourFBAccount('trumpJACK2018@outlook.com', '1qaz@WSX3edc', None)
    # fbAccount = OurFBAccount.getAccount('ourFBAccount.txt')
    # logName = time.strftime('%Y-%m-%d', time.localtime(time.time())) + ".log"
    # myargs = {'fName': logName, 'fLevel': logging.DEBUG}
    # logger = logHelper.getLogger('myLog', logging.INFO, **myargs)
    #
    # # 初始化
    # modify!
    # logger.info("============================================================")
    # # browser, isLogin = FBUserCrawler.initCrawler("https://www.facebook.com/", fbAccount)
    # # if not isLogin:
    # #     logHelper.getLogger().info("login error.please check the log file!")
    # #     time.sleep(10)
    # #     exit(1)

    while True:
        try:
            logHelper.getLogger().info('connecting dispatch server {0}:{1}'.format(serverconfig[0], serverconfig[1]))
            if common.testDispatchServer() == 'Disconnected!':  #1.测试调度服务器连接
                logHelper.getLogger().info('Dispatch server is disconnected!')
                time.sleep(5)
                continue
            logHelper.getLogger().info('Dispatch server is connected!')

            logHelper.getLogger().debug('connecting database server ...')
            if common.testDatabaseServer() == 'Disconnected!':  #2.测试服务器连接
                logHelper.getLogger().info('Database server is disconnected!,trying again later.')
                time.sleep(5)
                continue
            print('Database server is connected!')

            spiderType = 'Facebook.landmarkInfo'    #对应任务类型
            task = getFBLandmarkTask(spiderType)

            taskid = task['id']
            if int(taskid) == -1:
                time.sleep(5)
                continue

            fbid = task['fbid']
            originalfbid = task['originalfbid']
            deep = int(task['deep'])     #当前任务深度，在保存tb_user_friends时+1保存入库
            name = task['name']
            priority = task['priority']

            # fbUser = FBBaseUser(fbid,name,deep,priority)

            # logHelper.getLogger().debug(task)
            logHelper.getLogger().info('Spider [{0}] have got a task:{1}/{2}/{3}. spider is working...'.format(spiderType,taskid,fbid,originalfbid))
            time.sleep(5)
            #开始单线程爬取工作#####################################################################################################
            try:
                # 填写分配任务时间和承担的spider=''
                fblandmarkhelper.UpdateTaskDispatch(int(taskid))

                # modify!
                # if FBLandmarkCrawler.crawleInfo(browser, fbUser, fbAccount) == 1:
                if True:
                    #完成工作,入库
                    print('Spider have done the job. Saving the results...')

                    #告知调度端完成情况
                    print('Spider have saved the results,and reporting the job to dispatch server...')
                    k = reportFBTaskLandmarkComplete(taskid, 2, 'completed normally.')
                    print('task id:{0},fbid:{1} has reported the job status.'.format(taskid,fbid))

            except Exception as e1:
                logHelper.getLogger().error(e1)
                reportFBTaskLandmarkComplete(taskid, 3, 'completed abnormally.Error:{0}'.format(e1))
                #raise  e1

            time.sleep(15)
            # 结束单线程爬取工作#####################################################################################################
        except Exception as e:
            logHelper.getLogger().error(e)
            logHelper.getLogger().error('the main loop error,restart it!')
            time.sleep(50)

if __name__ == '__main__':
    main()


import scrapy
import json
from pbl.items import ShopCard

class SpidereuroSpider(scrapy.Spider):
    name = 'spiderEuro'
    allowed_domains = ['www.trobos.lt']
    start_urls = []
    for page in range(1,82):
        url=f'https://trobos.lt/prekes?vendor=eurovaistine&page={page}'
        start_urls.append(url)
    item = []
    list = [{
        'sid': 15,
        'name': 'Samsung',
        'domain': 'https://www.eurovaistine.lt/',
        'imageurl': 'https://www.eurovaistine.lt/build/themes/ev/eurovaistine-theme/images/app/logo.png',
        'product': item
        }]

    def __init__(self):
        self.declare_xpath()

    def declare_xpath(self):
        self.getAllItemsXpath =  '//*[@id="category"]/div/div[1]/div/div[3]/div[4]/div/div/div/div/div/a/@href'
        self.TitleXpath  = '//*[@id="product"]/section[1]/div[3]/section/div[2]/h1/text()'
        self.PriceXpath = '//*[@id="product"]/section[1]/div[3]/section/div[2]/div[1]/div/div[1]/div/div[1]/span/text()'

    def parse(self, response):
        for href in response.xpath(self.getAllItemsXpath):
            url = response.urljoin(href.extract())
            yield scrapy.Request(url=url,callback=self.parse_main_item, dont_filter=True)

    def parse_main_item(self,response):
        shop = ShopCard()
        Title = response.xpath(self.TitleXpath).extract_first()
        Link = response.url
        Image = 'https://cdns.iconmonstr.com/wp-content/releases/preview/2019/240/iconmonstr-product-3.png'
        Price = response.xpath(self.PriceXpath).extract_first()
        Price = Price.replace(',', '.')
        Price = float(Price.split(' ')[0])

        shop['item'] = {
                'title': Title,
                'link': Link,
                'image': Image,
                'price': Price
            }

        self.item.append(shop['item'])

    def closed(self, reason):
        with open("spiderEuro.json", "w") as final:
            json.dump(self.list, final, indent=2, ensure_ascii=False)


import json
import uuid
from django.core.serializers import serialize

from django.db import IntegrityError
from django.test import TestCase
import pytest
from django_uuid_pk.fields import StringUUID

from django_uuid_pk.tests.models import (AutoUUIDFieldModel, ManualUUIDFieldModel, NamespaceUUIDFieldModel,
                                         BrokenNamespaceUUIDFieldModel, PrimaryKeyUUIDFieldModel,
                                         BrokenPrimaryKeyUUIDFieldModel, ModelUUIDField)

def assertJSON(data):
    try:
        json.loads(data)
    except ValueError:
        raise

@pytest.mark.django_db
class UUIDFieldTestCase(TestCase):
    def test_protocols(self):
        obj = ModelUUIDField.objects.create()
        self.assertTrue(isinstance(obj.uuid1, uuid.UUID))
        self.assertTrue(isinstance(obj.uuid3, uuid.UUID))
        self.assertTrue(isinstance(obj.uuid4, uuid.UUID))
        self.assertTrue(isinstance(obj.uuid5, uuid.UUID))

    def test_auto_uuid4(self):
        obj = AutoUUIDFieldModel.objects.create()
        self.assertTrue(obj.uuid)
        self.assertEquals(len(obj.uuid), 32)
        #self.assertTrue(isinstance(obj.uuid, uuid.UUID))
        self.assertEquals(obj.uuid.version, 4)

    def test_raises_exception(self):
        self.assertRaises(IntegrityError, ManualUUIDFieldModel.objects.create)

    def test_manual(self):
        obj = ManualUUIDFieldModel.objects.create(uuid=uuid.uuid4())
        self.assertTrue(obj)
        self.assertEquals(len(obj.uuid), 32)
        #self.assertTrue(isinstance(obj.uuid, uuid.UUID))
        self.assertEquals(obj.uuid.version, 4)

    def test_namespace(self):
        obj = NamespaceUUIDFieldModel.objects.create()
        self.assertTrue(obj)
        self.assertEquals(len(obj.uuid), 32)
        #self.assertTrue(isinstance(obj.uuid, uuid.UUID))
        self.assertEquals(obj.uuid.version, 5)

    def test_broken_namespace(self):
        self.assertRaises(ValueError, BrokenNamespaceUUIDFieldModel.objects.create)

    def test_wrongvalue(self):
        obj = PrimaryKeyUUIDFieldModel.objects.create()
        with pytest.raises(ValueError):
            obj.uuid = 1

    def test_assign1(self):
        obj = PrimaryKeyUUIDFieldModel.objects.create()
        obj.uuid = uuid.UUID('5b27d1bd-e7c3-46f3-aaf2-11e4d32f60d4')
        obj.save()
        assert str(obj.uuid) == '5b27d1bde7c346f3aaf211e4d32f60d4'
        #assert obj.uuid == '5b27d1bd-e7c3-46f3-aaf2-11e4d32f60d4'
        assert obj.uuid == uuid.UUID('5b27d1bd-e7c3-46f3-aaf2-11e4d32f60d4')

    def test_assign2(self):
        obj = PrimaryKeyUUIDFieldModel.objects.create()
        obj.uuid = '5b27d1bd-e7c3-46f3-aaf2-11e4d32f60d4'
        obj.save()
        assert str(obj.uuid) == '5b27d1bde7c346f3aaf211e4d32f60d4'

    def test_primary_key(self):
        obj = PrimaryKeyUUIDFieldModel.objects.create()
        assert obj.pk

        obj = PrimaryKeyUUIDFieldModel()
        assert not obj.pk

        # reset primary key if save() fails
        BrokenPrimaryKeyUUIDFieldModel.objects.create(unique=1)
        obj = BrokenPrimaryKeyUUIDFieldModel(unique=1)
        with pytest.raises(IntegrityError):
            obj.save()
        assert not obj.pk

    def test_serialize(self):
        obj = PrimaryKeyUUIDFieldModel.objects.create()
        obj.uuid = uuid.UUID("2e9280cfdc8e42bdbf0afa3043acaa7e")
        obj.save()
        serialized = serialize('json', PrimaryKeyUUIDFieldModel.objects.all())
        assertJSON(serialized)

    #def test_json(self):
    #    obj = PrimaryKeyUUIDFieldModel.objects.create()
    #    obj.save()
    #    serialized = json.dumps(obj)
    #    assertJSON(serialized)

        #deserialized = json.loads(serialized, object_hook=registry.object_hook)
        #
        #print 111, deserialized
        #
        #assert PrimaryKeyUUIDField(**deserialized).uuid == obj.uuid


import os

from os import listdir

from os.path import isfile, join

import numpy as np

import pandas as pd

from plotly.subplots import make_subplots

import plotly.graph_objects as go

DATA_PATH = '/kaggle/input/efficientbx-melanoma-classification-with-tf'

# Importing Data

history_files = [f for f in listdir(DATA_PATH) if isfile(join(DATA_PATH, f)) and f.split('_')[0] == 'history']

submit_files = [f for f in listdir(DATA_PATH) if isfile(join(DATA_PATH, f)) and f.split('_')[0] == 'submit']
# Preprocessing Data for visualization (Performance x Model)

list_results = []

for file_name_i in history_files:

    model_name_i = file_name_i[8:22]

    fold_name_i = file_name_i[23:29]

    df_i = pd.read_csv(os.path.join(DATA_PATH, file_name_i), index_col=0)

    auc_i = df_i[df_i.val_loss == df_i.val_loss.min()]['val_auc'].iloc[0]

    loss_i = df_i.val_loss.min()

    list_results.append([model_name_i, fold_name_i, auc_i, loss_i])

df_results = pd.DataFrame(list_results, columns = ['Model', 'Fold', 'AUC', 'Loss'])

for model_name_i in df_results.Model.unique():

    mean_auc_i = df_results.AUC[df_results.Model == model_name_i].mean()

    mean_loss_i = df_results.Loss[df_results.Model == model_name_i].mean()

    df_i = pd.DataFrame([[model_name_i, 'mean_fold', mean_auc_i, mean_loss_i]], columns = ['Model', 'Fold', 'AUC', 'Loss'])

    df_results = pd.concat([df_results, df_i])

df_results = df_results.sort_values(by = ['Model', 'Fold'], ascending = True).reset_index().drop(columns = ['index'])
# Preprocessing Data for visualization (Training History)

model_names = ['EfficientNetB' + str(i) for i in range(8)]

fold_names = ['fold_' + str(i) for i in range(4)]

dict_df_history = {}

for model_i in model_names:

    dict_df_history[model_i] = {}

    for fold_i in fold_names:

        file_name_i = 'history_' + model_i + '_' + fold_i + '.csv'

        df_history_i = pd.read_csv(os.path.join(DATA_PATH, file_name_i))

        df_history_i=df_history_i.rename(columns = {

            'Unnamed: 0': 'Epoch',

            'loss': 'Train Loss',

            'auc': 'Train AUC',

            'val_loss': 'Valid Loss',

            'val_auc': 'Valid AUC',

            'lr': 'Learning Rate'

        }

                           )

        dict_df_history[model_i][fold_i] = df_history_i
from plotly.subplots import make_subplots

import plotly.graph_objects as go

df_results_mean_fold = df_results[df_results.Fold=='mean_fold']

fig = make_subplots(rows=2, cols=1)

fig.append_trace(

    go.Scatter(x=df_results_mean_fold.Model, y=df_results_mean_fold.AUC, name='AUC'),

    row=1, col=1

)

fig.append_trace(

    go.Scatter(x=df_results_mean_fold.Model, y=df_results_mean_fold.Loss, name='Loss'),

    row=2, col=1

)

fig.update_layout(height=800, width=600, title_text="Performance x Model")

fig.update_yaxes(title_text="AUC", row=1, col=1)

fig.update_yaxes(title_text="Loss", row=2, col=1)

for trace in fig['data']:

    trace['showlegend'] = False

fig.show()
fig = make_subplots(rows=2, cols=2, subplot_titles=('Fold 0', 'Fold 1', 'Fold 2', 'Fold 3'))

# Add Traces

for model_name_i in model_names:

    for i, fold_i in enumerate(fold_names):

        for set_i in ['Train', 'Valid']:

            for metric_i in ['AUC', 'Loss']:

                index_dict = {

                    'fold_0': [1,1],

                    'fold_1': [1,2],

                    'fold_2': [2,1],

                    'fold_3': [2,2]

                }

                color_dict = {

                    'Train AUC': {'color': '#dba053'},

                    'Valid AUC': {'color': '#6e53db'},

                    'Train Loss': {'color': '#4cd489'},

                    'Valid Loss': {'color': '#bf2e2e'}

                }

                fig.add_trace(

                    go.Scatter(

                        x=dict_df_history[model_name_i][fold_i]['Epoch'],

                        y=dict_df_history[model_name_i][fold_i][set_i + ' ' + metric_i],

                        name=set_i + ' ' + metric_i,

                        #name=model_name_i + ' ' + fold_i + ' ' + set_i + ' ' + metric_i,

                        #name=model_name_i[-2:] + fold_i[-1] + set_i + metric_i,

                        line=color_dict[set_i + ' ' + metric_i],

                        legendgroup=model_name_i[-2:] + ' ' + metric_i,

                        showlegend=(i==0)

                    ),

                    row=index_dict[fold_i][0], col=index_dict[fold_i][1]

                )

list_buttons = []

for i, model_name_i in enumerate(model_names):

    visible_list_aux_i = [False]*128

    visible_list_aux_i[i*16:i*16+16] = [True]*16

    button_i = dict(

        label=model_name_i[-2:],

        method="update",

        args=[{"visible": visible_list_aux_i}])

    list_buttons.append(button_i)

fig.update_layout(

    updatemenus=[

        dict(

            active=0,

            buttons=list_buttons,

            direction="right",

            pad={"r": 10, "t": 10},

            showactive=True,

            x=0.00,

            xanchor="left",

            y=1.25,

            yanchor="top",

            type = 'buttons'

        )

    ])

fig.show()
sample_submit = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv')

for model_name_i in model_names:

    target_list = []

    for fold_i in fold_names:

        target_i = pd.read_csv(os.path.join(DATA_PATH, 'submit_' + model_name_i + '_' + fold_i + '.csv'))['target'].values

        target_list.append(target_i)

    df_submit_i = sample_submit.copy()

    df_submit_i['target'] = np.mean(target_list, axis=0)

    df_submit_i.to_csv('submit_' + model_name_i + '_mean_fold.csv', index=False)

    print(model_name_i + ' - Mean Fold submit file saved')


"""add column confirmed to users

Revision ID: 92b13a79f1f1
Revises: 7031cf389ed4
Create Date: 2023-04-26 17:41:44.423409

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '92b13a79f1f1'
down_revision = '7031cf389ed4'
branch_labels = None
depends_on = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('users', sa.Column('confirmed', sa.Boolean(), nullable=True))
    # ### end Alembic commands ###

def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('users', 'confirmed')
    # ### end Alembic commands ###


import calendar

class YearlyCalendar:
    '''
    Get calendar of a given year
    '''

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

    def get_calendar(self):
        '''
        Getting calendar
        '''

        print('\n')
        print(calendar.calendar(self.year))

if __name__ == '__main__':
    year_calendar = YearlyCalendar(2020)
    year_calendar.get_calendar()