import multiprocessing

import pickle
import logging

logger = logging.getLogger(__name__)

from gensim.models import Word2Vec, KeyedVectors
from gensim.models.callbacks import CallbackAny2Vec

logging.getLogger("gensim").setLevel(
    logging.WARNING
)  # avoid seeing all logs from gensim.

from tqdm import tqdm

import numpy as np
import pandas as pd

from src.processing import list_shuffler
from autosklearn.classification import AutoSklearnClassifier
from autosklearn.metrics import roc_auc

np.random.seed(0)
tqdm.pandas()

class LossLogger(CallbackAny2Vec):
    """Gensim callback to output loss at each epoch"""

    def __init__(self):
        self.epoch = 1
        self.losses = []

    def on_epoch_end(self, model):
        loss = model.get_latest_training_loss()
        self.losses.append(loss)
        if self.epoch == 1:
            logger.info(f"Epoch: {self.epoch}    Loss: {loss}")
        else:
            logger.info(
                f"Epoch: {self.epoch}    Loss: {loss - self.loss_previous_step}"
            )
        self.epoch += 1
        self.loss_previous_step = loss

class ModelSaver(CallbackAny2Vec):
    """Gensim callback to save model every log_frequency epochs"""

    def __init__(
        self, d2v_object, rated_embeddings_path, w2v_model_path, log_frequency=5
    ):
        self.epoch = 1
        self.log_frequency = log_frequency
        self.d2v_object = d2v_object
        self.rated_embeddings_path = rated_embeddings_path
        self.w2v_model_path = w2v_model_path

    def on_epoch_begin(self, model):
        pass

    def on_epoch_end(self, model):
        if self.epoch % self.log_frequency == 0:
            logger.info("Saving model weights and embeddings.")
            self.d2v_object.w2v_model = model
            self.d2v_object.wv = model.wv
            self.d2v_object.save_rated_vec(self.rated_embeddings_path)
            self.d2v_object.save_w2v_model(self.w2v_model_path)
        self.epoch += 1

class D2V_Recommender:
    def __init__(
        self,
        embedding_size=100,
        window=3,
        min_count=1,
        workers=multiprocessing.cpu_count() - 1,
        num_epochs=50,
        sample=0,  # do not downsample
    ):
        self.embedding_size = embedding_size
        self.window = window
        self.min_count = min_count
        self.workers = workers
        self.num_epochs = num_epochs
        self.sample = sample

        self.wv = None  # access to embeddings with self.wv["rated_user_id"]
        self.mean_embeddings = (
            None  # access to embeddings with self.mean_embeddings["rater_user_id"]
        )
        self.data_dict = None  # dict of arrays with X_train, X_test, y_train, y_test

    def fit_rated_embeddings(
        self, d2v_train, w2v_model_path, rated_embeddings_path, resume_training=False
    ):
        """
        Fit and save Word2Vec model to embed rated users based on dating behavior of raters.

        :param d2v_train: A pd.Series of list of rated_ids (string) that were co-liked by a rater
        """
        # Prepare the data iterator
        d2v_train_iterator = self.build_data_iterator(d2v_train)

        # Initiate the model
        loss_logger = LossLogger()
        model_saver = ModelSaver(self, rated_embeddings_path, w2v_model_path)
        self.w2v_model = Word2Vec(
            size=self.embedding_size,
            window=self.window,
            min_count=self.min_count,
            workers=self.workers,
            sample=self.sample,
            sg=1,  # skip-gram
            hs=0,
            negative=5,
            callbacks=[loss_logger, model_saver],
            seed=0,
        )
        model = self.w2v_model
        if resume_training:
            logger.info("Resuming previous training.")
            model = self.load_w2v_model(w2v_model_path)
            model.build_vocab(d2v_train_iterator, update=True)
        elif model.train_count == 0:
            model.build_vocab(d2v_train_iterator)

        # train and save final model
        model.train(
            d2v_train_iterator,
            total_examples=model.corpus_count,
            epochs=self.num_epochs,
            compute_loss=True,
        )

        self.w2v_model = model
        self.wv = model.wv
        self.save_rated_vec(rated_embeddings_path)
        self.save_w2v_model(w2v_model_path)

    def fit_rater_embeddings(self, input_train, save_path=False):
        """
        Calculate an embedding for raters as the mean of embeddings of users they liked.

        :param df_: a pd.Series of list of rated_ids (string) that were co-liked by a rater
        """
        A_col, B_col, m_col = input_train.columns
        train_ = input_train.copy()
        train_ = train_[train_[m_col] > 0]  # select only those who matched

        # save the average embedding of matched people for all raters
        # TODO: optimize in one operation
        train_[B_col] = train_[B_col].apply(self.get_single_rated_vec)
        train_ = train_.dropna(
            subset=[B_col]
        )  # avoid considering the rated people appearing only once.
        train_ = train_.groupby(A_col)[B_col].apply(np.mean)
        train_.index = train_.index.astype(str)
        train_ = train_.to_frame()
        self.mean_embeddings = train_
        if save_path:
            self.save_rater_vec(save_path)

    def prepare_X_y_dataset(self, train_, test_, data_dict_path=False):
        """
        Calculate the embeddings euclidian distance (X) aligned with potential likes (y).

        :param train_: rater_id, rated_id, match (0/1) pandas Dataset
        :param test_: rater_id, rated_id, match (0/1) pandas Dataset
        :return data_dict: dictionnary of X_test, X_train, y_test, y_train np arrays.
        """
        A_col, B_col, m_col = train_.columns

        def get_vec_diff(rater_rated):
            rater, rated = rater_rated
            vec1 = self.get_single_rater_vec(rater)
            vec2 = self.get_single_rated_vec(rated)
            if vec1 is not None and vec2 is not None:
                diff = (vec1 - vec2)[0]
                dist = np.sqrt(np.sum(diff * diff, axis=0))
                return dist
            else:
                return None

        train_[A_col] = train_[[A_col, B_col]].progress_apply(
            lambda x: get_vec_diff(x), axis=1
        )
        logger.info("Finished getting distance for test dataset")
        train_ = train_.dropna()
        X_train = train_[A_col].values
        X_train = np.stack(X_train)
        y_train = train_[m_col].values
        del train_
        test_[A_col] = (
            test_[[A_col, B_col]]
            .progress_apply(lambda x: get_vec_diff(x), axis=1)
            .values
        )
        logger.info("Finished getting distance for test dataset")
        test_ = test_.dropna()
        X_test = test_[A_col].values
        X_test = np.stack(X_test)
        y_test = test_[m_col].values
        del test_

        logger.info(
            f"After skipping unseen rated users: Train size = {y_train.shape} Test size = {y_test.shape}"
        )
        data_dict = {
            "X_train": X_train,
            "X_test": X_test,
            "y_train": y_train,
            "y_test": y_test,
        }

        self.data_dict = data_dict

        if data_dict_path:
            self.save_data_dict(data_dict_path)

    def build_data_iterator(self, data):
        """ Create an iterator of which an iter is random passes on the data"""

        class shuffle_generator:
            def __init__(self, data):
                self.data = data

            def __iter__(self):
                self.data.apply(np.random.shuffle)
                return shuffle_generator_iter(self.data)

        class shuffle_generator_iter:
            def __init__(self, data):
                self.i = 0
                self.data = data
                self.data_length = len(data)

            def __iter__(self):
                # Iterators are iterables too.
                # Adding this functions to make them so.
                return self

            def __next__(self):
                if self.i < 5 * self.data_length:
                    # Shuffle at the end of the data
                    if self.i % self.data_length == 0:
                        self.data.apply(np.random.shuffle)
                    i = self.i
                    self.i += 1
                    return self.data[i % self.data_length]  # a list
                else:
                    raise StopIteration()

        return shuffle_generator(data)

    def get_single_rated_vec(self, rated_id):
        """ Get embedding vector of rated user of id rated_id"""
        try:
            return self.wv[str(rated_id)]
        except KeyError:
            # The rated user did not appear in the training dataset
            return None

    def get_single_rater_vec(self, rater_id):
        """ Get embedding vector of rater user of id rater_id"""
        try:
            return self.mean_embeddings.loc[str(rater_id)].values
        except KeyError:
            return None

    def save_rated_vec(self, wordvectors_path):
        wordvectors_path.parent.mkdir(parents=True, exist_ok=True)
        self.wv.save(str(wordvectors_path))

    def load_rated_vec(self, wordvectors_path):
        self.wv = KeyedVectors.load(str(wordvectors_path), mmap="r")
        return self.wv

    def save_w2v_model(self, w2v_model_path):
        w2v_model_path.parent.mkdir(parents=True, exist_ok=True)
        self.w2v_model.save(str(w2v_model_path))

    def load_w2v_model(self, w2v_model_path):
        self.w2v_model = Word2Vec.load(str(w2v_model_path), mmap="r")
        return self.w2v_model

    def save_rater_vec(self, rater_embeddings_path):
        # saving as a numpy array for later loading of embeddings
        rater_embeddings_path.parent.mkdir(parents=True, exist_ok=True)
        np.save(rater_embeddings_path, arr=self.mean_embeddings.reset_index().values)

    def load_rater_vec(self, rater_embeddings_path):
        arr = np.load(rater_embeddings_path, allow_pickle=True)
        self.mean_embeddings = pd.DataFrame(index=arr[:, 0].astype(str), data=arr[:, 1])
        return self.mean_embeddings

    def save_data_dict(self, data_dict_path):
        with open(data_dict_path, "wb") as handle:
            pickle.dump(self.data_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)

    def load_data_dict(self, data_dict_path):
        with open(data_dict_path, "rb") as handle:
            self.data_dict = pickle.load(handle)
        return self.data_dict


def add_missing_particles(st, rad='calc', tries=50, **kwargs):
    """
    Attempts to add missing particles to the state.

    Operates by:
    (1) featuring the difference image using feature_guess,
    (2) attempting to add the featured positions using check_add_particles.

    Parameters
    ----------
    st : :class:`peri.states.State`
        The state to check adding particles to.
    rad : Float or 'calc', optional
        The radius of the newly-added particles and of the feature size for
        featuring. Default is 'calc', which uses the median of the state's
        current radii.
    tries : Int, optional
        How many particles to attempt to add. Only tries to add the first
        ``tries`` particles, in order of mass. Default is 50.

    Other Parameters
    ----------------
    invert : Bool, optional
        Whether to invert the image. Default is ``True``, i.e. dark particles
    minmass : Float or None, optionals
        The minimum mass/masscut of a particle. Default is ``None``=calcualted
        by ``feature_guess``.
    use_tp : Bool, optional
        Whether to use trackpy in feature_guess. Default is False, since
        trackpy cuts out particles at the edge.

    do_opt : Bool, optional
        Whether to optimize the particle position before checking if it
        should be kept. Default is True (optimizes position).
    im_change_frac : Float, optional
        How good the change in error needs to be relative to the change
        in the difference image. Default is 0.2; i.e. if the error does
        not decrease by 20% of the change in the difference image, do
        not add the particle.

    min_derr : Float or '3sig', optional
        The minimal improvement in error to add a particle. Default
        is ``'3sig' = 3*st.sigma``.

    Returns
    -------
    accepts : Int
        The number of added particles
    new_poses : [N,3] list
        List of the positions of the added particles. If ``do_opt==True``,
        then these positions will differ from the input 'guess'.
    """
    if rad == 'calc':
        rad = guess_add_radii(st)

    guess, npart = feature_guess(st, rad, **kwargs)
    tries = np.min([tries, npart])

    accepts, new_poses = check_add_particles(
        st, guess[:tries], rad=rad, **kwargs)
    return accepts, new_poses


lista = []

lista.append("batman")
lista.append("atoa")
lista.append("iii")

lista2 = lista.copy()
lista3 = lista[:]

lista2.append("batgirl")

print("lista")

pop = lista.sort()

for el in range(0, 3):
    print(lista2[el])

print(pop)
print(lista3)
print("2")
print(lista2)

lista_num = ["1", "2", "3", "4"]

lista_really_num = [int(el) for el in lista_num]

print(lista_really_num)

for el in lista2:
    print(el)


#!/usr/bin/env python
# -*-coding:utf-8 -*-
###########################################
# Time  : 2021.12.08
# Author: zhangqiang
# Email : 504725439@qq.com
###########################################

import pymel.core as pm
import maya.cmds as cmds
import json
from rig_utils import *

class three_jnt_Ik():

    def __init__(self,jnts,name):
        self.jnts = jnts
        self.name =name
        self.handle_ctrl = None
        self.poleVector_ctrl = None

    @property
    def ctrl_offsets(self):

        return [i.offset for i in [self.poleVector_ctrl,self.handle_ctrl]]

    def get_pole_matrix(self):
        jnt_poss = [i.getTranslation(space="world") for i in self.jnts]
        f_dist = jnt_poss[0].distanceTo(jnt_poss[1])
        e_dist = jnt_poss[1].distanceTo(jnt_poss[2])

        back_point = jnt_poss[0] * (e_dist / (f_dist + e_dist)) + jnt_poss[2] * (f_dist / (f_dist + e_dist))
        vec = jnt_poss[1] - back_point
        vec.normalize()
        back_point += vec * f_dist
        matrix = pm.dt.Matrix()
        matrix[-1] = [back_point[0],back_point[1],back_point[2],1]
        return matrix

    def create_ctrls(self):

        hand_matrix = pm.dt.Matrix()
        hand_matrix[-1] = self.jnts[-1].getMatrix(ws=1)[-1]
        self.handle_ctrl = CtrlCreate(self.name + "_IK_Handle_Ctrl",matrix=hand_matrix,ctrl_type="cube")
        self.poleVector_ctrl = CtrlCreate(self.name+"_IK_pole_Ctrl",matrix=self.get_pole_matrix(),ctrl_type="sphere")
        self.handle_ctrl.ctrl.addAttr("follow",k=1,at="enum",en="master:")
        self.handle_ctrl.ctrl.addAttr("poleVector",k=1,min=0,max=1)
        self.handle_ctrl.ctrl.addAttr("twist",k=1)
        self.handle_ctrl.ctrl.addAttr("autoStretch",k=1,min=0,max=1)
        self.handle_ctrl.ctrl.addAttr("manualStretch",k=1)

    def jnt_bind(self):
        handle = pm.ikHandle(sj=self.jnts[0],ee=self.jnts[-1],n=self.name + "_IKHandle")[0]
        pm.parent(handle,self.handle_ctrl.ctrl)
        pm.poleVectorConstraint(self.poleVector_ctrl.ctrl,handle)

        streGrp = pm.group(em=1,p=self.jnts[0].getParent(),name=self.name + "_stretchGrp")
        streLoc_1 = pm.group(em=1,p=streGrp,name=self.name + "_stretch_%s_loc"%self.jnts[0])
        streLoc_1.setMatrix(self.jnts[0].getMatrix(ws=1),ws=1)
        pm.parentConstraint(self.jnts[0].getParent(),streLoc_1,mo=1)
        streLoc_2 = pm.group(em=1,p=streGrp,name=self.name + "_stretch_%s_loc"%self.jnts[0])
        pm.parentConstraint(self.handle_ctrl.ctrl,streLoc_2,mo=0)

        tmp_dist = quickCreateNode(type="distanceBetween",point1=streLoc_1.translate,point2=streLoc_2.translate)
        tmp_mult = quickCreateNode(type="multiplyDivide",input1X=tmp_dist.distance,input2X=tmp_dist.distance.get(),operation=2)

        tmp_md = quickCreateNode(type="multDoubleLinear",input1=999,input2 = self.handle_ctrl.ctrl.autoStretch)
        tmp_ad = quickCreateNode(type="addDoubleLinear",input1=1,input2=tmp_md.output)
        tmp_clamp = quickCreateNode(type="clamp",minR=1,maxR=tmp_ad.output,inputR=tmp_mult.outputX)

        tmp_md = quickCreateNode(type="multDoubleLinear",input1=tmp_clamp.outputR,input2=self.jnts[1].tx.get())
        tmp_ad = quickCreateNode(type="addDoubleLinear",input1=self.handle_ctrl.ctrl.manualStretch,input2=tmp_md.output)
        tmp_ad.output.connect(self.jnts[1].tx,f=1)

        tmp_md = quickCreateNode(type="multDoubleLinear",input1=tmp_clamp.outputR,input2=self.jnts[2].tx.get())
        tmp_ad = quickCreateNode(type="addDoubleLinear",input1=self.handle_ctrl.ctrl.manualStretch,input2=tmp_md.output)
        tmp_ad.output.connect(self.jnts[2].tx,f=1)

        #return handle

    def build(self):
        self.create_ctrls()
        self.jnt_bind()

    def get_groups(self):
        return self.handle_ctrl.offset,self.poleVector_ctrl.offset

class chian_FK():

    def __init__(self,jnts,name):
        self.name=name
        self.jnts = jnts
        self.ctrls = []

    def create_ctrls(self):
        for jnt in self.jnts[:-1]:
            ctrl = CtrlCreate(jnt.name() + "_Ctrl",matrix = jnt.getMatrix(ws=1))
            self.ctrls.append(ctrl)
        for i in range(len(self.ctrls))[1:][::-1]:
            pm.parent(self.ctrls[i].offset,self.ctrls[i-1].ctrl)

    def jnt_bind(self):

        for ctrl,jnt in zip(self.ctrls,self.jnts[:-1]):
            pm.parentConstraint(ctrl.dummy,jnt,mo=0)

    @property
    def ctrl_offsets(self):
        return [self.ctrls[0].offset]

    def build(self):
        self.create_ctrls()
        self.jnt_bind()

    def get_groups(self):
        return [ctrl.offset for ctrl in self.ctrls]

class blend_IK_FK():

    def __init__(self,jnts,name):
        self.jnts = jnts
        self.name = name
        self.setting_ctrl = None
        self.ik_rig = None
        self.fk_rig = None
        self.bend_ctrls = []

        set_ctrl_scale(jnts[1].translate.get().length() / 5.0)

    def duplicate_jnts(self,jnts,name):

        copy_jnt = jnts[0].duplicate(name=jnts[0].name() +name )[0]
        allChildren = copy_jnt.listRelatives(ad=1)
        notJntlist = []
        for c in allChildren:
            if c.type()=="joint":
                c.rename(c.name() + name)
            else:
                notJntlist.append(c)

        if notJntlist:
            [allChildren.remove(i) for i in notJntlist]
            pm.delete(notJntlist)
        reslut_jnt = allChildren[::-1]
        reslut_jnt.insert(0,copy_jnt)
        return reslut_jnt

    def blend_IKFK(self,baseJnts,ikJnts,fkJnts):
        for b,i,f in zip(baseJnts,ikJnts,fkJnts):
            tbc = quickCreateNode(type="blendColors",color1=f.translate,color2=i.translate,blender=self.setting_ctrl.ctrl.IKFK)
            tbc.output.connect(b.translate)
            rbc = quickCreateNode(type="blendColors",color1=f.rotate,color2=i.rotate,blender=self.setting_ctrl.ctrl.IKFK)
            rbc.output.connect(b.rotate)

            [self.setting_ctrl.ctrl.IKFK.connect(c.v,f=1) for c in self.fk_rig.ctrl_offsets]
            reverseNode = quickCreateNode(type="reverse",inputX=self.setting_ctrl.ctrl.IKFK)
            [reverseNode.outputX.connect(c.v,f=1) for c in self.ik_rig.ctrl_offsets]

    def create_ctrls(self):
        ctrl_matrix = pm.dt.Matrix()
        pos = self.jnts[0].getTranslation(space="world") * 0.5 + self.jnts[1].getTranslation(space="world")
        ctrl_matrix[-1] = [pos[0],pos[1],pos[2],1]
        self.setting_ctrl = CtrlCreate(self.name + "IKFK_setting",matrix=ctrl_matrix,ctrl_type="hex")
        self.setting_ctrl.ctrl.addAttr("IKFK",min=0,max=1,dv=0,k=1)

        tmp_matrix = self.jnts[0].getMatrix(ws=1)
        pos = self.jnts[0].getTranslation(space="world") * 0.5 + self.jnts[1].getTranslation(space="world") * 0.5
        tmp_matrix[-1] = [pos[0],pos[1],pos[2],1]
        bend_ctrl_1 = CtrlCreate(self.jnts[0].name() + "_mid_Ctrl",matrix=tmp_matrix,ctrl_type="hex")
        tmp_matrix = self.jnts[1].getMatrix(ws=1)
        bend_ctrl_2 = CtrlCreate(self.jnts[1].name() + "_Ctrl",matrix=tmp_matrix,ctrl_type="hex")
        pos = self.jnts[1].getTranslation(space="world") * 0.5 + self.jnts[2].getTranslation(space="world") * 0.5
        tmp_matrix[-1] = [pos[0],pos[1],pos[2],1]
        bend_ctrl_3 = CtrlCreate(self.jnts[1].name() + "_mid_Ctrl",matrix=tmp_matrix,ctrl_type="hex")
        self.bend_ctrls = [bend_ctrl_1,bend_ctrl_2,bend_ctrl_3]

        pm.pointConstraint(self.jnts[0],bend_ctrl_2.ctrl,bend_ctrl_1.offset,mo=1)
        #pm.orientConstraint(self.jnts[0],bend_ctrl_1.offset,mo=1)
        pm.pointConstraint(self.jnts[1],bend_ctrl_2.offset,mo=1)

        pm.orientConstraint(self.jnts[0],self.jnts[1],bend_ctrl_2.offset,mo=1)

        pm.pointConstraint(bend_ctrl_2.ctrl,self.jnts[-1],bend_ctrl_3.offset)

    def build(self):
        ikJnts = self.duplicate_jnts(self.jnts,"_IK")
        fkJnts = self.duplicate_jnts(self.jnts,"_FK")
        for i in range(len(ikJnts))[1:][::-1]:
            pm.parent(ikJnts[i],ikJnts[i-1])
        for i in range(len(fkJnts))[1:][::-1]:
            pm.parent(fkJnts[i],fkJnts[i-1])
        self.fk_rig = chian_FK(fkJnts,self.name)
        self.fk_rig.build()
        self.ik_rig = three_jnt_Ik(ikJnts,self.name)
        self.ik_rig.build()
        self.blend_IKFK(self.jnts,ikJnts,fkJnts)
        self.create_bend(*self.jnts[:-1])
        self.create_bend(*self.jnts[1:])

    def create_bend(self,sJnt,eJnt):
        bendCtrl = self.bend_ctrls[0 if self.jnts.index(sJnt)==0 else 2]
        bend_ik_jnt_1 = sJnt.duplicate(po=1,name=sJnt.name() + "_Bend_IK")[0]
        bend_ik_jnt_2 = eJnt.duplicate(po=1,name=eJnt.name() + "_Bend_IK")[0]
        pm.parent(bend_ik_jnt_2,bend_ik_jnt_1)
        pm.orientConstraint(bend_ik_jnt_1,bendCtrl.offset,mo=1)

        base_point = sJnt.getTranslation(space="world")
        base_dir = eJnt.getTranslation(space="world") - base_point
        curve_points = [base_point]
        for i in range(1,4):
            parmu = i * 0.25
            curve_points.append(base_point + base_dir * parmu)
        curve_points.append(eJnt.getTranslation(space="world"))
        bend_curve = pm.curve(p=curve_points,d=3,name=sJnt.name() + "_BendCurve")
        pm.parent(bend_curve,sJnt)
        bend_ikHandle = pm.ikHandle(sj=bend_ik_jnt_1,ee=bend_ik_jnt_2,n=sJnt.name() + "_Bend_IKHandle")[0]

        twis_pxy_wuo_off = pm.group(em=1,p=self.jnts[0].getParent(),name=sJnt.name() + "_TwisWUO_offset")
        twis_pxy_wuo = pm.group(em=1,p=twis_pxy_wuo_off,name=sJnt.name() + "_TwisWUO")
        twis_pxy_wuo_off.setMatrix(eJnt.getMatrix(ws=1),ws=1)
        twis_pxy_wuo_off.setTranslation(twis_pxy_wuo_off.getTranslation(space="world") + pm.dt.Vector(eJnt.getMatrix(ws=1)[1][:-1]) ,space="world")

        pm.dt.Vector(eJnt.getMatrix(ws=1)[1][:-1])
        pm.parentConstraint(sJnt,twis_pxy_wuo_off,mo=1)
        twis_pxy_jnt = bend_ik_jnt_2.duplicate(po=1,name=sJnt.name()+ "_TwisPxyJnt")[0]

        pm.parentConstraint(eJnt,twis_pxy_jnt,skipRotate=["x","y","z"],mo=1)
        pm.aimConstraint(sJnt,twis_pxy_jnt,aim=[-1,0,0],wu=[0,1,0],wuo=twis_pxy_wuo,mo=1,wut="object")

        pm.parent(bend_ikHandle,bend_ik_jnt_1.getParent())
        if self.jnts.index(sJnt) == 0:
            pm.pointConstraint(self.bend_ctrls[1].ctrl,bend_ikHandle,mo=1)
        else:
            pm.pointConstraint(eJnt,bend_ikHandle,mo=1)
            pm.pointConstraint(self.bend_ctrls[1].ctrl,bend_ik_jnt_1,mo=1)

        # if self.jnts.index(eJnt) == 1:
        #     pm.parentConstraint(bend_ikHandle,self.bend_ctrls[1].ctrl)

        bend_jnt_list = []

        for i in range(4):
            bend_jnt = sJnt.duplicate(po=1,name=sJnt.name() + "_Bend_Jnt_%s"%i)[0]
            pm.parent(bend_jnt,bend_ik_jnt_1)
            parmu = i * 0.25
            mp = quickCreateNode(type="motionPath",uValue=parmu,fractionMode=1,geometryPath=bend_curve.worldSpace)
            compMatrix = quickCreateNode(type="composeMatrix",inputTranslate=mp.allCoordinates)
            multMatrix = quickCreateNode(type="multMatrix")
            compMatrix.outputMatrix.connect(multMatrix.matrixIn[0])
            sJnt.worldInverseMatrix.connect(multMatrix.matrixIn[1])
            decompMatrix = quickCreateNode(type="decomposeMatrix",inputMatrix=multMatrix.matrixSum)
            decompMatrix.outputTranslate.connect(bend_jnt.translate)
            bend_jnt_list.append(bend_jnt)
            md = quickCreateNode(type="multDoubleLinear",input1=twis_pxy_jnt.rx,input2=parmu)
            ad = quickCreateNode(type="addDoubleLinear",input1=md.output,input2=bendCtrl.ctrl.rx)
            ad.output.connect(bend_jnt.rx)

        #pm.select(cl=1)
        curve_sk_jnt_1 = sJnt.duplicate(po=1,name=bend_curve.name() + "_skJnt_1")[0]
        print(bend_jnt_list[2].translate.get())
        curve_sk_jnt_2 = bend_jnt_list[2].duplicate(po=1,name=bend_curve.name() + "_skJnt_2")[0]
        curve_sk_jnt_3 = bend_jnt_list[2].duplicate(po=1,name=bend_curve.name() + "_skJnt_3")[0]
        pm.parent(curve_sk_jnt_1,w=1)
        pm.parent(curve_sk_jnt_2,curve_sk_jnt_1)
        pm.parent(curve_sk_jnt_3,curve_sk_jnt_2)

        if self.jnts.index(sJnt) == 0:
            sLoc_1 = pm.group(em=1,p=sJnt,name=curve_sk_jnt_1.name() + "_scale_loc")
            sLoc_2 = pm.group(em=1,p=sJnt,name=curve_sk_jnt_1.name() + "_scale_loc")
            pm.pointConstraint(self.bend_ctrls[1].ctrl,sLoc_2,mo=0)
        else:
            sLoc_1 = pm.group(em=1,p=eJnt,name=curve_sk_jnt_1.name() + "_scale_loc")
            sLoc_2 = pm.group(em=1,p=eJnt,name=curve_sk_jnt_1.name() + "_scale_loc")
            pm.pointConstraint(self.bend_ctrls[1].ctrl,sLoc_1,mo=0)

        tmp_dist = quickCreateNode(type="distanceBetween",point1=sLoc_1.translate,point2=sLoc_2.translate)
        tmp_mult = quickCreateNode(type="multiplyDivide",input1X=tmp_dist.distance,input2X=tmp_dist.distance.get(),operation=2)
        tmp_mult.outputX.connect(curve_sk_jnt_1.scaleX)

        sk = pm.skinCluster([curve_sk_jnt_1,curve_sk_jnt_3],bend_curve,name=bend_curve.name()+"_SKIN",tsb=1)
        sk.setWeights(bend_curve,[0,1],[1,0,0,1,0,1,0,1,1,0])

        self.bend_ctrls[0 if self.jnts.index(sJnt)==0 else 2].ctrl.translate.connect(curve_sk_jnt_3.translate)

if __name__ =="__main__":
    a = blend_IK_FK(pm.selected(),"testArm")
    a.create_ctrls()
    a.build()


import commands
from utils import tools

def show_menu():
    tools.clear_window()
    print("[1] Вывести список аккаунтов (только логины)")
    print("[2] Скопировать список участников с паролями")
    print("[3] Сменить пароли всем аккаунтам")
    print("[4] Добавить существующий аккаунт")
    print("[5] Удалить аккаунт(ы)")
    print("[6] Зарегистрировать новые аккаунты")

def main():
    cmd_dict = {'1': commands.account_list.print_list,
                '2': commands.account_list.copy_list,
                '3': commands.change_sets.shuffle_passwords,
                '4': commands.account_add.add_single,
                '5': commands.account_add.detach_accounts,
                '6': commands.account_reg.registrator}
    while True:
        show_menu()
        cmd = ''
        while cmd not in cmd_dict:
            cmd = input("Введите номер (q для выхода): ").strip().lower()
            if cmd == 'q':
                return
            if cmd not in cmd_dict:
                print("Невалидная команда, попробуйте ещё раз")
        cmd_dict[cmd]()
        input('Для продолжения нажмите Enter...: ')

if __name__ == '__main__':
    main()


class Solution:
    """
    @param A: A list of integer
    @return: The number of element in the array that
             are smaller that the given integer
    """
    def countOfSmallerNumber(self, A, queries):
        # write your code here
        A.sort()
        ans = []

        for q in queries:
            ans.append(self.bSearch(A, q))

        return ans

    def bSearch(self, A, q):
        l, r = 0, len(A) - 1

        while l < r:
            mid = l + (r - l) / 2
            if A[mid] >= q:
                r = mid
            else:
                l = mid + 1

        return l


# Create your views here.
import time
import simplejson
from django.http import HttpResponse
from common.performe_raw_sql import raw_sql
from common.render import render

def get_hosts_state():
    sql = """
    SELECT a.host_object_id, b.current_state
    FROM nagios_services a, nagios_servicestatus b
    WHERE a.service_object_id = b.service_object_id
    AND a.instance_id = b.instance_id
    ORDER BY a.host_object_id, a.display_name ASC
    """
    cursor = raw_sql(sql)
    rows = cursor.fetchall()
    return rows

def host_list(request):
    host_type = request.REQUEST.get('host_type',None)
    if not host_type:
        sql = """
        SELECT
        a.host_object_id,a.display_name,a.address,
        b.current_state,(select now()) as status_update_time,b.last_state_change,b.output
        FROM nagios_hosts a, nagios_hoststatus b
        WHERE a.host_object_id = b.host_object_id AND a.instance_id = b.instance_id
        ORDER by a.display_name ASC
        """
    elif host_type == "unhandled":
        sql = """
        SELECT
        a.host_object_id,a.display_name,a.address,
        b.current_state,(select now()) as status_update_time,b.last_state_change,b.output
        FROM nagios_hosts a, nagios_hoststatus b
        WHERE a.host_object_id = b.host_object_id AND a.instance_id = b.instance_id
        AND b.current_state !=0
        ORDER by a.display_name ASC
        """

    cursor = raw_sql(sql)
    rows = cursor.fetchall()
    host_list = list()
    for line in rows:
        temp = dict()
        temp['host_object_id'] = line[0]
        temp['display_name'] = line[1]
        temp['address'] = line[2]
        temp['current_state'] = line[3]
        duration = line[4] - line[5]
        hms = time.gmtime(duration.seconds)
        temp['state_duration'] = str(duration.days) + ' d ' + \
            str(hms.tm_hour) + ' h ' + str(hms.tm_min) +  ' m ' + str(hms.tm_sec) + ' s'
        temp['output'] = line[6]
        host_list.append(temp)
    return render('host_list/main.html',{'host_list':host_list,'services_list':get_hosts_state()},request)


#! /usr/bin/env python3

import random
import os
import sys
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

def plot_image(i, predictions_array, true_label, img):
  predictions_array, true_label, img = predictions_array, true_label[i], img[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])

  plt.imshow(img, cmap=plt.cm.binary)

  predicted_label = np.argmax(predictions_array)
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'

  plt.xlabel("test {} {:2.0f}% ({})".format(i,
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)

def plot_value_array(i, predictions_array, true_label):
  predictions_array, true_label = predictions_array, true_label[i]
  plt.grid(False)
  plt.xticks(range(10))
  plt.yticks([])
  thisplot = plt.bar(range(10), predictions_array, color="#777777")
  plt.ylim([0, 1])
  predicted_label = np.argmax(predictions_array)

  thisplot[predicted_label].set_color('red')
  thisplot[true_label].set_color('blue')

print('tensorflow version is', tf.__version__)

# the module gives easy access to the test data
# first run this will download to a local file
# subsequent runs it will be much quicker
#fashion_mnist = keras.datasets.fashion_mnist
#(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
#class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
#               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

number_mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) =  number_mnist.load_data()
class_names = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

# see what the data looks like
show_data = (len(sys.argv) > 1)
if show_data:
   plt.figure(figsize=(10,10))
   for i in range(25):
       plt.subplot(5,5,i+1)
       plt.xticks([])
       plt.yticks([])
       plt.grid(False)
       plt.imshow(train_images[i], cmap=plt.cm.binary)
       plt.xlabel(class_names[train_labels[i]])
   plt.show()

train_images = train_images / 255.0
test_images  = test_images  / 255.0

if os.path.exists('model.json') and os.path.exists('model.h5'):
   with open('model.json') as file:
      json = file.read()
   model = keras.models.model_from_json(json)
   model.load_weights('model.h5')
   print('loaded model from disk')

   model.compile(optimizer='adam',
                 loss='sparse_categorical_crossentropy',
                 metrics=['accuracy'])
else:
   model = keras.Sequential([
       keras.layers.Flatten(input_shape=(28, 28)),
       keras.layers.Dense(128, activation='relu'),
       keras.layers.Dense(10, activation='softmax')
   ])

   model.compile(optimizer='adam',
                 loss='sparse_categorical_crossentropy',
                 metrics=['accuracy'])

   # this is the training step that takes all the time
   model.fit(train_images, train_labels, epochs=5)

   with open('model.json', 'w') as file:
      file.write(model.to_json())
   model.save_weights('model.h5')
   print('saved model to files model.json and model.h5')

# either way now we have a model with weights; write them out to csv
i = 0
for a in model.get_weights():
   fname = 'model.weights'+str(i)+'.csv'
   i += 1
   with open(fname, 'w') as csv:
      if len(a.shape)==2:
         rows, cols = a.shape
         for r in range(rows):
            for c in range(cols):
               csv.write(str(a[r][c]) + ',')
            csv.write("\n")
      else:
         rows = a.shape[0]
         for r in range(rows):
            csv.write(str(a[r]) + "\n")

test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print('\nTest loss:    ', test_loss)
print('Test accuracy:', test_acc)

# this applies the model to all the test images
predictions = model.predict(test_images)

num_rows = 10
num_cols = 6
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for ii in range(num_images):
  i = random.randint(0,len(test_images)-1)
  plt.subplot(num_rows, 2*num_cols, 2*ii+1)
  plot_image(i, predictions[i], test_labels, test_images)
  plt.subplot(num_rows, 2*num_cols, 2*ii+2)
  plot_value_array(i, predictions[i], test_labels)
plt.tight_layout()
plt.show()


######## Webcam Object Detection Using Tensorflow-trained Classifier #########

# This program uses a TensorFlow-trained classifier to perform object detection.
# It loads the classifier uses it to perform object detection on a webcam feed.
# It draws boxes and scores around the objects of interest in each frame from
# the webcam.

# Import packages
import os
import cv2
import numpy as np
import tensorflow as tf
from utils import label_map_util
from utils import visualization_utils as vis_util
from playsound import playsound
detection_graph = tf.Graph()

# Import utilites
# from utils import label_map_util

# Name of the directory containing the object detection module we're using
TRAINED_MODEL_DIR = 'frozen_graphs'

# Path to frozen detection graph .pb file, which contains the model that is used
# for object detection.
PATH_TO_CKPT = TRAINED_MODEL_DIR + '/frozen_inference_graph.pb'
print(PATH_TO_CKPT)
# Path to label map file
PATH_TO_LABELS = TRAINED_MODEL_DIR + '/labelmap.pbtxt'

# Number of classes the object detector can identify

NUM_CLASSES = 2
# load label map using utils provided by tensorflow object detection api
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(
    label_map, max_num_classes = NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)

# Load the Tensorflow model into memory.

print("> ====== Loading frozen graph into memory")
detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')

    sess = tf.Session(graph=detection_graph)
    print(">  ====== Inference graph loaded.")

# Define input and output tensors (i.e. data) for the object detection classifier

# Input tensor is the image
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

# Output tensors are the detection boxes, scores, and classes
# Each box represents a part of the image where a particular object was detected
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

# Each score represents level of confidence for each of the objects.
# The score is shown on the result image, together with the class label.
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')

# Number of objects detected
num_detections = detection_graph.get_tensor_by_name('num_detections:0')

# Initialize webcam feed
video = cv2.VideoCapture(0)

while True:

    # Acquire frame and expand frame dimensions to have shape: [1, None, None, 3]
    # single-column array, where each item in the column has the pixel RGB value
    _, frame = video.read()
    frame = cv2.resize(frame, (900, 900), interpolation=cv2.INTER_AREA)

    frame_expanded = np.expand_dims(frame, axis=0)

    # Perform the actual detection by running the model with the image as input
    (boxes, scores, classes, num) = sess.run(
        [detection_boxes, detection_scores, detection_classes, num_detections],
        feed_dict={image_tensor: frame_expanded})

    result = scores.flatten()
    new_classes = classes.flatten()

    index = []

    for i, value in enumerate(result):
        if value > 0.70:
            index.append(i)

    new_class = []
    for ind in index:
        new_class.append(new_classes[ind])

    try:
        if 1 in new_class and 2 not in new_class:

            k = vis_util.visualize_boxes_and_labels_on_image_array(frame,
                                                                   np.squeeze(boxes),
                                                                   np.squeeze(classes).astype(np.int32),
                                                                   np.squeeze(scores),
                                                                   category_index,
                                                                   use_normalized_coordinates=True,
                                                                   line_thickness=8,
                                                                   min_score_thresh=0.75)

            cv2.imshow('Object detector', k)

        elif 2 in new_class:

            k1 = vis_util.visualize_boxes_and_labels_on_image_array(frame,
                                                                   np.squeeze(boxes),
                                                                   np.squeeze(classes).astype(np.int32),
                                                                   np.squeeze(scores),
                                                                   category_index,
                                                                   use_normalized_coordinates=True,
                                                                   line_thickness=8,
                                                                   min_score_thresh=0.75)

            cv2.putText(frame, "ALERT", (0, 180), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 2)
            playsound(r"alert.wav")
            #cv2.resize(frame, (600, 600), interpolation=cv2.INTER_AREA)
            cv2.imshow('Object detector', k1)

        else:
            print("Here, There is no detection related to mask or not mask")

    except Exception as e:
        print("Errors", e)

    if cv2.waitKey(1) == ord('q'):
        break
# Clean up
video.release()
cv2.destroyAllWindows()


from lxml import etree
from mpkg.common import Soft
from mpkg.utils import GetPage

class Package(Soft):
    ID = 'virtualbox'

    def _prepare(self):
        data = self.data
        url = 'https://www.virtualbox.org/wiki/Downloads'
        links = etree.HTML(GetPage(url)).xpath('//*[@class="ext-link"]')
        link = [link for link in links if list(
            link.itertext())[1] == 'Windows hosts'][0]
        data.arch = {'64bit': link.values()[1]}
        data.changelog = 'https://www.virtualbox.org/wiki/Changelog'
        data.ver = data.arch['64bit'].split('/virtualbox/')[1].split('/')[0]


from django import template
from slimit import minify

register = template.Library()

class MinifyNode(template.Node):
    """
    Node for the minify_js template tag.
    """
    def __init__(self, nodelist):
        self.nodelist = nodelist

    def render(self, context):
        return minify(
            self.nodelist.render(context),
            mangle=True,
            mangle_toplevel=True
        )

@register.tag
def minify_js(parser, token):
    """
    Block tag that minifies the JavaScript it contains.
    """
    nodelist = parser.parse(('endminify_js',))
    parser.delete_first_token()
    return MinifyNode(nodelist)


# coding: utf-8

from __future__ import absolute_import

from flask import json
from six import BytesIO

from swagger_server.test import BaseTestCase

class TestPostCardController(BaseTestCase):
    """PostCardController integration test stubs"""

    def test_postcard_createpostcard(self):
        """Test case for postcard_createpostcard

        Create Postcard
        """
        data = dict(to='to_example',
                    _from='_from_example',
                    attachbyid='attachbyid_example',
                    front='front_example',
                    back='back_example',
                    message='message_example',
                    setting='setting_example',
                    description='description_example',
                    htmldata='htmldata_example')
        response = self.client.open(
            '/api/v3/Postcard/createpostcard.json',
            method='POST',
            data=data,
            content_type='application/x-www-form-urlencoded')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

    def test_postcard_deletepostcard(self):
        """Test case for postcard_deletepostcard

        Delete Postcard
        """
        data = dict(postcardid='postcardid_example')
        response = self.client.open(
            '/api/v3/Postcard/deletepostcard.json',
            method='POST',
            data=data,
            content_type='application/x-www-form-urlencoded')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

    def test_postcard_listpostcard(self):
        """Test case for postcard_listpostcard

        List Postcards
        """
        data = dict(page=1,
                    pagesize=10,
                    postcardid='postcardid_example',
                    dateCreated='dateCreated_example')
        response = self.client.open(
            '/api/v3/Postcard/listpostcard.json',
            method='POST',
            data=data,
            content_type='application/x-www-form-urlencoded')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

    def test_postcard_viewpostcard(self):
        """Test case for postcard_viewpostcard

        View Postcard
        """
        data = dict(postcardid='postcardid_example')
        response = self.client.open(
            '/api/v3/Postcard/viewpostcard.json',
            method='POST',
            data=data,
            content_type='application/x-www-form-urlencoded')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

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


"Niftools Vertex Correction"
"Dirty script to duplicate a nif and overwrite its vert colors"

import site
import bpy
from pyffi.formats.nif import NifFormat

class NifDataHolder:
    """ A python singleton """

    class __impl:
        """ Implementation of the singleton interface """
        def __init__(self):
            self.data = null

        def getinstancedata(self):
            return self.data

        def setinstancedata(self,data):
            self.data = data

    # storage for the instance reference
    __instance = None

    def __init__(self):
        """ Create singleton instance """
        # Check whether we already have an instance
        if Singleton.__instance is None:
            # Create and remember instance
            Singleton.__instance = Singleton.__impl()

        # Store instance reference as the only member in the handle
        self.__dict__['_Singleton__instance'] = Singleton.__instance

    def __getattr__(self, attr):
        """ Delegate access to implementation """
        return getattr(self.__instance, attr)

    def __setattr__(self, attr, value):
        """ Delegate access to implementation """
        return setattr(self.__instance, attr, value)

class NifReader(bpy.types.Operator):
    "This is a dirty script used to change vert color"

    '''Bevels selected edges of the mesh'''
    bl_idname = "vertexcolor.nifread"
    bl_label = "Duplicate mesh and overwrite the vert color"

    def execute(self, context):
        filein = bpy.context.scene.vertexcolor.fileinput
        hexvalue = bpy.context.scene.vertexcolor.hexwidget

        # open file for binary reading
        print("Importing %s" % filein)
        niffile = open(filein, "rb")
        self.data = NifFormat.Data()

        # check if nif file is valid
        self.data.inspect(niffile)
        if self.data.version >= 0:
            # it is valid, so read the file
            print("NIF file version: 0x%08X" % self.data.version)
            print("Reading file")
            self.data.read(niffile)
        elif self.data.version == -1:
            print("Unsupported NIF version.")
        else:
            print("Not a NIF file.")

        for root in self.data.roots:
            for block in root.tree():
                if isinstance(block, NifFormat.NiTriShape):
                    if(block.data.has_vertex_colors):
                        vertexcol = block.data.vertex_colors[0]
                        bpy.context.scene.vertexcolor.hexwidget = [vertexcol._items[0]._value,
                                                                   vertexcol._items[1]._value,
                                                                   vertexcol._items[2]._value]

        if(bpy.context.scene.vertexcolor.fileoutput == ""):
            print("check run ok")
            input = bpy.context.scene.vertexcolor.fileinput
            bpy.context.scene.vertexcolor.fileoutput = input[ :-4] + "_copy.nif"

        niffile.close()
        print("Finished Reading file :D")

        nif = NifDataHolder()
        nif.setinstancedata(self.data)
        return{'FINISHED'}

class NifWriter(bpy.types.Operator):
    "This will write the loaded file"

    bl_idname = "vertexcolor.nifwrite"
    bl_label = "Output file with overwritten vert color"

    def execute(self, context):

        nif = NifDataHolder()
        if(nif.getinstancedata() == null):
            bpy.context.scene.vertexcolor.helpmsg
        else:
            self.data = nif.getinstancedata()
            for root in self.data.roots:
                for block in root.tree():
                    if isinstance(block, NifFormat.NiTriShape):
                        if(block.data.has_vertex_colors):
                            for vertexcol in block.data.vertex_colors:
                                vertexcol._items[0]._value = bpy.context.scene.vertexcolor.hexwidget[0]
                                vertexcol._items[1]._value = bpy.context.scene.vertexcolor.hexwidget[1]
                                vertexcol._items[2]._value = bpy.context.scene.vertexcolor.hexwidget[2]
                            print(block.data.vertex_colors)
            stream = open(bpy.context.scene.vertexcolor.fileoutput, "wb")
            try:
                self.data.write(stream)
                print("Writing File")
            finally:
                stream.close()

        return{'FINISHED'}

class Popup(bpy.types.Operator):
    bl_idname = "vertexcolor.helpmsg"
    bl_label = "Useful info messages"

    test = bpy.props.StringProperty(name="warning", default="Please load the nif before trying to save")

    def execute(self, context):
        wm = context.window_manager
        return wm.invoke_props_dialog(self)


import torch
import torch.nn.functional as F
import math

def conv3D_output_shape_f(i, input_shape, kernel_size, dilation, padding, stride):
    """
    Calculate the original output size assuming the convolution is nn.Conv3d based on
    input size, kernel size, dilation, padding and stride.
    """
    return math.floor((input_shape[i]-kernel_size[i]-(dilation[i]-1)*
                                        (kernel_size[i]-1)+2*padding[i])
                                    /stride[i])+1

def acs_conv_f(x, weight, bias, kernel_size, dilation, padding, stride, groups, out_channels, acs_kernel_split):
    B, C_in, *input_shape = x.shape
    C_out = weight.shape[0]
    assert groups==1 or groups==C_in==C_out, "only support standard or depthwise conv"

    conv3D_output_shape = (conv3D_output_shape_f(0, input_shape, kernel_size, dilation, padding, stride),
                            conv3D_output_shape_f(1, input_shape, kernel_size, dilation, padding, stride),
                            conv3D_output_shape_f(2, input_shape, kernel_size, dilation, padding, stride))

    weight_a = weight[0:acs_kernel_split[0]].unsqueeze(2)
    weight_c = weight[acs_kernel_split[0]:(acs_kernel_split[0]+acs_kernel_split[1])].unsqueeze(3)
    weight_s = weight[(acs_kernel_split[0]+acs_kernel_split[1]):].unsqueeze(4)
    if groups==C_in==C_out:
        # depth-wise
        x_a = x[:, 0:acs_kernel_split[0]]
        x_c = x[:, acs_kernel_split[0]:(acs_kernel_split[0]+acs_kernel_split[1])]
        x_s = x[:, (acs_kernel_split[0]+acs_kernel_split[1]):]
        group_a = acs_kernel_split[0]
        group_c = acs_kernel_split[1]
        group_s = acs_kernel_split[2]
    else:
        # groups=1
        x_a = x_c = x_s = x
        group_a = group_c = group_s = 1

    f_out = []
    if acs_kernel_split[0]>0:
        a = F.conv3d(x_a if conv3D_output_shape[0]==input_shape[0] or 2*conv3D_output_shape[0]==input_shape[0] else F.pad(x, (0,0,0,0,padding[0],padding[0]),'constant',0)[:,:,
                                            kernel_size[0]//2:kernel_size[0]//2+(conv3D_output_shape[0]-1)*stride[0]+1,
                                            :,:],
                                            weight=weight_a, bias=None,
                                            stride=stride,
                                            padding=(0,padding[1],padding[2]),
                                            dilation=dilation,
                                            groups=group_a)
        f_out.append(a)
    if acs_kernel_split[1]>0:
        c = F.conv3d(x_c if conv3D_output_shape[1]==input_shape[1] or 2*conv3D_output_shape[1]==input_shape[1] else F.pad(x, (0,0,padding[1],padding[1]),'constant',0)[:,:,:,
                                            kernel_size[1]//2:kernel_size[1]//2+stride[1]*(conv3D_output_shape[1]-1)+1,
                                            :],
                                            weight=weight_c, bias=None,
                                            stride=stride,
                                            padding=(padding[0],0,padding[2]),
                                            dilation=dilation,
                                            groups=group_c)
        f_out.append(c)
    if acs_kernel_split[2]>0:
        s = F.conv3d(x_s if conv3D_output_shape[2]==input_shape[2] or 2*conv3D_output_shape[2]==input_shape[2] else F.pad(x, (padding[2],padding[2]),'constant',0)[:,:,:,:,
                                            kernel_size[2]//2:kernel_size[2]//2+stride[2]*(conv3D_output_shape[2]-1)+1
                                            ],
                                            weight=weight_s,
                                            bias=None,
                                            stride=stride,
                                            padding=(padding[0],padding[1],0),
                                            dilation=dilation,
                                            groups=group_s)
        f_out.append(s)
    f = torch.cat(f_out, dim=1)

    if bias is not None:
        f += bias.view(1,out_channels,1,1,1)

    return f


from vector import Vector

"""Using the Vector Operations to represent a colour, stored in RGB dimentions"""

class Colour(Vector):

    @classmethod
    def from_hex(cls, hexcolour="#000000"):
        r = int(hexcolour[1:3], 16) / 255.0
        g = int(hexcolour[3:5], 16) / 255.0
        b = int(hexcolour[5:7], 16) / 255.0
        return cls(r, g, b)


import re
import json
from uuid import uuid4
from datetime import datetime
from apt.cache import Cache
from .piplist import getpip

def tocyclonedx(pattern = None,dopip=False):
    """
    Routine to convert apt information to CycloneDx.
    """
    pkgs = [ ]
    deps = []

    sbom = {
        "bomFormat": "CycloneDX",
        "specVersion": "1.4",
        "serialNumber": "urn:uuid:" + str(uuid4()),
        "version": 1
        }
    meta = {
        "timestamp": re.sub(r'[.].*$','',str(datetime.now().isoformat())) + 'Z',
        "tools" : [ {
            "vendor" : "Eliot Lear",
            "name" : "apt2sbom",
            } ],
        "component" : {
            "type" : "device",
            "name" : "ubuntu-derived-system",
            "version" : "1"
            },
        "licenses" : [ { "license" : {
            "id" : "BSD-3-Clause"
            }
        } ]
      }
    sbom['metadata']= meta

    cache=Cache()

    for pkg in cache:
        if not pkg.is_installed:
            continue
        if pattern:
            if not re.match(pattern,pkg.name):
                continue
        ver = pkg.installed
        pack = { }
        pack['type'] = 'application'
        pack["name"]=pkg.name
        rec_info=ver.record
        pack["bom-ref"] = pkg.name
        pack["version"] = ver.version
        pack["purl"] = "pkg:deb/ubuntu/" + pkg.name + "@" + ver.version +\
            "?arch=" + ver.architecture
        pack["supplier"] = { "name" : rec_info['Maintainer'] }
        if not ver.uri is None and not ver.uri == "":
            pack['supplier']['url']= [ ver.uri ]

        hashes= []
        try:
            hashes.append({ "alg" : 'SHA-256',
                       'content' : ver.sha256 })
        except SystemError:
            pass
        try:
            hashes.append({ "alg" : 'SHA-1',
                       'content' : ver.sha1 })
        except SystemError:
            pass

        try:
            hashes.append({ "alg" : 'MD5',
                       'content' : ver.md5 })
        except SystemError:
            pass

        if hashes:
            pack['hashes'] = hashes

        if ver.homepage != '':
            pack['externalReferences'] = [ {
                "url" : ver.homepage,
                "type" : "website"
            } ]

        if ver.dependencies:
            dep = { "ref" : pack["bom-ref"] }
            dees = []

            for dep_ent in ver.dependencies:
                tname=re.sub(':any$','',dep_ent[0].name)
                if tname in cache and cache[tname].is_installed and\
                   not tname in dees:
                    dees.append(tname)

            if dees:
                dep["dependsOn"] = dees
                deps.append(dep)

        pkgs.append(pack)

    if dopip:
        pips = getpip()
        for pip in pips:
            if pattern:
                if not re.match(pattern,pip['Name']):
                    continue
            pack={}
            pack["name"]=pip["Name"] + ".pip"
            pack['type']="application"
            pack["version"] = pip["Version"]
            pack["purl"] = "pkg:pypi/" + re.sub('_','-',pack["name"].lower()) +\
                "@" + pack["version"]
            pack["bom-ref"] = pack["name"]
            pack["supplier"] = {
                "name" : pip["Author"]
                }
            try:
                pack["externalReferences"] = [ {
                    "url" : pip["homepage"],
                    "type" : "website"
                    }]
            except KeyError:
                pass
            pkgs.append(pack)

    sbom['components'] = pkgs
    sbom['dependencies'] = deps
    return json.dumps(sbom)


# -*- coding: utf-8 -*-
"""
Created on Sun Apr 18 22:29:51 2021

@author: tanzheng
"""

class SpecialTokens:
    bos = '<bos>'
    eos = '<eos>'
    pad = '<pad>'
    unk = '<unk>'

class CharVocab:
    def __init__(self, chars_set, st=SpecialTokens):
        all_symbols = sorted(list(chars_set)) + [st.bos, st.eos, st.pad, st.unk]

        self.st = st

        self.c2i = {c: i for i, c in enumerate(all_symbols)}
        self.i2c = {i: c for i, c in enumerate(all_symbols)}

    @property
    def bos_id(self):
        return self.c2i[self.st.bos]

    @property
    def eos_id(self):
        return self.c2i[self.st.eos]

    @property
    def pad_id(self):
        return self.c2i[self.st.pad]

    @property
    def unk_id(self):
        return self.c2i[self.st.unk]

    def char2id(self, char):
        if char not in self.c2i:
            return self.unk_id

        return self.c2i[char]

    def id2char(self, id):
        if id not in self.i2c:
            return self.st.unk

        return self.i2c[id]

    ###################################################################
    def string2ids(self, string, add_bos=False, add_eos=False):
        ids = [self.char2id(c) for c in string]

        if add_bos:
            ids = [self.bos_id] + ids
        if add_eos:
            ids = ids + [self.eos_id]

        return ids

    def ids2string(self, ids, rem_bos=True, rem_eos=True):
        if len(ids) == 0:
            return ''
        if rem_bos and ids[0] == self.bos_id:
            ids = ids[1:]
        if rem_eos and ids[-1] == self.eos_id:
            ids = ids[:-1]

        string = ''.join([self.id2char(id) for id in ids])

        return string

###################################################################
def char_set(string_set):
    charset = set()

    for string in string_set:
        charset.update(string)

    return charset


from numpipe import scheduler, pbar
import numpy as np
from time import sleep

job = scheduler()

@job.cache
def sim():
    for i in pbar(range(100)):
        yield dict(counter=i)
        sleep(1)

@job.cache
def other():
    for i in pbar(range(100)):
        yield dict(counter=i)
        sleep(1)

@job.at_end
def vis():
    var = job.load(sim)
    print(var.counter)

if __name__ == '__main__':
    job.run()


import sys
import os
from os import listdir as listdir
from os.path import join
from os.path import isdir
import numpy as np
import dicom2nifti
import mne
import matplotlib.pyplot as plt
sys.path.insert(0, '/imaging/ai05/RED/RED_MEG/resting/analysis/RED_Rest')
from REDTools import sourcespace_setup
from REDTools import sourcespace_command_line
from REDTools import study_info
from REDTools import connectivity
import joblib
#%%

#located in imaging/rs04/RED/DICOMs

# The T1s DICOMS are located in a folder in a folder for each participants
# The name of this folder is the same for each participant
MP_name = 'Series005_CBU_MPRAGE_32chn'
dicoms = '/imaging/rs04/RED/DICOMs'
STRUCTDIR = '/imaging/ai05/RED/RED_MEG/resting/STRUCTURALS'
MAINDIR = '/imaging/ai05/RED/RED_MEG/resting'
folders = [f for f in os.listdir(dicoms) if os.path.isdir(os.path.join(dicoms,f))]
ids = [f.split('_')[0] for f in folders]

#get ids etc
RED_id, MR_id, MEG_fname = study_info.get_info()
#change fsaverage so they can find

#%% define function for envelope corellations
# input settings for this function and then run
i = 4
invdir = join(MAINDIR, 'invs')
rawdir = join(MAINDIR, 'preprocessed')
outdir = join(MAINDIR, 'envelope_cors')
fsdir = join(MAINDIR, 'STRUCTURALS','FS_SUBDIR')
MEG_fname = MEG_fname
RED_id = RED_id
MR_id = MR_id
method = 'MNE'
combine = 'mean'
lambda2 = 1. / 9.
pick_ori = 'normal'
parc = 'aparc'
time_course_mode = 'mean'

#frequencies we are gonna do
freqs = {'Theta_s':(4,7),
         'Alpha_s':(8,12),
         'Lower Beta_s': (13, 20),
         'Upper Beta_s':(21, 30)}

# make list of dicts for input (we're gonna use job lib for this)
dictlist = []
for f in freqs:
    freq = freqs[f]
    freqname = f
    dictlist.append(dict(
        i = i,
        invdir = invdir,
        rawdir = rawdir,
        outdir = outdir,
        fsdir = fsdir,
        MEG_fname = MEG_fname,
        RED_id = RED_id,
        MR_id = MR_id,
        method = method,
        combine = combine,
        lambda2 =lambda2,
        pick_ori = pick_ori,
        parc = parc,
        time_course_mode = time_course_mode,
        freq = freq,
        freqname = freqname,
    ))

joblib.Parallel(n_jobs=len(dictlist))(
    joblib.delayed(connectivity.envelope_corellation)(indict) for indict in dictlist)

#%% or instead use cluster version
connectivity.cluster_envelope_corellation(MR_id, MAINDIR)

#%% Calculate average corellation matrix for each brain
aec_dir = join(MAINDIR, 'envelope_cors')
thetas = [f for f in listdir(aec_dir) if 'Theta' in f] # find thetas
thetas = [thetas[[i for i, s in enumerate(thetas) if i_d in s][0]] for i_d in RED_id] # reorder to match RED
#repeat for all bands
alphas =[f for f in listdir(aec_dir) if 'Alpha' in f]
alphas = [alphas[[i for i, s in enumerate(alphas) if i_d in s][0]] for i_d in RED_id]
betalow = [f for f in listdir(aec_dir) if 'Lower Beta' in f]
betalow = [betalow[[i for i, s in enumerate(betalow) if i_d in s][0]] for i_d in RED_id]
betahigh =[f for f in listdir(aec_dir) if 'Upper Beta' in f]
betahigh = [betahigh[[i for i, s in enumerate(betahigh) if i_d in s][0]] for i_d in RED_id]

# Combine each band into an average connectivity matrix
arraylist = []
for band in [thetas, alphas, betalow, betahigh]:
    #intialise empty array
    mat_3d = np.zeros(shape=(68, 68, len(band)))
    for i, f in enumerate(band):
        mat_3d[:,:,i] = np.load(join(aec_dir, f))
    arraylist.append(mat_3d)

#%% make a basic average array for one frequency
freq_labels = ['thetas', 'alphas', 'betalow', 'betahigh']
threshold = 0.05 # threshold for strength
perc_part = 0.70 # Threshold for how many participants
for i in range(len(freq_labels)):
    thisarray = arraylist[i]
    corr = thisarray.mean(axis=2) # simple mean
    # try thresholding per participants
    bools = thisarray > threshold # where we are above threshold
    chn_mask = np.zeros(thisarray.shape[0:2], dtype=bool)
    corr_masked = np.zeros(thisarray.shape[0:2])
    for ii in range(thisarray.shape[0]): #channels where perce participants above threshold
        for iii in range(thisarray.shape[1]):
            perc = sum(bools[ii,iii])/len(bools[ii,iii])
            print(perc)
            if perc < perc_part:
                chn_mask[ii,iii] = False
                corr_masked[ii,iii] = 0
            else:
                chn_mask[ii,iii] = True
                corr_masked[ii,iii] = corr[ii,iii]
    corr2plot = corr_masked
    nlines=300

    #% plot corellation matrix
    # let's plot this matrix
    fig, ax = plt.subplots(figsize=(4, 4))
    ax.imshow(corr2plot, cmap='viridis', clim=np.percentile(corr, [5, 95]))
    fig.tight_layout()
    plt.savefig(f'/imaging/ai05/images/AEC{freq_labels[i]}_group_mat.png')

    #% plot this on a brain
    threshold_prop = 0.3  # percentage of strongest edges to keep in the graph
    #degree = mne.connectivity.degree(corr, threshold_prop=threshold_prop) # get the degree
    degree = mne.connectivity.degree(corr2plot) # get the degree
    #load in FSAVERAGE for parcellations and inverse for plotting
    labels = mne.read_labels_from_annot('fsaverage_1', parc='aparc',
                                        subjects_dir=join(MAINDIR, 'STRUCTURALS','FS_SUBDIR'))
    labels = labels[0:-1]
    # we know that 99144's inversion contains source from FSAVERAGE so load that
    inv_op = mne.minimum_norm.read_inverse_operator(join(invdir, '99144-inv.fif'))
    stc = mne.labels_to_stc(labels, degree)
    stc = stc.in_label(mne.Label(inv_op['src'][0]['vertno'], hemi='lh') +
                       mne.Label(inv_op['src'][1]['vertno'], hemi='rh'))
    stc.plot(
        clim=dict(kind='percent', lims=[75, 85, 95]), colormap='gnuplot',
        subjects_dir=join(STRUCTDIR, 'FS_SUBDIR'), hemi='lh',
        smoothing_steps=1, time_label=freq_labels[i], backend='matplotlib').savefig(f'/imaging/ai05/images/AEC_{freq_labels[i]}_src_group_lh.png')
    stc.plot(
        clim=dict(kind='percent', lims=[75, 85, 95]), colormap='gnuplot',
        subjects_dir=join(STRUCTDIR, 'FS_SUBDIR'), hemi='rh',
        smoothing_steps=1, time_label=freq_labels[i], backend='matplotlib').savefig(f'/imaging/ai05/images/AEC_{freq_labels[i]}_src_group_rh.png')

    #% make a circle plot
    # First, we reorder the labels based on their location in the left hemi
    label_names = [label.name for label in labels]
    lh_labels = [name for name in label_names if name.endswith('lh')]

    # Get the y-location of the label
    label_ypos = list()
    for name in lh_labels:
        idx = label_names.index(name)
        ypos = np.mean(labels[idx].pos[:, 1])
        label_ypos.append(ypos)

    # Reorder the labels based on their location
    lh_labels = [label for (yp, label) in sorted(zip(label_ypos, lh_labels))]

    # For the right hemi
    rh_labels = [label[:-2] + 'rh' for label in lh_labels]

    # Save the plot order and create a circular layout
    node_order = list()
    node_order.extend(lh_labels[::-1])  # reverse the order
    node_order.extend(rh_labels)

    node_angles = mne.viz.circular_layout(label_names, node_order, start_pos=90,
                                  group_boundaries=[0, len(label_names) / 2])
    label_colors = [label.color for label in labels]
    # Plot the graph using node colors from the FreeSurfer parcellation. We only
    # show the 300 strongest connections.
    mne.viz.plot_connectivity_circle(corr2plot, label_names, n_lines=nlines,
                             node_angles=node_angles, node_colors=label_colors,
                             title=f'{freq_labels[i]}')[0].savefig(f'/imaging/ai05/images/AEC_{freq_labels[i]}_circle.png')

    plotting.plot_connectome(corr_masked, coords,
                             edge_threshold="95%",
                             title=f'{freq_labels[i]}').savefig(f'/imaging/ai05/images/AEC_{freq_labels[i]}net.png')
#%% vertex to MNI space
coords = []
for i in range(len(labels)):
    if 'lh' in label_names[i]:
        hem = 1
    else:
        hem = 0
    coord = mne.vertex_to_mni(labels[i].center_of_mass(subjects_dir=fsdir), subject='fsaverage_1',hemis=hem,subjects_dir=fsdir)
    coords.append(coord[0])
#%% nii learn plots
from nilearn import datasets
from nilearn.input_data import NiftiLabelsMasker
from nilearn import plotting
des = datasets.fetch_atlas_surf_destrieux()
# create masker to extract functional data within atlas parcels
masker = NiftiLabelsMasker(labels_img=labels, standardize=True,
                           memory='nilearn_cache')
coordinates = plotting.find_parcellation_cut_coords(labels_img='/imaging/ai05/RED/RED_MEG/resting/analysis/aparcaseg.nii.gz')

#%% Meeting notes from 6/2/2019
#Continous concetantion, downsample after hilbert envelope
#kanads method section of PhD -- threshold free level setting (singular value decomposition)
#

#%% source recon continous raw data
pythonpath = '/home/ai05/.conda/envs/mne_2/bin/python'
for i in range(len(RED_id)):
    pycom = f"""
import sys
import os
from os.path import join
import numpy as np
import mne
sys.path.insert(0, '/imaging/ai05/RED/RED_MEG/resting/analysis/RED_Rest')
from REDTools import study_info

MP_name = 'Series005_CBU_MPRAGE_32chn'
dicoms = '/imaging/rs04/RED/DICOMs'
STRUCTDIR = '/imaging/ai05/RED/RED_MEG/resting/STRUCTURALS'
MAINDIR = '/imaging/ai05/RED/RED_MEG/resting'
folders = [f for f in os.listdir(dicoms) if os.path.isdir(os.path.join(dicoms,f))]
ids = [f.split('_')[0] for f in folders]

#get ids etc
RED_id, MR_id, MEG_fname = study_info.get_info()

def raw2source(meg_f, inv_op, method, outdir):
    raw = mne.io.read_raw_fif(meg_f, preload=True)
    inv = mne.minimum_norm.read_inverse_operator(inv_op)
    start, stop = raw.time_as_index([60, raw.times[-30000]])
    stc = mne.minimum_norm.apply_inverse_raw(raw, inv,
                                             lambda2=1.0/1.0**2,
                                             method=method,
                                             start=start,
                                             stop=stop,
                                             buffer_size=int(len(raw.times)/10))
    np.save(join(outdir, f'{{os.path.basename(meg_f).split("_")[0]}}_{{method}}_stc_data.npy'),stc.data)
    np.save(join(outdir, f'{{os.path.basename(meg_f).split("_")[0]}}_{{method}}_stc_vertices.npy'),stc.vertices)
    np.save(join(outdir, f'{{os.path.basename(meg_f).split("_")[0]}}_{{method}}_stc_times.npy'),stc.times)

invdir = join(MAINDIR, 'inverse_ops')
rawdir = join(MAINDIR, 'preprocessed')
fsdir = join(MAINDIR, 'STRUCTURALS','FS_SUBDIR')
meg_f = join(rawdir, MEG_fname[{i}])
inv_op = join(invdir, f'{{RED_id[{i}]}}-inv.fif')
outdir = join(MAINDIR, 'raw_stcs')
method = 'MNE'
raw2source(meg_f, inv_op, method,outdir)
    """

    # save to file
    print(pycom, file=open(join(MAINDIR, 'cluster_scripts', f'{i}_aec.py'), 'w'))

    # construct csh file
    tcshf = f"""#!/bin/tcsh
            {pythonpath} {join(MAINDIR, 'cluster_scripts', f'{i}_aec.py')}
                    """
    # save to directory
    print(tcshf, file=open(join(MAINDIR, 'cluster_scripts', f'{i}_aec.csh'), 'w'))

    # execute this on the cluster
    os.system(f"sbatch --job-name=AmpEnvConnect_{i} --mincpus=2 --mem-per-cpu=40G -t 0-1:00 {join(MAINDIR, 'cluster_scripts', f'{i}_aec.csh')}")

#%% Read in a file
from scipy.signal import hilbert, resample
import scipy as ss
i = 10
outdir = join(MAINDIR, 'raw_stcs')
method = 'MNE'
data = np.load(join(outdir, f'{os.path.basename(MEG_fname[i]).split("_")[0]}_{method}_stc_data.npy'))
#%% Calculate the hilbert envelope for all the voxels and save downsampled version
downsample_factor = 100
#analytic_signal = np.zeros((data.shape[0], int(data.shape[1]/downsample_factor)))
analytic_signal = np.zeros(data.shape)
for i in range(data.shape[0]):
    tmp_sig = hilbert(data[i,:])
    #analytic_signal[i,:] = resample(tmp_sig, int(data.shape[1]/downsample_factor))
    analytic_signal[i,:] = tmp_sig
    print(i/data.shape[0])

#%% downsample
ii = 1
#plot some stuff
fig, ax = plt.subplots(1)

section = data[ii,0:4000]

section = mne.filter.filter_data(section,1000,freqs['Alpha'][0], freqs['Alpha'][1])
#get number of fft
n_fft = mne.filter.next_fast_len(len(section))
env = hilbert(section,N=n_fft, axis =-1)
d_env = resample(np.abs(env),int(len(env)/25))
d_sec = resample(section,int(len(section)/25))
#ax.plot(section[1000:5000])
#ax.plot(np.abs(env)[1000:5000])
#ax.plot(section)
ax.plot(d_sec)
ax.plot(d_env)
fig.savefig('/home/ai05/test.png')

#%% convert example into MNI space
i = 0

def mni_conv(i):
    inv_op = mne.minimum_norm.read_inverse_operator(join(invdir, f'{RED_id[i]}-inv.fif'))
    # get raw file
    raw = mne.io.read_raw_fif(join(rawdir, MEG_fname[i]), preload=False)
    events = mne.make_fixed_length_events(raw, duration=10.)
    epochs = mne.Epochs(raw, events=events, tmin=0, tmax=10.,
                        baseline=None, preload=True)
    del raw
    epochs = epochs[0:1]
    # invert the eopchs
    stcs = mne.minimum_norm.apply_inverse_epochs(epochs, inv_op,
                                                 lambda2=lambda2,
                                                 pick_ori=pick_ori,
                                                 method=method)
    stc = stcs[0]
    del stcs
    mni = mne.vertex_to_mni(stc.vertices, [0,1], MR_id[i], subjects_dir=fsdir) # mni space
    mni = np.vstack([mni[0], mni[1]]) # stack
    outdir = join(MAINDIR, 'mni_coords')
    np.save(join(outdir, f'{os.path.basename(MEG_fname[i]).split("_")[0]}_mni.npy'),mni)

joblib.Parallel(n_jobs=15)(
    joblib.delayed(mni_conv)(i) for i in range(len(MEG_fname)))


# Imports
import random
import time
import getpass

# Write animation
def write_fx(frase):
    for letra in frase:
        print(letra, end='', flush=True)
        time.sleep(0.010)

# game
def play(num1, num2, cpu_cs):

    empty = 0

    computer_wins = 0

    user_wins = 0

    rounds = 0

    while True:

        num2 = random.choice(cpu_cs)

        if num1 == num2:
            time.sleep(1)
            result = '\n    [*] Nadie gana'
            empty += 1
            rounds += 1

        elif num1 =='tijeras' and num2 == 'piedra' or num1 == 'papel' and num2 == 'tijeras' or num1 == 'piedra' and num2 == 'papel':
            time.sleep(1)
            result = '\n    [*] La maquina gana'
            computer_wins += 1
            rounds += 1

        elif num1 == 'piedra' and num2 == 'tijeras' or num1 == 'tijeras' and num2 == 'papel' or num1 == 'papel' and num2 == 'piedra':
            time.sleep(1)
            result = '\n    [*] El jugador gana'
            user_wins += 1
            rounds += 1

        if user_wins == 2:
            write_fx(f'''
    Resultado de Partida numero {rounds}

            {result}

    La seleccion de la maquina fue {num2}''')
            table('EL Usuario :D', rounds, user_wins, computer_wins, empty)
            break

        elif computer_wins == 2:
            write_fx(f'''
    Resultado de Partida numero {rounds}

            {result}

    La seleccion de la maquina fue {num2}''')
            table('La Maquina :D', rounds, user_wins, computer_wins, empty)
            break

        else:
            write_fx(f'''
    RESULTADO DE PARTIDA NUMERO {rounds}

            {result}

    La seleccion de la maquina fue {num2}''')

            write_fx(f'''

    vuelve a escoger entre

    [*] Piedra
    [*] Papel
    [*] Tijeras

    para la siguiente ronda: ''')

            num1 = input('').lower().replace(' ','')

            if num1 not in cpu_cs:
                write_fx('\n    Comando no valido')
                time.sleep(1)

                write_fx(f'''

    vuelve a escoger entre

    [*] Piedra
    [*] Papel
    [*] Tijeras

    para la siguiente ronda: ''')
                num1 = input('').lower().replace(' ','')
                continue

# User Interface
def table(nom, var1, var2, var3, var4):
    time.sleep(1)
    write_fx(f'''

     ________________________________________________________________
    | Partidas | ganadas por usuario | ganadas por maquina | Empates |
    |------------------------------------------------------|---------|
    |     {var1}    |         {var2}           |         {var3}           |    {var4}    |
     ----------------------------------------------------------------

    El ganador de la ronda es  {nom}

                         ██████╗   ██████╗
                        ██╔════╝  ██╔════╝
                        ██║░░██╗  ██║░░██╗
                        ██║░░╚██╗ ██║░░╚██╗
                        ╚██████╔╝ ╚██████╔╝
                         ╚═════╝   ╚═════╝

    ''')

def home():
    choices = ('piedra', 'papel', 'tijeras')

    write_fx ('''

    Bienvenido a piedra, papel o tijeras

    El juego es en partidas, el primero en ganar
    2 partidas gana la ronda.

    [*] Piedra
    [*] Papel
    [*] Tijeras

    Escribe cual va a escoger aca --> ''')

    user_cs = input('').lower().replace(' ','')

    comp_cs = random.choice(choices)

    if user_cs not in choices:
        write_fx('\n    Comando no valido')
        time.sleep(1)
        home()

    else:
        play(user_cs, comp_cs, choices)

        write_fx(f'\n\n    Preciona enter para volver a jugar')
        getpass.getpass(' ')
        home()

if __name__ == '__main__':
    home()


import os,sys,argparse
# import fileinput,time
import glob
import re,bisect
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib
# matplotlib.use('Agg')
import matplotlib.pyplot as plt
matplotlib.rcParams['font.size']=14
import seaborn as sns
sns.set(font_scale=1.)
sns.set_style("whitegrid", {'axes.grid' : False})
import scipy
import scipy.optimize
sns.set_style("ticks")
matplotlib.rcParams["font.sans-serif"] = ["Arial"]
from matplotlib import gridspec

def mark_pvalue(compr_pos,positions,box_vals):
    s,p = stats.ttest_ind(box_vals[compr_pos[0]],box_vals[compr_pos[1]],nan_policy='omit')
    y, h, col = np.percentile(np.append(box_vals[compr_pos[0]],box_vals[compr_pos[1]]),95)*.95 ,1.05, 'k'
    y2 = np.percentile(np.append(box_vals[compr_pos[0]],box_vals[compr_pos[1]]),5)*0.99
    x1,x2 = positions[compr_pos[0]],positions[compr_pos[1]]
    indicator = 'down' if s>0 else 'up'
    p_label='{} \n {:.1e}'.format(indicator,p)
    if p_label[-2]=='0':
        p_label = p_label[:-2]+p_label[-1]
    if p>=0.05:
        p_label = 'n.s.'
    if compr_pos[2] == 't':
        plt.plot([x1*1.03, x1*1.03, x2*0.97, x2*0.97], [y, y*h, y*h, y], lw=1, c=col)
        plt.text((x1+x2)*.5, y*h, p_label, ha='center', va='bottom', color=col,fontsize=12)
    else:
        plt.plot([x1*1.03, x1*1.03, x2*0.97, x2*0.97], [y2, y2*.91, y2*.91, y2], lw=1, c=col)
        plt.text((x1+x2)*.5, y2*.95, p_label, ha='center', va='top', color=col,fontsize=12)

outdir = 'f2_compr_statistics_box'
os.makedirs(outdir,exist_ok=True)

indirs = ['CP_TFBS_vs_TFMS','CP_TFBS_with_motif_vs_TFMS','CP_TFBS_without_motif_vs_TFMS']
group_info = {'CP':'log10-distance CP t-stats', 'SE':'fisher_exact_s'}
or_thre = {'CP':9,'SE':6}
label_name = {'CP':'log2 T-statistics','SE':'log2 Odds Ratio'}

xticklabels = ['All peaks','Peaks w/ motif','Peaks w/o motif']
positions = [0,1,2]
colors = ['grey','grey','grey']

for or_key in ['CP','SE'][:]:
    col = group_info[or_key]
    box_vals = []
    for indir in indirs[:]:
        infile = '../f2_TFMS_TFBS_CP/{}/per_TF_per_Celltype_{}.csv'.format(indir,indir)
        df = pd.read_csv(infile,index_col = 0)
        vals = np.log2(df[col].replace(0,np.nan)).dropna().values
        box_vals.append(vals)

    ## box with scatter
    plt.figure(figsize=(3,3))
    g = plt.boxplot(box_vals,positions=positions,widths = .6,patch_artist=True,\
                boxprops=dict(color='k',facecolor='w',fill=None,lw=1),\
                medianprops=dict(color='grey'),showfliers=False)

    for patch, color in zip(g['boxes'], colors):
        patch.set_facecolor(color)

    # scatter_X = []
    # for position_id in np.arange(len(positions)):
    #     scatter_x = np.random.normal(positions[position_id],0.07,len(box_vals[position_id]))
    #     plt.scatter(scatter_x,box_vals[position_id],color=colors[position_id],s=20,zorder=0,alpha=0.99,rasterized=True)

    # positions=[0,1,2]
    for compr_pos in [[0,1,'b'], [1,2,'b'],[0,2,'t']]:
        mark_pvalue(compr_pos,positions,box_vals)

    plt.axes().set_xticklabels(xticklabels,rotation=30, ha='right',fontsize=16,color='k')
    plt.ylabel(label_name[or_key],fontsize=16)
    plt.title(or_key)
    plt.savefig('{}/{}.pdf'.format(outdir,or_key),bbox_inches='tight',pad_inches=0.02,transparent=True)
    plt.show()
    plt.close()


from dash import html, dcc, Input, Output, State, html
import dash_bootstrap_components as dbc
from dash import page_registry, page_container

dropdown_ninez_english = dbc.Row([
    dbc.Col(
        dbc.DropdownMenu(
        children=[
            dbc.DropdownMenuItem("Schooling", header=True),
            dbc.DropdownMenuItem("Pre-primary enrollment rate (5 Year old)", href="/ninez_english/enrollment-rate-pre-primary-5"),
            dbc.DropdownMenuItem("Pre-primary enrollment rate (3-5 Year old)", href="/ninez_english/enrollment-rate-pre-primary-35"),
            dbc.DropdownMenuItem("Primary net enrollment rate", href="/ninez_english/net-enrollment-primary"),
            dbc.DropdownMenuItem("Secondary net enrollment rate", href="/ninez_english/net-enrollment-secondary"),
            dbc.DropdownMenuItem("Tertiary net enrollment rate", href="/ninez_english/net-enrollment-tertiary"),
            dbc.DropdownMenuItem("Primary completion rate", href="/ninez_english/primary-completion-rate"),
            dbc.DropdownMenuItem("Secondary completion rate", href="/ninez_english/secondary-completion-rate"),
            dbc.DropdownMenuItem(divider=True),
            dbc.DropdownMenuItem("Activities and time use", header=True),
            dbc.DropdownMenuItem("Participation in care activities", href="/ninez_english/participation-care-activities"),
            dbc.DropdownMenuItem("Weekly hours allocated to care activities", href="/ninez_english/hours-care-activities"),
            dbc.DropdownMenuItem("Participation in childcare activities", href="/ninez_english/participation-childcare-activities"),
            dbc.DropdownMenuItem("Weekly hours allocated to childcare activities", href="/ninez_english/hours-childcare-activities"),
            dbc.DropdownMenuItem("Youth neither in school nor economically active", href="/ninez_english/youth-neither-school-active"),
            dbc.DropdownMenuItem(divider=True),
            dbc.DropdownMenuItem("Test Scores", header=True),
            dbc.DropdownMenuItem("Average score in standardized tests for 3rd grade students", href="/ninez_english/score-3rd"),
            dbc.DropdownMenuItem("Average score in standardized tests for 6th grade students", href="/ninez_english/score-6th"),
            dbc.DropdownMenuItem("Average score in standardized tests for 15 year-old students", href="/ninez_english/score-15"),
            dbc.DropdownMenuItem("Ratio of standardized tests scores for 3rd grade students", href="/ninez_english/ratio-3rd-english"),
            dbc.DropdownMenuItem("Ratio of standardized tests scores for 6th grade students", href="/ninez_english/ratio-6th-english"),
            dbc.DropdownMenuItem("Ratio of standardized tests scores for 15 year-old students", href="/ninez_english/ratio-15-english"),
            dbc.DropdownMenuItem("Percentage of women among the bottom 10% test scores in 3rd grade", href="/ninez_english/women-10-low-3rd"),
            dbc.DropdownMenuItem("Percentage of women among the bottom 10% test scores in 6th grade", href="/ninez_english/women-10-low-6th"),
            dbc.DropdownMenuItem("Percentage of women among the bottom 10% test scores for 15 year-old students", href="/ninez_english/women-10-low-15"),
            dbc.DropdownMenuItem("Percentage of women among the top 10% test scores in 3rd grade", href="/ninez_english/women-10-high-3rd"),
            dbc.DropdownMenuItem("Percentage of women among the top 10% test scores in 6th grade", href="/ninez_english/women-10-high-6th"),
            dbc.DropdownMenuItem("Percentage of women among the top 10% test scores for 15 year-old students", href="/ninez_english/women-10-high-15"),
            dbc.DropdownMenuItem("Percentage of women among 15 year-old functionally-illiterate students", href="/ninez_english/women-illiterate-functionally"),
        ],
        size="lg",
        nav=True,
        in_navbar=True,
        label="Indicators",
        className="ms-0",
        toggle_style={"color": "#460074"},
        align_end=False,
        style={'width':'100%'}

        )
    )
],
className="g-0 ms-auto flex-nowrap mt-5 mt-md-0",
align="center",
)

navbar_ninez_english = dbc.Navbar(
    dbc.Container(
        [
            html.A(
                # Use row and col to control vertical alignment of logo / brand
                dbc.Row(
                    [
                        dbc.NavbarToggler(id="navbar-toggler_ninez_english", n_clicks=0),
                        dbc.Collapse(
                            dropdown_ninez_english,
                            className="ml-auto",
                            id="navbar-collapse_ninez_english",
                            is_open=False,
                            navbar=True,
                        ),
                    ],
                    align="center",
                    className="g-0",
                ),
                href="/ninez_english/enrollment-rate-pre-primary-5",
                style={"textDecoration": "none"},
            ),
        ],
    fluid=True),
    #outline=True,
    color="light",
    dark=True,
    expand=True,
)

def generate_dropdown(options):
    dropdown = dcc.Dropdown(
        options=[{'label': option, 'value': option} for option in options],
        value="Argentina",
        id="all-pages-dropdown-pais-ninez-english",
        persistence=True,
        persistence_type = 'memory',
        multi=True,
        className="bg-light",
    )
    return dropdown

# Armar loop:
mark_values = {2000:'2000',2001:'2001',2002:'2002',
                2003:'2003',2004:'2004',2005:'2005',
                2006:'2006',2007:'2007',2008:'2008',
                2009:'2009',2010:'2010',2011:'2011',
                2012:'2012',2015:'2015',2016:'2016',
                2013:'2013',2014:'2014',2015:'2015',
                2016:'2016',2017:'2017',2018:'2018',
                2019:'2019',2020:'2020',2021:'2021'}

ranger_slider_year_ninez_english = dcc.RangeSlider(
        min=2000,
        max=2021,
        value=[2000,2021],
        marks=mark_values,
        step=1,
        persistence=True,
        persistence_type = 'memory',
        id="all-pages-ranger-slider-year-ninez-english"
)


from sympy_old import *
import numpy as np

KnownVar = 5
x = symbols('x')
y = symbols('y')
eq1 = Eq(5*x + 5*y - 7, 0)
eq2 = Eq(6*x - 4*y - 9, 0)
solution = solve((eq1,eq2),x,y)
print(solution)
x = solution[x]
print(N(x))
print(np.sin())


import os
import sys
import inspect
# insert env dir into sys
# env must contain env folder:
currentdir = os.path.dirname(os.path
                             .abspath(inspect.getfile(inspect.currentframe())))
env = currentdir.find("env")
env_dir = currentdir[:env]
# print(env_dir)
if env_dir not in sys.path:
    sys.path.insert(0, env_dir)

from env.equation.data.terms.output.cpp.additions.someFuncs import NewtonBinomCoefficient

import logging

# create logger
log_level = logging.INFO  # logging.DEBUG
logging.basicConfig(level=log_level)
logger = logging.getLogger('gen_pure_common.py')
logger.setLevel(level=log_level)

class GenPureCommon():
    '''
    DESCRIPTION:

    Find d^{n}(u)/d(x)^{n}
     where n is
      n > 1 for central functions
            (used method common_diff)

    INPUT:

    See ``self.__init__``

    '''
    def __init__(self, blockNumber, unknownVarIndex,
                 indepVar, indepVarOrders):

        '''
        Inputs:

        - ``blockNumber`` -- used for strides and cellsize
        variables names

        - ``unknownVarIndex`` -- shift index for variable
        (like (U, V)-> (source[+0], source[+1]))
        (Ex: dict([('U', 0), ('V', 1)])['U'])

        - ``indepVar`` -- like 'x' i.e. for which diff maked

        - ``indepVarOrders`` -- dict([(var, float(order))])

        '''

        # PARAMS FOR ALL
        self.blockNumber = blockNumber

        # shift index for variable like
        # like (U,V)-> (source[+0], source[+1])
        self.unknownVarIndex = unknownVarIndex

        # like ['x'] i.e. for which diff maked
        # see in begining of callDerivGenerator
        # TODO use 'x' instead:
        self.indepVar = indepVar
        x = self.indepVar

        logger.debug("indepVarOrders:")
        logger.debug(indepVarOrders)

        self.derivOrder = int(indepVarOrders[x])

        # like ['x', 'y', 'z']
        self.userIndepVariables = ['x', 'y', 'z']

        # for method choosing for special
        var = self.indepVar
        self.indepVarIndexList = [self.userIndepVariables.index(var)]

        # for all
        self.make_general_data()
        # END FOR ALL

    def make_general_data(self):
        '''
        DESCRIPTION:
        DXM1 - means dx
        DXM2 - means dx^2
        '''
        self.increment = ('D' + self.indepVar.upper()
                          + 'M' + str(self.derivOrder))
        self.specialIncrement = 'D' + self.indepVar.upper()
        self.stride = ('Block' + str(self.blockNumber)
                       + 'Stride' + self.indepVar.upper())

    def createIndicesList(self):
        '''
        DESCRIPTION:

        Т.к. для CentralFunction умеем генерировать аппроксимации
        производных любого порядка, то эти аппроксимации содержат
        много слагаемых, каждое из которых имеет свой индекс.

        EXAMPLES:

        For derivOrder = 1
        [' + 0', '-1']

        For derivOrder = 2
        [' + 1', ' + 0', '-1']

        For derivOrder = 3
        [' + 1', ' + 0', '-1', '-2']

        USED FUNCTIONS::

            self.derivOrder
        '''
        leftIndex = self.derivOrder // 2
        rightIndex = -(self.derivOrder - leftIndex)
        reverseList = [i for i in range(rightIndex, leftIndex + 1)]
        comfortableList = reverseList[::-1]
        indicesListAsString = []
        for index in comfortableList:
            if index >= 0:
                indicesListAsString.extend([' + ' + str(int(index))])
            else:
                indicesListAsString.extend([str(int(index))])
        return indicesListAsString

    def createCoefficientList(self):
        '''
        DESCRIPTION:

        Т.к. для CentralFunction умеем генерировать аппроксимации
        производных любого порядка, то эти аппроксимации содержат
        много слагаемых, перед каждым из которых имеется свой
        коэффициент.

        Create Newton binom coefficients

        self.derivOrder
        '''
        numberList = [NewtonBinomCoefficient(self.derivOrder, k)
                      for k in range(0, self.derivOrder + 1)]
        stringList = []
        for number in numberList:
            stringList.extend([str(number)])
        return stringList

    def common_diff(self):
        '''
        DESCRIPTION:

        generate cpp derivative for central function
        or for border in case of non border variable
        (for example: for y at border x=0)

        for ``derivOrder = 1``:

        .. math::  du/dx = (u_{i+1}-u_{i-1})/(2 dx)

        for ``derivOrder = 2``:

        .. math:: ddu/ddx = (u_{i+1}-2*u_{i}+u_{i-1})/(dx^2)

        Template:

        source[idx  # point in that derive will find
        +  stride  * Block0CELLSIZE  # +1 to some of {x,y,z} direction
                                     # (defined by stride)
                                     # ('x': +1,
                                     #  'y': + Block0StrideY*Block0CELLSIZE,
                                     #  'z': + Block0StrideZ*Block0CELLSIZE
                                     #         = Block0SizeY*Block0CountY*Block0CELLSIZE)
        +  $ unknownVarIndex[0] $ ]  # shift, differ for each variable ('U': +0, 'V': +1)

        USED FUNCTIONS:

        str self.blockNumber
        self.delay
        str self.unknownVarIndex

        self.createIndicesList
        self.createCoefficientList

        '''
        try:
            increment = self.increment
            stride = self.stride
        except:
            raise(SyntaxError("increment or stride are not defined"
                              + "\n use make_general_data first"))

        if self.derivOrder == 1:
            toLeft = ('source['+'delay'+'][idx - '
                      + stride + ' * ' + 'Block'
                      + str(self.blockNumber) + 'CELLSIZE + '
                      + str(self.unknownVarIndex) + ']')
            toRight = ('source['+'delay'+'][idx + '
                       + stride + ' * ' + 'Block'
                       + str(self.blockNumber) + 'CELLSIZE + '
                       + str(self.unknownVarIndex) + ']')
            return('0.5 * ' + increment + ' * '
                   + '(' + toRight + ' - ' + toLeft + ')')
        else:
            indicesList = self.createIndicesList()
            coefficientList = self.createCoefficientList()
            finiteDifference = ''
            for i, index in enumerate(indicesList):
                m1 = i % 2
                m2 = (i + 1) % 2
                m3 = i > 0
                m4 = coefficientList[i] != '1.0'
                startOfLine = (finiteDifference
                               + m1 * ' - ' + m2 * m3 * ' + '
                               + m4 * (str(coefficientList[i])
                                       + ' * '))
                restOfLine = ('source['+'delay'+'][idx' + str(index)
                              + ' * ' + stride + ' * ' + 'Block'
                              + str(self.blockNumber) + 'CELLSIZE + '
                              + str(self.unknownVarIndex) + ']')
                finiteDifference = startOfLine + restOfLine
            return '(' + increment + ' * ' + '(' + finiteDifference + ')' + ')'


def rows_after(self):
        """The rows that consume meshes from this row.

        :rtype: list
        :return: a list of rows that consume meshes from this row. Each row
          occurs only once. They are sorted by the first occurrence in the
          instructions.
        """
        rows_after = []
        for mesh in self.produced_meshes:
            if mesh.is_consumed():
                row = mesh.consuming_row
                if rows_after not in rows_after:
                    rows_after.append(row)
        return rows_after


import os
import sys

sum=0
path1=os.getcwd()
print(path1)
for item in os.scandir(path1):
    if item.is_file():
        sum+=os.path.getsize(item)
print(str(round(sum*(10**-9),3))+' GB')


from pandas import DataFrame
from pandas import read_csv
from smtplib import SMTP
from smtplib import SMTPConnectError
from smtplib import SMTPServerDisconnected
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from pretty_html_table import build_table
from socket import gaierror
from io import StringIO
from sys import exc_info
from log import Log

class SendMail():
    def __init__(self):
        pass

    def set_config(self, cfg_mail, cfg_smtp):

        # return [style, sender_email, receiver_email]
        self.style = cfg_mail[0]
        self.sender_email = cfg_mail[1]
        self.receiver_email1 = cfg_mail[2]

        # return [smtp_server, port]
        self.smtp_server = cfg_smtp[0]
        self.port = cfg_smtp[1]

    def set_content(self, content):

        style = self.style
        content_out_html_final = ""

        for sheet in content:
            sheet = StringIO(sheet)
            content_out = read_csv(sheet, sep=",")
            content_out = DataFrame(content_out)

            if(content_out.empty != True):
                content_out_text = content_out.to_string()
                content_out_text = content_out_text.replace("NaN", "Brak wpisu")

                content_out_html = build_table(content_out, style)
                content_out_html_final = content_out_html_final + content_out_html + "\n
\n"

        self.text = f"""\
                Zajrzyj do pliku excel z dostępami. Zaraz jakieś wygasną !

                {content_out_text}

                ----------------------------
                Powered by Majster
                """

        self.html = f"""\

                    Zajrzyj do pliku excel z dostępami.

                        Zaraz jakieś wygasną !

                {content_out_html_final}

----------------------------

                Powered by Majster
                """

    def send_mail(self, temat):

        log_error = Log().log_error

        sender_email = self.sender_email
        receiver_email1 = self.receiver_email1
        text = self.text
        html = self.html
        smtp_server = self.smtp_server
        port = self.port

        message = MIMEMultipart("alternative")
        message["Subject"] = temat
        message["From"] = sender_email

        part1 = MIMEText(text, "plain")
        part2 = MIMEText(html, "html")

        message.attach(part1)
        message.attach(part2)

        try:
            server = SMTP(smtp_server, port)
            server.ehlo_or_helo_if_needed()

            for receiver_email in receiver_email1:
                message["To"] = receiver_email
                server.sendmail(sender_email, receiver_email, message.as_string())
                receiver_email = None

            server.close()
            return True

        except SMTPConnectError as err:
            msg = "Błąd wysyłania maila. SMTP Connection Error."
            print(msg)
            log_error(err)
            return False

        except SMTPServerDisconnected as err:
            msg = "Błąd wysyłania maila. SMTP Auth Error."
            print(msg)
            log_error(f"SMTP Error: {err}\nSprawdź dane wysyłającego")
            return False

        except TimeoutError as err:
            msg = "Błąd wysyłania maila. Timeout Error."
            print(msg)
            log_error(f"SMTP Error: {err}\nSprawdź port. Czy ruch jest otwarty?\n")
            return False

        except gaierror as err:
            msg = "Błąd wysyłania maila. SMTP Address Error."
            print(msg)
            log_error(f"SMTP Error: {err}\nSprawdź nazwę serwera SMTP w configu")
            return False

        except:
            print(exc_info()[0])
            log_error(exc_info()[0])
            return False

    def __del__(self):
        pass