import numpy as np
import time
import PyraLNet as pyral
import matplotlib.pyplot as plt
import Dataset
import fcntl
from pathlib import Path
import mnist

dtype = pyral.dtype

class Layer(pyral.Layer):

    def update(self, r_in, u_next, dir_up):
        '''
        compute pyramidial and interneuron potentials by means of steady-state solution.
        If dir_up, compute pyramidials first, else interneurons first.
        Parameters
        ----------
        r_in:       input rates from previous layer
        u_next:     soma potentials of paramidals from next layer (may be None)
        dir_up:     direction of propagation (determines order in which pyramidals and internneurons are updated)
        '''
        #### input rates
        r_pyr = np.zeros(self.u_pyr["soma"].shape[0] + self.bias, dtype=dtype)
        r_in_buf = np.zeros(len(r_in) + self.bias, dtype=dtype)
        r_pyr[: len(self.u_pyr["soma"])] = self.act(self.u_pyr["soma"])
        r_in_buf[: len(r_in)] = r_in
        if self.bias:
            r_in_buf[-1] = self.bias_val
            r_pyr[-1] = self.bias_val

        #### compute potentials
        # if upwards pass compute pyramidials first, if downwards pass compute laterals first
        if dir_up:
            self.update_pyr(r_in_buf, u_next)
            r_pyr[: len(self.u_pyr["soma"])] = self.act(self.u_pyr["soma"])
            if self.bias:
                r_pyr[-1] = self.bias_val
            self.update_inn(r_pyr, u_next)
        else:
            self.update_inn(r_pyr, u_next)
            self.update_pyr(r_in_buf, u_next)

    def apply(self):
        pass

    def update_weights(self, r_in, n_exposures, reset_deltas=False):
        '''
        Update weights for one timestep dT = t_pattern/n_exposures.
        Parameters
        ----------
        r_in:           input rates from previous layer
        n_exposures:    number of weight updates per pattern -> timestep dT = t_pattern / n_exposures
        reset_deltas:   reset Deltas (weight update matrices) before update
        '''
        # input rates
        r_pyr = np.zeros(self.u_pyr["soma"].shape[0] + self.bias, dtype=dtype)
        r_in_buf = np.zeros(len(r_in) + self.bias, dtype=dtype)
        r_pyr[: len(self.u_pyr["soma"])] = self.act(self.u_pyr["soma"])
        r_in_buf[: len(r_in)] = r_in
        if self.bias:
            r_in_buf[-1] = self.bias_val
            r_pyr[-1] = self.bias_val

        #discretised lowpass
        dT = (self.params["t_pattern"] - self.params["learning_lag"])/n_exposures
        a = dT/(self.params["tau_w"]+dT)
        if reset_deltas:
            self.Delta_up.fill(0)
            self.Delta_ip.fill(0)
            self.Delta_pi.fill(0)
        self.Delta_up = self.Delta_up * (1 - a) + a * np.outer(
            self.act(self.u_pyr["soma"]) - self.act(self.gb / (self.gl + self.gb + self.ga) * self.u_pyr["basal"]), r_in_buf)
        self.Delta_ip = self.Delta_ip * (1 - a) + a * np.outer(
            self.act(self.u_inn["soma"]) - self.act(self.gd / (self.gl + self.gd) * self.u_inn["dendrite"]), r_pyr)
        self.Delta_pi = self.Delta_pi * (1 - a) + a * np.outer(-self.u_pyr["apical"], self.act(self.u_inn["soma"]))

        self.W_up += dT * self.eta["up"] * self.Delta_up
        self.W_ip += dT * self.eta["ip"] * self.Delta_ip
        self.W_pi += dT * self.eta["pi"] * self.Delta_pi

    def update_pyr(self, r_in, u_next):
        ####compute pyramidial potentials
        self.u_pyr["basal"][:] = np.matmul(self.W_up, r_in)  # [:] to enforce rhs to be copied to lhs
        self.u_pyr["soma"][:] = self.gb / (self.gl + self.gb + self.ga) * self.u_pyr["basal"]
        if u_next is not None:
            r_inn = self.act(self.u_inn["soma"])
            r_next = self.act(u_next)
            self.u_pyr["apical"][:] = np.matmul(self.W_down, r_next) + np.matmul(self.W_pi, r_inn)
            self.u_pyr["soma"][:] += self.ga / (self.gl + self.gb + self.ga) * self.u_pyr["apical"]

    def update_inn(self, r_pyr, u_next):
        ####compute interneuron potentials
        self.u_inn["dendrite"][:] = np.matmul(self.W_ip, r_pyr)
        self.u_inn["soma"][:] = self.gd / (self.gl + self.gd) * self.u_inn["dendrite"]
        if u_next is not None:
            l = self.gsom / (self.gl + self.gd + self.gsom)
            self.u_inn["soma"][:] = (1 - l) * self.gd / (self.gl + self.gd) * self.u_inn["dendrite"] + l * u_next

class OutputLayer(pyral.OutputLayer):
    '''
    Output layer -> see Layer for more info about the methods.
    '''
    def update(self, r_in, u_target):
        ### input rates
        r_in_buf = np.zeros(len(r_in) + self.bias, dtype=dtype)
        if self.bias:
            r_in_buf[:-1] = r_in
            r_in_buf[-1] = self.bias_val
        else:
            r_in_buf = r_in

        ### compute potentials
        self.u_pyr["basal"][:] = np.matmul(self.W_up, r_in_buf)  # [:] to enforce rhs to be copied to lhs
        self.u_pyr["soma"][:] = self.gb / (self.gl + self.gb) * self.u_pyr["basal"]
        if u_target is not None:
            l = self.gsom / (self.gl + self.gb + self.gsom)
            self.u_pyr["soma"][:] = (1 - l) * self.gb / (self.gl + self.gb) * self.u_pyr["basal"] + l * u_target

    def update_weights(self, r_in, n_exposures, reset_deltas=False):
        # input rates
        r_in_buf = np.zeros(len(r_in) + self.bias, dtype=dtype)
        if self.bias:
            r_in_buf[:-1] = r_in
            r_in_buf[-1] = self.bias_val
        else:
            r_in_buf = r_in

        # discretised lowpass
        dT = (self.params["t_pattern"] - self.params["learning_lag"]) / n_exposures
        a = dT / (self.params["tau_w"] + dT)
        if reset_deltas:
            self.Delta_up.fill(0)
        self.Delta_up = self.Delta_up * (1 - a) + a * np.outer(
            self.act(self.u_pyr["soma"]) - self.act(self.gb / (self.gl + self.gb) * self.u_pyr["basal"]),
            r_in_buf)

        self.W_up += dT * self.eta["up"] * self.Delta_up

    def apply(self):
        pass

class Net(pyral.Net):
    '''
    Steady-State approximation of PyraLNet. Receives the *same* parameters as PyraLNet! (for comparison also seemingly
    unused params like learning-lag should be given, as the shorter learning-time will be accounted for)
    '''

    def __init__(self, params, act=None, seed=None):
        if seed is not None:
            np.random.seed(seed)
        self.params = params
        self.layer = []
        if act is None:
            self.act = pyral.soft_relu
        else:
            self.act = act
        dims = params["dims"]
        self.dims = dims
        bias = params["bias"]["on"]
        bias_val = params["bias"]["val"]
        eta = {}
        for n in range(1, len(dims) - 1):
            eta["up"] = params["eta"]["up"][n - 1]
            eta["pi"] = params["eta"]["pi"][n - 1]
            eta["ip"] = params["eta"]["ip"][n - 1]
            self.layer += [Layer(dims[n], dims[n - 1], dims[n + 1], eta, params, self.act, bias, bias_val)]
        eta["up"] = params["eta"]["up"][-1]
        eta["pi"] = params["eta"]["pi"][-1]
        eta["ip"] = params["eta"]["ip"][-1]
        self.layer += [OutputLayer(dims[-1], dims[-2], eta, params, self.act, bias, bias_val)]
        print("feedback-couplings: lambda_out = %f, lambda_inter = %f, lambda_hidden = %f"
              % (params["gsom"] / (params["gl"] + params["gb"] + params["gsom"]),
                 params["gsom"] / (params["gl"] + params["gd"] + params["gsom"]),
                 params["ga"] / (params["gl"] + params["gb"] + params["ga"])))

    def update(self, r_in, u_target, n_passes=2, n_exposures=10, learning_on=True, records=None):
        '''
        Compute updates from one pattern (r_in, u_trgt). The pattern is presented n_exposures time. After each
        such step the potentials are updated by means of the steady-state approximation and then the weights are updated
        with timestep dT = t_pattern / n_exposures.
        Parameters
        ----------
        r_in:           input rate vector
        u_target:       target potential vector (may be None)
        n_passes:       number of up and downwards-passing cycles to compute potentials (see report)
        n_exposures:    number of weight updates -> weight update time step is dT = t_pattern / n_exposures
        learning_on:    switch learning off or on
        records:        list of quantities to record (only once per pattern after all n_exposures updates)
                        If provided, a list for each layer, starting from the hidden layer,
                        is expected, eg. [["pyr_soma", "pyr_basal"], ["pyr_apical", "inn_soma", "inn_dendrite"], ...].
                        See implementation of run for which quantities are available
        '''
        for k in range(n_exposures):
            # update potentials
            for i in range(n_passes):
                self.layer[0].update(r_in, None if i==0 else self.layer[1].u_pyr["soma"], dir_up=True)
                for n in range(1, len(self.layer) - 1):
                    self.layer[n].update(self.act(self.layer[n - 1].u_pyr["soma"]), None if i==0 else self.layer[n + 1].u_pyr["soma"], dir_up=True)
                self.layer[-1].update(self.act(self.layer[-2].u_pyr["soma"]), u_target)
                for n in range(len(self.layer) - 1, 1):
                    self.layer[n].update(self.act(self.layer[n - 1].u_pyr["soma"]), self.layer[n + 1].u_pyr["soma"], dir_up=False)
                self.layer[0].update(r_in, self.layer[1].u_pyr["soma"], dir_up=False)

            # update weights
            if learning_on:
                self.layer[0].update_weights(r_in, n_exposures=n_exposures, reset_deltas=(self.params["reset_deltas"] and k==0))
                for n in range(1, len(self.layer)):
                    self.layer[n].update_weights(self.act(self.layer[n - 1].u_pyr["soma"]), n_exposures=n_exposures)

        for i, layer in enumerate(self.layer):
            if records is not None and not records == []:
                for _, r in records[i].items():
                    r.record()

    def run(self, in_seq, trgt_seq=None, reset_weights=False, val_len=0, n_passes=2, n_exposures=10, metric=None,
            rec_quants=None, rec_dn=1, learning_off=False, info_update=100):
        '''
        Run a full simulation for the whole sequence of input patterns in in_seq.
        Nudging can be switched on or off per pattern.
        If val_len > 0 and trgt_seq is not None, target patterns with disabled nudging are used for validation.
        Parameters
        ----------
        in_seq:         sequence of input patterns. Expects N x dim_0 (input layer size) array.
        trgt_seq:       sequence of target potentials (might be None). Expects N x (dim_out+1) array. Last column has
                        to take values in {0,1}, determining if nudging is switched on (1) or off (0) for each pattern.
        reset_weights:  randomly reset weight connections before starting (soma potentials are always reset to zero)?
        val_len:        if val_len > 0 and trgt_seq is not None all patterns for which trgt_seq[:,-1] is Zero are used
                        for validation. In this case, chunks of length val_len for which trgt_seq[:,-1]==0 are expected.
                        During validation learning and noise is always disabled.
        n_passes:       see SteadNet.Net.update
        n_exposures:    see SteadNet.Net.update
        metric:         metric used during validation
        rec_quants:     list of all quantities to track. If provided, a list for each layer, starting from the hidden layer,
                        is expected, eg. [["pyr_soma", "pyr_basal"], ["pyr_apical", "inn_soma", "inn_dendrite"], ...].
        rec_dn:         specifies resolution for tracking of rec_quants (number of patterns to average over)
        learning_off:   switch learning off
        info_update:    print info about remaining time after each info_update patterns

        Returns
        -------
        [(recorded quantities; if rec_quants!=None),
         sequence of outputs (averaged soma potentials of output layer) with size N x dim_out,
         (N x 2 array containing the validation results [output layer MSE, metric]; if val_len>0)]
        '''

        #### prepare run
        print("each input pattern is presented %d times -> dT/tau_w: %.3f"%(n_exposures, (self.params["t_pattern"] - self.params["learning_lag"]) /(self.params["tau_w"] * n_exposures)))
        print("effective learning rate multiplier: %f"%((self.params["t_pattern"] - self.params["learning_lag"]) / n_exposures))

        rec_len = int(np.ceil(len(in_seq)/ rec_dn))
        compress_len = rec_dn
        records = []

        # sequence of outputs generated by the network
        out_seq = np.zeros((len(in_seq), self.params["dims"][-1]), dtype=dtype)

        #store validation results
        if val_len > 0:
            val_res = []  # [mse of val result, metric of val result]

        if trgt_seq is not None:
            if len(trgt_seq) != len(in_seq):
                raise Exception("input and target sequence mismatch")

        # reset/initialize and add trackers for potentials that should be recorded
        for i in range(len(self.layer)):
            l = self.layer[i]
            l.reset(reset_weights)
            if rec_quants is None:
                continue
            rp = rec_quants[i]
            rcs = {}
            if "pyr_soma" in rp:
                rcs["pyr_soma"] = pyral.Tracker(rec_len, l.u_pyr["soma"], compress_len)
            if "pyr_basal" in rp:
                rcs["pyr_basal"] = pyral.Tracker(rec_len, l.u_pyr["basal"], compress_len)
            if "pyr_apical" in rp:
                rcs["pyr_apical"] = pyral.Tracker(rec_len, l.u_pyr["apical"], compress_len)
            if "inn_dendrite" in rp:
                rcs["inn_dendrite"] = pyral.Tracker(rec_len, l.u_inn["dendrite"], compress_len)
            if "inn_soma" in rp:
                rcs["inn_soma"] = pyral.Tracker(rec_len, l.u_inn["soma"], compress_len)
            if "W_up" in rp:
                rcs["W_up"] = pyral.Tracker(rec_len, l.W_up, compress_len)
            if "W_down" in rp:
                rcs["W_down"] = pyral.Tracker(rec_len, l.W_down, compress_len)
            if "W_ip" in rp:
                rcs["W_ip"] = pyral.Tracker(rec_len, l.W_ip, compress_len)
            if "W_pi" in rp:
                rcs["W_pi"] = pyral.Tracker(rec_len, l.W_pi, compress_len)
            records += [rcs]

        ####simulate

        start = time.time()
        val_idx = -1
        for i in range(len(in_seq)):
            nudging_on = trgt_seq[i, -1] if trgt_seq is not None else False
            if not nudging_on and val_len > 0:
                # this patterns is for validation! (val_idx will be >=0 during validation)
                val_idx += 1
            if trgt_seq is not None:
                u_trgt = trgt_seq[i, :-1]
                self.update(in_seq[i], u_trgt if nudging_on else None, n_passes, n_exposures,
                            learning_on=not learning_off and val_idx<0, records=records)
                out_seq[i] = self.layer[-1].u_pyr["soma"]
            else:
                self.update(in_seq[i], None, n_passes, n_exposures, learning_on=not learning_off, records=records)
                out_seq[i] = self.layer[-1].u_pyr["soma"]

            # print validation results if finished
            if val_idx >= 0 and val_idx == val_len - 1:
                print("---Validating on %d patterns---" % (val_len))
                pred = out_seq[i - val_len + 1:i + 1]
                true = trgt_seq[i - val_len + 1:i + 1, :-1]
                mse = np.mean((pred - true) ** 2)
                print("mean squared error: %f" % (mse))
                vres = [mse, 0]
                if metric is not None:
                    name, mres = metric(pred, true)
                    print("%s: %f" % (name, mres))
                    vres[1] = mres
                val_res += [vres]
                val_idx = -1 # disable validation mode again

            # print some info
            if i > 0 and i % info_update == 0:
                print("%d/%d input patterns done. About %s left." % (i, len(in_seq), pyral.time_str((len(in_seq) - i - 1) * (time.time() - start) / info_update)))
                start = time.time()

        # finalize recordings
        for rcs in records:
            for _, r in rcs.items(): r.finalize()

        ret = []
        if rec_quants is not None:
            ret += [records]
        ret += [out_seq]
        if val_len > 0:
            ret += [np.array(val_res, dtype=dtype)]
        return tuple(ret) if len(ret) > 1 else ret[0]

    def train(self, X_train, Y_train, X_val, Y_val, n_epochs, val_len, n_out, classify, u_high=1.0,
              u_low=0.1, n_passes=2, n_exposures=10, rec_quants=None, rec_dn=1, vals_per_epoch=1, reset_weights=False,
              info_update=1000, metric=None):
        '''
        see PyraLNet.Net.train and SteadNet.Net.run (for the new params)
        '''

        assert len(X_train) > vals_per_epoch
        assert len(X_train) == len(Y_train)
        assert len(X_val) == len(Y_val)

        n_features = X_train.shape[1]

        assert n_features == X_val.shape[1]
        assert n_features == self.dims[0]
        assert n_out == self.dims[-1]

        if classify:
            assert len(Y_train.shape) == 1
            assert len(Y_val.shape) == 1
        else:
            assert (len(Y_train.shape) == 1 and n_out == 1) or Y_train.shape[1] == n_out
            assert (len(Y_val.shape) == 1 and n_out == 1) or Y_val.shape[1] == n_out

        len_split_train = round(len(X_train) / vals_per_epoch)  # validation after each split
        vals_per_epoch = len(X_train) // len_split_train
        print("%d validations per epoch" % (vals_per_epoch))

        len_per_ep = vals_per_epoch * val_len + len(X_train)
        length = len_per_ep * n_epochs

        r_in_seq = np.zeros((length, n_features))
        val_res = np.zeros((vals_per_epoch * n_epochs,
                            3),
                           dtype=dtype)  # [number of training patterns seen, mse of val result, metric of val result]

        if classify:
            target_seq = np.ones((length, n_out), dtype=dtype) * u_low
        else:
            target_seq = np.zeros((length, n_out), dtype=dtype)

        nudging_on = np.ones((length, 1), dtype=dtype)
        val_idc = np.zeros((len(val_res), val_len))  # indices of validation patterns

        for n in range(n_epochs):
            perm_train = np.random.permutation(len(X_train))
            left = n * len_per_ep
            left_tr = 0
            for k in range(vals_per_epoch):
                if k == vals_per_epoch - 1:
                    right_tr = len(X_train)
                else:
                    right_tr = left_tr + len_split_train
                right = left + right_tr - left_tr
                r_in_seq[left: right] = X_train[perm_train[left_tr:right_tr]]
                if classify:
                    target_seq[np.arange(left, right), 1 * Y_train[
                        perm_train[left_tr:right_tr]]] = u_high  # enforce Y_train is an integer array!
                else:
                    target_seq[left:right] = Y_train[perm_train[left_tr:right_tr]]
                perm_val = np.random.permutation(len(X_val))[:val_len]
                left = right
                right = left + val_len
                r_in_seq[left: right] = X_val[perm_val]
                if classify:
                    target_seq[
                        np.arange(left, right), 1 * Y_val[perm_val]] = u_high  # enforce Y_val is an integer array!
                else:
                    target_seq[left:right] = Y_val[perm_val]
                nudging_on[left: right, 0] = False
                val_res[vals_per_epoch * n + k, 0] = right_tr + n * len(X_train)
                val_idc[vals_per_epoch * n + k] = np.arange(left, right)
                left = right
                left_tr = right_tr

        target_seq = np.hstack((target_seq, nudging_on))

        ret = self.run(r_in_seq, trgt_seq=target_seq, n_passes=n_passes, n_exposures=n_exposures,
                       reset_weights=reset_weights, val_len=val_len, metric=metric,
                       rec_quants=rec_quants, rec_dn=rec_dn, info_update=info_update)
        val_res[:, 1:] = ret[-1] #valres

        return ret[:-1] + tuple([val_res])

    def train_long(self, X_train, Y_train, X_val, Y_val, n_epochs, val_len, n_out, classify, u_high=1.0,
              u_low=0.1, n_passes=2, n_exposures=1, vals_per_epoch=1, info_update=1000, metric=None):
        '''
        Version of SteadNet.Net.train for long datasets. run() is called per epoch not once for all epochs.
        Returns
        -------
        returns [SteadNet.Net.run from last epoch without last entry,
         validation results like train() concatenated for all epochs]
        '''
        assert len(X_train) > vals_per_epoch
        assert len(X_train) == len(Y_train)
        assert len(X_val) == len(Y_val)

        n_features = X_train.shape[1]

        assert n_features == X_val.shape[1]
        assert n_features == self.dims[0]
        assert n_out == self.dims[-1]

        if classify:
            assert len(Y_train.shape) == 1
            assert len(Y_val.shape) == 1
        else:
            assert (len(Y_train.shape) == 1 and n_out == 1) or Y_train.shape[1] == n_out
            assert (len(Y_val.shape) == 1 and n_out == 1) or Y_val.shape[1] == n_out

        len_split_train = round(len(X_train) / vals_per_epoch)  # validation after each split
        vals_per_epoch = len(X_train) // len_split_train
        print("%d validations per epoch" % (vals_per_epoch))

        length = vals_per_epoch * val_len + len(X_train)

        r_in_seq = np.zeros((length, n_features))
        val_res = np.zeros((vals_per_epoch * n_epochs,
                            3),
                           dtype=dtype)  # [number of training patterns seen, mse of val result, metric of val result]

        nudging_on = np.ones((length, 1), dtype=dtype)
        val_idc = np.zeros((len(val_res), val_len))  # indices of validation patterns

        for n in range(n_epochs):
            if classify:
                target_seq = np.ones((length, n_out), dtype=dtype) * u_low
            else:
                target_seq = np.zeros((length, n_out), dtype=dtype)
            perm_train = np.random.permutation(len(X_train))
            left = 0
            left_tr = 0
            for k in range(vals_per_epoch):
                if k == vals_per_epoch - 1:
                    right_tr = len(X_train)
                else:
                    right_tr = left_tr + len_split_train
                right = left + right_tr - left_tr
                r_in_seq[left: right] = X_train[perm_train[left_tr:right_tr]]
                if classify:
                    target_seq[np.arange(left, right), 1 * Y_train[
                        perm_train[left_tr:right_tr]]] = u_high  # enforce Y_train is an integer array!
                else:
                    target_seq[left:right] = Y_train[perm_train[left_tr:right_tr]]
                perm_val = np.random.permutation(len(X_val))[:val_len]
                left = right
                right = left + val_len
                r_in_seq[left: right] = X_val[perm_val]
                if classify:
                    target_seq[
                        np.arange(left, right), 1 * Y_val[perm_val]] = u_high  # enforce Y_val is an integer array!
                else:
                    target_seq[left:right] = Y_val[perm_val]
                nudging_on[left: right, 0] = False
                val_res[vals_per_epoch * n + k, 0] = right_tr + n * len(X_train)
                val_idc[vals_per_epoch * n + k] = np.arange(left, right)
                left = right
                left_tr = right_tr

            target_seq = np.hstack((target_seq, nudging_on))

            print("%d/%d epochs:"%(n+1, n_epochs))
            ret = self.run(r_in_seq, trgt_seq=target_seq, n_passes=n_passes, n_exposures=n_exposures,
                           reset_weights=False, val_len=val_len, metric=metric, info_update=info_update)
            val_res[n*vals_per_epoch:(n+1)*vals_per_epoch, 1:] = ret[-1] #valres

        return ret[:-1] + tuple([val_res])

def seq_to_trace(seq, dT):
    '''
    Transforms a sequence into a 'step-trace' with step size dT, i.e. each value in seq will appear twice in the
    output trace: seq = [s_0, s_1, s_2, ...] -> T = [0, dT, dT, 2dT, 2dT, 3dT, 3dT, ...],
     trace = [s_0, s_0, s_1, s_1, s_2, s_2, ...]
    Parameters
    ----------
    seq:        input sequence
    dT:         duration of each step for which output signal is constant and takes corresponding value of seq

    Returns
    -------
    [T,
    trace]
    '''
    T = np.zeros(len(seq) * 2)
    T[np.arange(len(seq)) * 2] = np.arange(len(seq))*dT
    T[np.arange(len(seq)) * 2 + 1] = np.arange(1, len(seq)+1)*dT
    trace = np.zeros((len(T), seq.shape[1]))
    trace[np.arange(len(seq)) * 2] = seq
    trace[np.arange(len(seq)) * 2 + 1] = seq
    return T, trace

# compare SteadNet with PyraLNet without plasticity (-> compare potentials)
def stead_vs_sim(N=1000, seed=45, reflect=False):
    Path("plots/SteadNet/stead_vs_sim").mkdir(parents=True, exist_ok=True)

    # how well does SteadNet predict fix-points of PyraLNet?

    X_train, Y_train = Dataset.YinYangDataset(size=N, flipped_coords=True, seed=seed)[:]
    params = {"dims": [4, 120, 3], "dt": 0.1, "gl": 0.1, "gb": 1.0, "ga": 0.8, "gd": 1.0,
              "gsom": 0.8,
              "eta": {"up": [0, 0.0], "pi": [0, 0], "ip": [0.0, 0]},
              "bias": {"on": False, "val": 0.5},
              "init_weights": {"up": 0.5, "down": 0.5, "pi": 0.5, "ip": 0.5}, "tau_w": 30, "noise": 0, "t_pattern": 100,
              "out_lag": 80, "tau_0": 3, "learning_lag": 0, "reset_deltas": False}

    stead_net = Net(params, act=pyral.sigmoid)
    sim_net = pyral.Net(params, act=pyral.sigmoid)
    if reflect:
        stead_net.reflect()
    # copy weights from SteadNet to PyraLNet
    for i in range(len(stead_net.layer) - 1):
        sim_net.layer[i].W_up = stead_net.layer[i].W_up
        sim_net.layer[i].W_ip = stead_net.layer[i].W_ip
        sim_net.layer[i].W_pi = stead_net.layer[i].W_pi
        sim_net.layer[i].W_down = stead_net.layer[i].W_down
    sim_net.layer[-1].W_up = stead_net.layer[-1].W_up

    # run PyraLNet for on a couple of patterns and record potentials
    rec_quants = [["pyr_soma", "inn_soma"], ["pyr_soma"]]
    target_seq = np.ones((len(Y_train), 3)) * 0.1
    target_seq[np.arange(len(Y_train)), 1 * Y_train] = 1.0
    target_seq = np.hstack((target_seq, np.zeros((len(Y_train), 1))))
    records_sim, T, _, _, _ = sim_net.run(X_train, trgt_seq=target_seq, learning_off=True, rec_quants=rec_quants, rec_dt=1)

    c = ["blue", "green", "orange"]
    ls = ["-", ":", "--"]
    plt.figure(figsize=(9, 6))
    # run SteadNet (with different param n_passes) for on a couple of patterns and record potentials
    for i, n_passes in enumerate([1, 2, 4]):
        records_stead, _ = stead_net.run(X_train, trgt_seq=target_seq, learning_off=True, rec_quants=rec_quants, rec_dn=1, n_exposures=1, n_passes=n_passes)

        u_sim = records_sim[0]["pyr_soma"].data.reshape(-1, 100, 120)[:,-1,:] # last value of pattern
        plt.plot(np.sqrt(np.mean((u_sim-records_stead[0]["pyr_soma"].data) ** 2 / np.mean(u_sim, axis=0) ** 2, axis=1)), c=c[0], ls=ls[i])
        u_sim = records_sim[0]["inn_soma"].data.reshape(-1, 100, 3)[:, -1, :]
        plt.plot(np.sqrt(np.mean((u_sim-records_stead[0]["inn_soma"].data) ** 2 / np.mean(u_sim, axis=0) ** 2, axis=1)), c=c[1], ls=ls[i])
        u_sim = records_sim[1]["pyr_soma"].data.reshape(-1, 100, 3)[:, -1, :]
        plt.plot(np.sqrt(np.mean((u_sim-records_stead[1]["pyr_soma"].data) ** 2 / np.mean(u_sim, axis=0) ** 2, axis=1)), c=c[2], ls=ls[i])

    # compare both methods
    plt.yscale("log")
    plt.ylabel("RMSE $u_{sim}-u_{stead}$ / RMS $u_{sim}$")
    plt.xlabel("pattern")
    plt.ylim([10 ** -7, None])
    plt.plot([0], [0], label="$u^{P}_0$", c=c[0])
    plt.plot([0], [0], label="$u^{I}_0$", c=c[1])
    plt.plot([0], [0], label="$u^{P}_1$", c=c[2])
    plt.plot([0], [0], label="n_passes: 1", c="lightgray", ls=ls[0])
    plt.plot([0], [0], label="n_passes: 2", c="lightgray", ls=ls[1])
    plt.plot([0], [0], label="n_passes: 4", c="lightgray", ls=ls[2])
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
    plt.title("Simulation vs. Steady-State: divergence of settled potentials%s"%(" (SPS)" if reflect else ""))
    plt.tight_layout()
    plt.savefig("plots/SteadNet/stead_vs_sim/div_pots%s.png" % ("_sps" if reflect else ""))
    plt.show()

# compare SteadNet with PyraLNet with plasticity (-> compare weights)
def stead_vs_sim_learning(N=5000, seed=45, reflect=False):
    Path("plots/SteadNet/stead_vs_sim").mkdir(parents=True, exist_ok=True)

    X_train, Y_train = Dataset.YinYangDataset(size=N, flipped_coords=True, seed=seed)[:]
    params = {"dims": [4, 120, 3], "dt": 0.1, "gl": 0.1, "gb": 1.0, "ga": 0.8, "gd": 1.0,
              "gsom": 0.8,
              "eta": {"up": [0.1, 0.01], "pi": [0.01, 0], "ip": [0.02, 0]},
              "bias": {"on": False, "val": 0.5},
              "init_weights": {"up": 0.5, "down": 0.5, "pi": 0.5, "ip": 0.5}, "tau_w": 30, "noise": 0, "t_pattern": 100,
              "out_lag": 80, "tau_0": 3, "learning_lag": 0, "reset_deltas": False}

    stead_net = Net(params, act=pyral.sigmoid)
    sim_net = pyral.Net(params, act=pyral.sigmoid)
    if reflect:
        sim_net.reflect()
    # save initial weights to init each network
    W0_up = sim_net.layer[0].W_up.copy()
    W0_ip = sim_net.layer[0].W_ip.copy()
    W0_pi = sim_net.layer[0].W_pi.copy()
    W0_down = sim_net.layer[0].W_down.copy()
    W1_up = sim_net.layer[-1].W_up.copy()

    # run PyraLNet for a bunch of patterns and records weights
    rec_quants = [["W_up", "W_ip"], ["W_up"]]
    target_seq = np.ones((len(Y_train), 3)) * 0.1
    target_seq[np.arange(len(Y_train)), 1 * Y_train] = 1.0
    target_seq = np.hstack((target_seq, np.ones((len(Y_train), 1))))
    records_sim, _, _, _, _ = sim_net.run(X_train, trgt_seq=target_seq, rec_quants=rec_quants, rec_dt=1)

    c = ["C0", "C1", "C2"]
    ls = ["-", ":", "--"]
    plt.figure(figsize=(9, 6))
    # run PyraLNet (with different param n_exposures) for a bunch of patterns and records weights
    for i, n_exposures in enumerate([1, 4, 8]):
        # init with the same weights
        stead_net.layer[0].W_up = W0_up.copy()
        stead_net.layer[0].W_ip = W0_ip.copy()
        stead_net.layer[0].W_pi = W0_pi.copy()
        stead_net.layer[0].W_down = W0_down.copy()
        stead_net.layer[-1].W_up = W1_up.copy()

        records_stead, _ = stead_net.run(X_train, trgt_seq=target_seq, rec_quants=rec_quants, rec_dn=1, n_exposures=n_exposures, n_passes=2)

        W_sim = records_sim[0]["W_up"].data.reshape(-1, 100, 120, 4)[:,-1] # last value of pattern
        plt.plot(np.sqrt(np.mean((W_sim-records_stead[0]["W_up"].data) ** 2 / np.mean(W_sim ** 2), axis=(1,2))), c=c[0], ls=ls[i])
        W_sim = records_sim[0]["W_ip"].data.reshape(-1, 100, 3, 120)[:, -1]
        plt.plot(np.sqrt(np.mean((W_sim-records_stead[0]["W_ip"].data) ** 2 / np.mean(W_sim ** 2), axis=(1,2))), c=c[1], ls=ls[i])
        W_sim = records_sim[1]["W_up"].data.reshape(-1, 100, 3, 120)[:, -1]
        plt.plot(np.sqrt(np.mean((W_sim-records_stead[1]["W_up"].data) ** 2 / np.mean(W_sim ** 2), axis=(1,2))), c=c[2], ls=ls[i])

    # compare weights
    plt.yscale("log")
    plt.xlabel("pattern")
    plt.ylabel("RMSE $W_{sim}-W_{stead}$ / RMS $W_{sim}$")
    plt.ylim([10**-4, None])
    plt.plot([0], [0], label="$W^{up}_0$", c=c[0])
    plt.plot([0], [0], label="$W^{ip}_0$", c=c[1])
    plt.plot([0], [0], label="$W^{up}_1$", c=c[2])
    plt.plot([0], [0], label="$\\frac{t_{pattern}}{dT}$ = 1", c="lightgray", ls=ls[0])
    plt.plot([0], [0], label="$\\frac{t_{pattern}}{dT}$ = 4", c="lightgray", ls=ls[1])
    plt.plot([0], [0], label="$\\frac{t_{pattern}}{dT}$ = 8", c="lightgray", ls=ls[2])
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
    plt.title("Simulation vs. Steady-State: Divergence of weights%s"%(" (SPS)" if reflect else ""))
    plt.tight_layout()
    plt.savefig("plots/SteadNet/stead_vs_sim/div_weights%s.png"%("_sps" if reflect else ""))
    plt.show()

def yinyang_task(N_train=6000, N_test=600, n_epochs=45, runs=10, reset_deltas=False):
    Path("plots/SteadNet/yinyang_task%s"%("_reset_deltas" if reset_deltas else "")).mkdir(parents=True, exist_ok=True)

    np.random.seed(42)
    seeds = np.random.randint(10, 12000, runs)

    accs = []
    for i in range(runs):
        X_train, Y_train = Dataset.YinYangDataset(size=N_train, flipped_coords=True, seed=seeds[i])[:]
        X_val, Y_val = Dataset.YinYangDataset(size=N_test, flipped_coords=True, seed=None)[:]
        X_test, Y_test = Dataset.YinYangDataset(size=N_test, flipped_coords=True, seed=None)[:]
        params = {"dims": [4, 120, 3], "dt": 0.1, "gl": 0.1, "gb": 1.0, "ga": 0.28, "gd": 1.0,
                  "gsom": 0.34,
                  "eta": {"up": [6.1, 0.00012], "pi": [0, 0], "ip": [0.00024, 0]},
                  "bias": {"on": True, "val": 0.5},
                  "init_weights": {"up": 0.1, "down": 1, "pi": 1, "ip": 0.1}, "tau_w": 30, "noise": 0, "t_pattern": 100,
                  "out_lag": 80, "tau_0": 3, "learning_lag": 20, "reset_deltas": reset_deltas}
        net = Net(params, act=pyral.sigmoid)
        net.reflect()

        out_seq, val_res = net.train(X_train, Y_train, X_val, Y_val, n_epochs=n_epochs, val_len=100, vals_per_epoch=1,
                                                          n_out=3, classify=True, u_high=1.0, u_low=0.1, metric=pyral.accuracy, n_exposures=1)

        # plot exponential moving average of validation error
        plt.title("Validation error during training")
        plt.semilogy(val_res[:, 0], pyral.ewma(val_res[:, 1], round(len(val_res) / 10)), label="mse")
        plt.xlabel("trial")
        plt.ylabel("mean squared error")
        ax2 = plt.gca().twinx()
        ax2.plot(val_res[:, 0], pyral.ewma(val_res[:, 2], round(len(val_res) / 10)), c="g", label="accuracy")
        ax2.set_ylabel("accuracy")
        plt.savefig("plots/SteadNet/yinyang_task/validation_accuracy_%d.png"%(i))
        plt.clf()

        # test run
        out_seq_test = net.run(X_test, learning_off=True, n_exposures=1)
        y_pred = np.argmax(out_seq_test, axis=1)
        acc = np.sum(y_pred == Y_test) / len(Y_test)
        print("test set accuracy: %f"%(acc))
        Dataset.plot_yy(X_test, y_pred)
        plt.title("test acc = %f"%(acc))
        plt.savefig("plots/SteadNet/yinyang_task/test_result_%d.png"%(i))
        plt.clf()

        accs += [acc]

    if runs > 1:
        print("mean/std accuracy: %f/%f%%"%(np.mean(accs*100), np.std(accs*100)))

def mnist_task():
    Path("plots/SteadNet/mnist_task").mkdir(parents=True, exist_ok=True)
    X_train = mnist.train_images().reshape((-1, 28**2))
    Y_train = mnist.train_labels()
    rand_ind = np.random.permutation(len(X_train))
    X_val = X_train[rand_ind[:5000]]
    Y_val = Y_train[rand_ind[:5000]]
    X_train = X_train[rand_ind[5000:]]
    Y_train = Y_train[rand_ind[5000:]]
    X_test = mnist.test_images().reshape((-1, 28 ** 2))
    Y_test = mnist.test_labels()

    params = {"dims": [28**2, 500, 500, 10], "dt": 0.1, "gl": 0.1, "gb": 1.0, "ga": 0.471, "gd": 1.0,
              "gsom": 0.12,
              "eta": {"up": [0.001111, 0.0003333, 0.0001], "pi": [0, 0, 0], "ip": [0.002222, 0.0006666]},
              "bias": {"on": False, "val": 0.0},
              "init_weights": {"up": 0.05, "down": 1, "pi": 1, "ip": 0.05}, "tau_w": 30, "noise": 0, "t_pattern": 100,
              "out_lag": 80, "tau_0": 3, "learning_lag": 0, "reset_deltas": False}
    net = Net(params, act=pyral.sigmoid_stable)
    net.reflect()

    out_seq, val_res = net.train_long(X_train, Y_train, X_val, Y_val, n_epochs=200, val_len=5000, vals_per_epoch=1,
                                 n_out=10, classify=True, metric=pyral.accuracy, n_exposures=1)

    # plot exponential moving average of validation error
    plt.title("Validation error during training")
    plt.semilogy(val_res[:, 0], pyral.ewma(val_res[:, 1], round(len(val_res) / 10)), label="mse")
    plt.xlabel("trial")
    plt.ylabel("mean squared error")
    ax2 = plt.gca().twinx()
    ax2.plot(val_res[:, 0], pyral.ewma(val_res[:, 2], round(len(val_res) / 10)), c="g", label="accuracy")
    ax2.set_ylabel("accuracy")
    plt.savefig("plots/SteadNet/yinyang_task/validation_accuracy.png")
    plt.show()

    # test run
    out_seq_test = net.run(X_test, learning_off=True, n_exposures=1)
    y_pred = np.argmax(out_seq_test, axis=1)
    acc = np.sum(y_pred == Y_test) / len(Y_test)
    print("test set accuracy: %f" % (acc))

if __name__ == "__main__":
    yinyang_task(reset_deltas=True)


import json

from Products.Five.browser import BrowserView
from zope.i18n import translate

from collective.whathappened.browser.notifications import getHotNotifications
from collective.whathappened.browser.notifications import getUnseenCount

class NotificationAjax(BrowserView):
    def updateResponse(self):
        response = self.request.response
        if response.status in (301, 302):
            response.setStatus(200)
        response.setHeader("Content-type", "application/json")

    def __call__(self):
        self.updateResponse()
        notifications = getHotNotifications(self.context, self.request)
        for notification in notifications:
            notification['title'] = translate(
                notification['title'],
                context=self.request
            )
        unseenCount = getUnseenCount(self.context, self.request)
        return json.dumps({'unseenCount': unseenCount,
                           'notifications': notifications})


from rest_framework import viewsets, mixins
from rest_framework.decorators import list_route
from rest_framework.response import Response

from apps.admins.models import *
from apps.admins.serializers import *
from util.errors import ParameterException

# 用户视图
class WhAdminViewSet(viewsets.GenericViewSet, mixins.ListModelMixin,
                     mixins.RetrieveModelMixin, mixins.CreateModelMixin):
    queryset = WhAdmin.objects.all()
    serializer_class = WhAdminSerializer
    # authentication_classes = (WhAdminTokenAuthentication,)

    def list(self, request, *args, **kwargs):
        token = request.query_params.get("token")
        user_id = cache.get(token)
        user = WhAdmin.objects.filter(admin_id=user_id).first()
        if user:  # 如果用户已经登录的情况
            serializer = self.get_serializer(user)  # 返回serializer_class指向的序列化类的对象
            res = {
                "user_info": serializer.data,
                "flag": True  # 是否登录的标识
            }
            return Response(res)
        else:
            res = {
                "flag": False  # 是否登录的标识
            }
            return Response(res)

    # @list_route注册一个新API，形如：/methodname/
    @list_route(methods=["POST"], serializer_class=WhAdminRegisterSerializer)
    def register(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        # raise_exception=False,未通过验证，不抛出异常，返回值，继续往下执行
        # is_valid()会联动调用序列化对象的validate（）方法
        result = serializer.is_valid(raise_exception=True)
        if not result:
            raise ParameterException({"code": "1005", "msg": "注册数据未通过验证！"})

        print("serializer.data=====")
        print(type(serializer.data))
        data = serializer.register_data(serializer.data)  # 调用封装的注册方法，返回字典
        return Response(data)

    @list_route(methods=["POST"], serializer_class=WhAdminLoginSerializer)
    def login(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        result = serializer.is_valid(raise_exception=False)

        if not result:
            raise ParameterException({'code': '1005', 'msg': '登陆数据未通过验证！'})
        res = serializer.login_data(serializer.data)
        return Response(res)

    @list_route(methods=['POST', "GET"], )
    def logout(self, request, *args, **kwargs):
        token = request.data.get("token")
        user_id = cache.get(token)  # 获取token对相应的用户id
        user = WhAdmin.objects.filter(admin_id=user_id).exists()
        data = {}
        if user:
            cache.delete(token)
            data['code'] = 200
            data['msg'] = '账户已退出！！'
        return Response(data)

# 分组视图
class WhAdminGroupViewSet(viewsets.GenericViewSet, mixins.ListModelMixin,
                     mixins.RetrieveModelMixin, mixins.CreateModelMixin):
    queryset = WhAdminGroup.objects.all()
    serializer_class = WhAdminGroupSerializer


import sys
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s', filename='logfile.txt')

def print_maturity(age):
    if age >= 18:
        logging.info('Adult!')
    else:
        logging.info('Not adult!!!')

if __name__ == '__main__':
    logging.debug('The program was called with these parameters %s' % sys.argv[1:])
    logging.debug('The first parameter is %s' % sys.argv[1])
    age = int(sys.argv[1])
    print_maturity(age)


import os
import logging
import pandas as pd
import click
from sklearn.model_selection import train_test_split

@click.command('data_split')
@click.option('--input-path')
@click.option('--output-path')
@click.option('--train-size', type=float)
@click.option('--shuffle', type=bool)
def data_split(input_path: str, output_path: str, train_size: float,
               shuffle: bool):
    log('Begin split data...')

    if not os.path.exists(input_path):
        raise ValueError(f'Data path {input_path} does not exist')

    data_path = os.path.join(input_path, 'data.csv')
    if not os.path.exists(data_path):
        raise ValueError(f'Data file {data_path} does not exist')

    data = pd.read_csv(data_path)
    data_train, data_valid = train_test_split(data, stratify=data['target'],
                                              train_size=train_size,
                                              shuffle=shuffle, random_state=9)
    data_train.to_csv(os.path.join(output_path, 'data_train.csv'), index=False)
    data_valid.to_csv(os.path.join(output_path, 'data_valid.csv'), index=False)

    log('Data split success')

def log(message):
    logger = logging.getLogger('data_split')
    logger.warning(message)

if __name__ == '__main__':
    data_split()


import scrapy
from scrapy.loader import ItemLoader

from leroy.items import LeroyParserItem

class LeroyMerlinSpider(scrapy.Spider):
    name = 'leroymerlin'
    allowed_domains = ['leroymerlin.ru']
    url = 'https://leroymerlin.ru'

    def __init__(self, searchInput):
        super(LeroyMerlinSpider, self).__init__()
        self.search = searchInput
        self.start_urls = [self.url + f'/search/?q={self.search}&suggest=true']

    def parse(self, response):
        product_links = response.xpath(
            '//a[@data-qa="product-name"]/@href').extract()
        for link in product_links:
            yield response.follow(f'{self.url + link}', callback=self.parse_goods)

        next_link = response.xpath(
            '//a[contains(@aria-label, "Следующая страница")]/@href').extract()
        if next_link[0] is not None:
            next_page = self.url + next_link[0]
            yield response.follow(next_page, callback=self.parse)

    def parse_goods(self, response):
        loader = ItemLoader(item=LeroyParserItem(), response=response)
        loader.add_xpath('name', '//h1/text()')
        loader.add_value('url', response.url)
        loader.add_xpath('price', '//span[@slot="price"]/text()')
        loader.add_xpath('article', '//span[@slot="article"]/text()')
        loader.add_xpath(
            'text',
            '//uc-pdp-section-layout/uc-pdp-section-vlimited/div/p/text()')
        loader.add_xpath('photo', '//img[@alt="image thumb"]/@src')
        specifications_list = response.xpath('//dl[@class="def-list"]/child::div')
        loader.add_value('specifications', specifications_list)

        yield loader.load_item()


import boto3
from botocore import UNSIGNED
from botocore.config import Config
from enum import Enum
import os

class OpenImageDataType(Enum):
    TRAIN = "train"
    TEST = "test"
    VALIDATION = "validation"

class OpenImageWrapper:
    def __init__(self, root_dst_folder="image"):
        self.__bucket_name = "open-images-dataset"
        self.__train_folder_name = "train"
        self.__val_folder_name = "validation"
        self.__test_folder_name = "test"
        self.__root_dst_folder = root_dst_folder

        # initialize
        os.makedirs(self.__root_dst_folder, exist_ok=True)
        self.__s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED))

    def download(self, data_type):
        folder_name = data_type.value
        dst_folder = os.path.join(self.__root_dst_folder, folder_name)
        os.makedirs(dst_folder, exist_ok=True)

        # get image file list
        file_key_list = self.__get_file_list(folder_name)

        # download image files.
        file_count = len(file_key_list)
        for i, file_key in enumerate(file_key_list):
            print("{}/{} start to download file..".format(i+1, file_count))
            filename = os.path.basename(file_key)
            with open(os.path.join(dst_folder, filename), "wb") as file:
                self.__s3.download_fileobj(Bucket=self.__bucket_name, Key=file_key, Fileobj=file)

    def __get_file_list(self, prefix):
        file_key_list = []
        next_token = ""

        while True:
            if next_token == "":
                response = self.__s3.list_objects_v2(Bucket=self.__bucket_name, Prefix=prefix)
            else:
                response = self.__s3.list_objects_v2(Bucket=self.__bucket_name, Prefix=prefix, ContinuationToken=next_token)

            if "Contents" in response:
                contents = response["Contents"]

                for content in contents:
                    file_key_list.append(content["Key"])

            if "NextContinuationToken" in response:
                next_token = response['NextContinuationToken']
            else:
                break

        return file_key_list

if __name__ == "__main__":
    wrapper = OpenImageWrapper()
    wrapper.download(OpenImageDataType.VALIDATION)


from dataclasses import dataclass

from starknet_py.contract import SentTransaction
from starknet_py.utils.sync import add_sync_methods

@add_sync_methods
@dataclass(frozen=True)
class AccountDeploymentResult(SentTransaction):
    """
    Result of the :meth:`Account.deploy_account <starknet_py.net.account.account.Account.deploy_account>` method.
    """

    account: "Account" = None  # pyright: ignore
    """Account instance created during the deployment."""

    def __post_init__(self):
        if self.account is None:
            raise ValueError(
                "Parameter account cannot be None in AccountDeploymentResult."
            )


import string
import json
import uuid
import pytest
import autoit
import getpass
import uuid
from playwright.async_api import Page, Playwright, Browser

def test_newtemplatepage(playwright: Playwright, pageurl: string)-> Page:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context(storage_state="cway")
    page = context.new_page()
    page.goto(pageurl)
    return page

def test_open_page_sso(playwright: Playwright, pageurl: string)-> Page:
    context = playwright.chromium.launch_persistent_context(user_data_dir=f"c:\\User\\{getpass.getuser()}\\AppData\\Local\\Microsoft\\Edge\\User Data",
                                                accept_downloads=True,
                                                headless=False,
                                                bypass_csp=False,
                                                slow_mo=1000,
                                                channel="msedge")
    page = context.new_page()
    page.storage_state="cway"
    page.goto(pageurl)
    if page.get_by_text("Single sign-on").is_visible():
        page.get_by_text("Single sign-on").click()
        page.get_by_role("button", name="Continue").click()
        page.wait_for_timeout(5000)
    return page

def test_createAndinstall(page:Page, machinetype: string):
    page.wait_for_timeout(500)

    if not machinetype=="2-core":
        page.get_by_role("button", name="2-core").click()
        page.wait_for_timeout(500)
        page.get_by_role("menuitemcheckbox", name=machinetype).click()
        page.wait_for_timeout(800)
        assert page.get_by_role("button", name=machinetype).count()==1

    page.get_by_role("button", name="Create codespace").click()

    page.wait_for_timeout(75000)

    test_upload_install_vsix(page)
    page.wait_for_timeout(3000)

def test_terminalcommand(page: Page, cmdline: string):
    terminaltextarea="#terminal > div > div > div.monaco-scrollable-element > div.split-view-container > div > div > div.pane-body.shell-integration.integrated-terminal.wide > div.monaco-split-view2.horizontal > div.monaco-scrollable-element > div.split-view-container > div > div > div > div > div > div.monaco-scrollable-element > div.split-view-container > div > div > div > div > div > div.xterm-screen > div.xterm-helpers > textarea"
    page.type(terminaltextarea, cmdline)
    page.keyboard.press("Enter")

def test_terminalothertemplatecommand(page: Page, cmdline: string, terminaltextarea: string,assertstr:string):
    if "fatal: not a git repository"!=assertstr:
        if page.locator('#list_id_1_0').get_attribute('aria-selected')=="false":
            bashselector="#list_id_1_0 > div > div > div.monaco-icon-label-container"
            page.locator(bashselector).click()
    page.type(terminaltextarea, cmdline)
    page.keyboard.press("Enter")

# @pytest.mark.getgithubuserrepo
def test_getgithubuserrepo(page: Page, reponame: string):
    page.goto(test_getgithubuser()+"/"+reponame)
    page.wait_for_timeout(5000)
    page.reload()
    # assert that a link exists on the page
    assert reponame in page.title()
    page.wait_for_timeout(3000)

def test_getgithubuser() -> string:
    github="https://github.com/"
    return github + test_getusenamefromcookiefile()

def test_getusenamefromcookiefile() -> string:
    # read cookies from the json file
    with open('cway', 'r') as f:
      cookies = json.load(f)
    # get a value from the cookies array by name
    for cookie in cookies["cookies"]:
      if cookie['name'] == 'dotcom_user':
        return cookie['value']

def test_create_ppe_codespace(page: Page, reponame: string):
    page.get_by_role("button", name="Select a repository").click()
    page.keyboard.type(reponame)
    page.wait_for_timeout(1500)
    page.keyboard.press("ArrowDown")
    page.keyboard.press("Enter")
    page.wait_for_timeout(1000)
    page.get_by_role("button", name="production").click()
    page.get_by_role("menuitemradio", name="pre-production").check()
    page.wait_for_timeout(400)
    assert  page.get_by_role("button", name="pre-production").is_visible()
    page.wait_for_timeout(600)

def test_upload_install_vsix(page: Page):
    page.wait_for_timeout(2000)
    page.get_by_role("tab", name="Extensions").click()
    page.get_by_label("Search Extensions in Marketplace").fill("github codespace")
    page.locator("div[class=extension-list-item]").filter(has_text="GitHub Codespaces").nth(0).click()
    githubcodespaceversion=page.get_by_title("Extension Version").inner_text()
    page.wait_for_timeout(2000)
    page.keyboard.press("Control+Shift+E")
    for i in range(10):
        page.wait_for_timeout(2500)
        if ".vsix" not in page.locator("#workbench\.view\.explorer > div > div > div.monaco-scrollable-element > div.split-view-container > div:nth-child(1) > div > div.pane-body").inner_text():
            # if not autoit.win_exists("[CLASS:#32770]"):
            page.mouse.click(x=150, y=500, delay=0, button="right")
            page.wait_for_timeout(800)
            for i in range(10):
                if not page.locator("text=Upload...").is_visible():
                    page.mouse.click(x=150, y=500, delay=0, button="right")
                    page.wait_for_timeout(800)
                else:
                    break
            page.click("text=Upload...")
            page.wait_for_timeout(2000)
        # else:
            autoit.control_send("[CLASS:#32770]", "Edit1", 'C:\\Users\\v-margema\\Downloads\\codespaces-1.14.8.vsix')
            page.wait_for_timeout(2000)
            autoit.control_click("[Class:#32770]", "Button1")
            page.wait_for_timeout(6000)
        else:
            break
    page.get_by_role("treeitem").filter(has_text="vsix").click()
    page.get_by_role("treeitem").filter(has_text="vsix").click(button="right")
    page.wait_for_timeout(500)
    if page.locator("text=Install Extension VSIX").count()<1:
        page.click("text=Copy")
        page.wait_for_timeout(500)
        page.get_by_role("treeitem").filter(has_text="vsix").click()
        page.get_by_role("treeitem").filter(has_text="vsix").click(button="right")
        page.wait_for_timeout(500)
    page.click("text=Install Extension VSIX")
    page.wait_for_timeout(3000)
    page.get_by_role("tab", name="Extensions").click()
    page.locator("div[class=extension-list-item]").filter(has_text="GitHub Codespaces").nth(0).click()
    githubcodeInstalledversion=page.get_by_title("Extension Version").inner_text()
    assert githubcodeInstalledversion >= githubcodespaceversion
    page.wait_for_timeout(1000)
    if page.get_by_title("Reload Now").is_visible():
        page.get_by_title("Reload Now").click()
    page.wait_for_timeout(15000)


import datetime
import urllib.parse
import urllib.request
import zlib

#Functions to retrieve schedules
class HubCacheRetriever:
  def __init__(self, appgwUrl, slot):
    self.appgwUrl = appgwUrl
    self.slot = slot

  def getCache(self, timestamp, channelMapId, popularityOrder = False):
    cacheUrl = self._buildCacheUrl(timestamp, channelMapId, popularityOrder)
    print("The channel map hub catchup cache url generated is : \n", cacheUrl)
    cache = None
    self.lastRequestedUrl = cacheUrl
    try:
      compressedCache = urllib.request.urlopen(cacheUrl)
      decompressedData = zlib.decompress(compressedCache.read(), 16+zlib.MAX_WBITS)
      cache = decompressedData.decode('utf-8')
    except urllib.error.HTTPError:
      print("HTTPError when trying to obtain Hub Cache. URL:" + cacheUrl)
    return cache

  def _buildCacheUrl(self, timestamp, channelMapId, popularityOrder = False):
    utcDateStr = datetime.datetime.utcfromtimestamp(int(timestamp)).strftime('%Y-%m-%d')
    blobContainerName = str(self.slot).lower() + "-catchup-data-" + utcDateStr
    strOrderBy = "Popularity-" if popularityOrder else ""
    cacheFileName = "channelmap-hub-" + strOrderBy + channelMapId + ".gz"
    #format of the url: http://{appgwUrl}/catalogcache/{bloblContainerName}/channelmap-hub-[Popularity-]{channelMapId}.gz
    cacheUrl = self.appgwUrl.replace("{type}", "storage") + "catalogcache/" + blobContainerName + "/" + cacheFileName
    return cacheUrl

class PerStationCacheRetriever:
  def __init__(self, appgwUrl, slot):
    self.appgwUrl = appgwUrl
    self.slot = slot

  def getCache(self, requestedTimestamp, countPerStation, stations, top, popularityOrder = False):
    cacheUrl = _buildPerStationCatchupCacheUrl(requestedTimestamp, countPerStation, stations, top, popularityOrder)
    print("The PerStationCatchupSchedules url generated is : \n", cacheUrl)
    cache = None
    self.lastRequestedUrl = cacheUrl
    try:
      cache = urllib.request.urlopen(cacheUrl)
    except urllib.error.HTTPError:
      print("HTTPError when trying to obtain PerStation Cache. URL:" + cacheUrl)
    return cache

  def _buildCacheUrl(self, requestedTimestamp, countPerStation, stations, top, popularityOrder = False):
    params = {}
    dateStr = datetime.datetime.utcfromtimestamp(requestedTimestamp).strftime('%Y-%m-%dT%H:%M:%S.000Z')
    params['requestedTime'] = dateStr
    params['pivots'] = "Language|en"
    params['countPerStation'] = countPerStation
    params['$orderBy'] = "Popularity" if popularityOrder else ""
    params['$stations'] = ",".join(map(str, stations))
    params['$top'] = top
    params['$lang'] = "en-US"
    strUrlEncodedParams = urllib.parse.urlencode(params)
    return appgwUrl.replace("{type}", "client") + slot.upper() + "/discovery/v3/feeds/catchup/pivot-items?" + strUrlEncodedParams

class StationCacheRetriever:
  def __init__(self, appgwUrl, slot):
    self.appgwUrl = appgwUrl
    self.slot = slot

  def getCache(self, requestedTime, stationId, skip, top, popularityOrder = False):
    cacheUrl = _buildStationCatchupCacheUrl(requestedTime, stationId, skip, top, popularityOrder)
    print("The StationCatchupSchedules url generated is : \n", cacheUrl)
    cache = None
    self.lastRequestedUrl = cacheUrl
    try:
      cache = urllib.request.urlopen(cacheUrl)
    except urllib.error.HTTPError:
      print("HTTPError when trying to obtain Station Cache. URL:" + cacheUrl)
    return cache

  def _buildCacheUrl(self, requestedTimestamp, stationId, skip, top, popularityOrder = False):
    params = {}
    dateStr = datetime.datetime.utcfromtimestamp(requestedTimestamp).strftime('%Y-%m-%dT%H:%M:%S.000Z')
    params['requestedTime'] = dateStr
    params['pivots'] = "Language|en"
    params['$skip'] = skip
    params['$orderBy'] = "Popularity" if popularityOrder else ""
    params['$stations'] = stationId
    params['$top'] = top
    params['$lang'] = "en-US"
    strUrlEncodedParams = urllib.parse.urlencode(params)
    return self.appgwUrl.replace("{type}", "client") + self.slot.upper() + "/discovery/v3/libraries/catchup/items?" + strUrlEncodedParams
#End of Functions to retrieve schedules


# Using Mobilenet_V2

# -----------------------------------------------------------------------

# Liên kết danh sách ảnh trong bộ dữ liệu, sau đó khởi tạo
# danh sách dữ liệu và nhãn
from imutils import paths
import config
imagePaths = list(paths.list_images(config.BASE_PATH))
# print("\n".join(imagePaths))

# Khởi tạo biến để lưu label và data
labels = []
dataset = []

import os
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
# Load từng ảnh trong dữ liệu (data)
print("[INFO] Loading image form disk....")
for imagePath in imagePaths:
    # Lấy nhãn
    label = imagePath.split(os.path.sep)[1]
    # print(label)

    # Tải ảnh với đầu vào là 224x224 và xử lý ảnh sang dạng mảng
    image = load_img(imagePath, target_size=config.INPUT_DIMS)
    image = img_to_array(image)
    image = preprocess_input(image)

    # Cập nhật dữ liệu và nhãn
    labels.append(label)
    dataset.append(image)

# -----------------------------------------------------------------------

# Chuyển đổi dữ liệu và nhãn sang kiểu dữ liệu Numpy Array
print("[INFO] Converting data format...")
import numpy as np
dataset = np.array(dataset, dtype="float32")
labels = np.array(labels)
# print(labels)

# -----------------------------------------------------------------------

from sklearn.preprocessing import LabelBinarizer
from tensorflow.keras.utils import to_categorical
# Sử dụng one-hot coding để tăng hiệu xuất phân loại cho class
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
labels = to_categorical(labels)
# print(labels)

# -----------------------------------------------------------------------

from sklearn.model_selection import train_test_split
# Chia tập train và tập validation
(Xtrain, Xtest, Ytrain, Ytest) = train_test_split(dataset, labels, test_size=0.2,
                                                  stratify=labels, random_state=42)
# print(Xtrain.shape)
# print(Ytrain)
# print(Xtest.shape)
# print(Ytest)

# -----------------------------------------------------------------------

# Tạo bộ dữ liệu lớn hơn bằng cách sử dụng ImageGenerater
from tensorflow.keras.preprocessing.image import ImageDataGenerator
aug = ImageDataGenerator(rotation_range=20,
                         zoom_range=0.15,
                         width_shift_range=0.2,
                         height_shift_range=0.2,
                         shear_range=0.15,
                         horizontal_flip=True,
                         fill_mode="nearest"
                         )

# -----------------------------------------------------------------------

# Using transfer learning - Mobilenet_V2 model
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Flatten
from tensorflow.keras.models import Model

baseModel = MobileNetV2(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3)))
# print(baseModel.summary())

# Tạo phần ourput model để thay thế cho phần output của model gốc
head_Model = baseModel.output
head_Model = AveragePooling2D(pool_size=(7, 7))(head_Model)
head_Model = Flatten()(head_Model)
head_Model = Dense(64, activation="relu")(head_Model)
head_Model = Dropout(0.2)(head_Model)
head_Model = Dense(2, activation="softmax")(head_Model)

# Đặt vào phần FC model và phần cuối của model gốc
model = Model(inputs=baseModel.input, outputs=head_Model)
# print(model.summary())

# Đóng băng các layers để chúng ko bị update trong quá trình training
for layer in baseModel.layers:
    layer.trainable = False

# -----------------------------------------------------------------------

# Biên soạn model
from tensorflow.keras.optimizers import Adam
print("[INFO] Compiling model....")
model.compile(loss="categorical_crossentropy", optimizer=Adam(config.LR), metrics=["acc"])

# Train model
print("[INFO] Training head model....")
BS = 24
epochs = 5
Hist = model.fit(aug.flow(Xtrain, Ytrain, batch_size=BS),
                      steps_per_epoch=len(Xtrain) // BS,
                      validation_data=(Xtest, Ytest),
                      validation_steps=len(Xtest) // BS,
                      epochs=epochs)

# -----------------------------------------------------------------------

# Dự đoán trên tập test
print("[INFO] Evaluating network....")
predIdxs = model.predict(Xtest, batch_size=BS)

# Tìm index của mỗi ảnh trong tập test và dự đoán class cho ảnh đó
predIdxs = np.argmax(predIdxs, axis=1)
# print(predIdxs)

# Show classification report
from sklearn.metrics import classification_report
print(classification_report(Ytest.argmax(axis=1), predIdxs, target_names=lb.classes_))

# -----------------------------------------------------------------------

# Save the model
import pickle

print("[INFO] Saving the model....")
model.save(config.MODEL_PATH)

# Save label sử dụng label encoder

f = open(config.LABEL_PATH, "wb")
f.write(pickle.dumps(lb))
f.close()

# -----------------------------------------------------------------------

# plot the training loss and accuracy
import matplotlib.pyplot as plt
N = epochs
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, N), Hist.history["loss"], label="train_loss")
plt.plot(np.arange(0, N), Hist.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), Hist.history["acc"], label="train_acc")
plt.plot(np.arange(0, N), Hist.history["val_acc"], label="val_acc")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")
plt.savefig("plot.png")


import numpy as np
import cv2
import glob
from collections import deque
import os

whiteLower = np.array([0,0,0])
whiteUpper = np.array([255,255,255])

darkblueLower = np.array([101,136,125])
darkblueUpper = np.array([121,184,201])

# greenLower = np.array([56,66,51])
# greenUpper = np.array([85,150,130])

greenLower = np.array([40,63,86])
greenUpper = np.array([71,145,181])

redLower = np.array([169,145,29])
redUpper = np.array([180,207,190])

yellowLower = np.array([21,137,121])
yellowUpper = np.array([36,202,191])

from collections import deque

# Define the upper and lower boundaries for a color to be considered "Blue"
# blueLower = np.array([100, 60, 60])
# blueUpper = np.array([140, 255, 255])

# Define a 5x5 kernel for erosion and dilation
kernel = np.ones((5, 5), np.uint8)

# Setup deques to store separate colors in separate arrays
bpoints = [deque(maxlen=512)]
gpoints = [deque(maxlen=512)]
rpoints = [deque(maxlen=512)]
ypoints = [deque(maxlen=512)]

bindex = 0
gindex = 0
rindex = 0
yindex = 0

colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 255, 255)]

# (145, 255, 251)

colorIndex = 0

# Setup the Paint interface
paintWindow = np.zeros((1366,768,3)) + 255
paintWindow = cv2.rectangle(paintWindow, (40,1), (140,65), (0,0,0), 2)
paintWindow = cv2.rectangle(paintWindow, (160,1), (255,65), colors[0], -1)
paintWindow = cv2.rectangle(paintWindow, (275,1), (370,65), colors[1], -1)
paintWindow = cv2.rectangle(paintWindow, (390,1), (485,65), colors[2], -1)
paintWindow = cv2.rectangle(paintWindow, (505,1), (600,65), colors[3], -1)
cv2.putText(paintWindow, "CLEAR ALL", (49, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "BLUE", (185, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "GREEN", (298, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "RED", (420, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
cv2.putText(paintWindow, "YELLOW", (520, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (150,150,150), 2, cv2.LINE_AA)

cv2.namedWindow('Paint', cv2.WINDOW_AUTOSIZE)

# Load the video
camera = cv2.VideoCapture(0)

path = os.listdir('saved/')

img_no=1
done = 0

def colour_points( hsv, img, lower_thres, upper_thres, bpoints, bindex, gpoints, gindex, rpoints, rindex, ypoints, yindex, colorIndex):

    blueMask = cv2.inRange(hsv, lower_thres, upper_thres)
    blueMask = cv2.erode(blueMask, kernel, iterations=3)
    blueMask = cv2.morphologyEx(blueMask, cv2.MORPH_OPEN, kernel)
    blueMask = cv2.dilate(blueMask, kernel, iterations=2)

    # Find contours in the image
    (cnts, _) = cv2.findContours(blueMask.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    center = None

    # Check to see if any contours were found
    if len(cnts) > 0:
        # Sort the contours and find the largest one -- we
        # will assume this contour correspondes to the area of the bottle cap
        cnt = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
        # Get the radius of the enclosing circle around the found contour
        ((x, y), radius) = cv2.minEnclosingCircle(cnt)
        # Draw the circle around the contour
        cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
        # cv2.circle(img, (int(x), int(y)), 1, (0, 255, 255), 10)

        # Get the moments to calculate the center of the contour (in this case Circle)
        M = cv2.moments(cnt)
        center = (int(M['m10'] / M['m00']), int(M['m01'] / M['m00']))

        if colorIndex==4:
            # if 40 <= center[0] <= 140: # Clear All

            bpoints.clear()
            gpoints.clear()
            rpoints.clear()
            ypoints.clear()

            bpoints = [deque(maxlen=512)]
            gpoints = [deque(maxlen=512)]
            rpoints = [deque(maxlen=512)]
            ypoints = [deque(maxlen=512)]

            bindex = 0
            gindex = 0
            rindex = 0
            yindex = 0

            paintWindow[67:,:,:] = 255

        #     elif 160 <= center[0] <= 255:
        #             colorIndex = 0 # Blue
        #     elif 275 <= center[0] <= 370:
        #             colorIndex = 1 # Green
        #     elif 390 <= center[0] <= 485:
        #             colorIndex = 2 # Red
        #     elif 505 <= center[0] <= 600:
        #             colorIndex = 3 # Yellow

        else :
            if colorIndex == 0:
                bpoints[bindex].appendleft(center)
            elif colorIndex == 1:
                gpoints[gindex].appendleft(center)
            elif colorIndex == 2:
                rpoints[rindex].appendleft(center)
            elif colorIndex == 3:
                ypoints[yindex].appendleft(center)
    # Append the next deque when no contours are detected (i.e., bottle cap reversed)
    else:
        bpoints.append(deque(maxlen=512))
        bindex += 1
        gpoints.append(deque(maxlen=512))
        gindex += 1
        rpoints.append(deque(maxlen=512))
        rindex += 1
        ypoints.append(deque(maxlen=512))
        yindex += 1

    # Draw lines of all the colors (Blue, Green, Red and Yellow)
    points = [bpoints, gpoints, rpoints, ypoints]
    for i in range(len(points)):
        for j in range(len(points[i])):
            for k in range(1, len(points[i][j])):
                if points[i][j][k - 1] is None or points[i][j][k] is None:
                    continue

                # cv2.circle(img, (int(points[i][j][k - 1][0]), int(points[i][j][k - 1][1])), 1, (0, 255, 255), 10)
                # cv2.line(frame, points[i][j][k - 1], points[i][j][k], colors[i], 2)
                cv2.line(img, points[i][j][k - 1], points[i][j][k], colors[i], 10)
                # print( points[i][j][k - 1][0], points[i][j][k - 1][1], points[i][j][k])
                # cv2.line(paintWindow, points[i][j][k - 1], points[i][j][k], colors[i], 2)

    return img

# cv2.namedWindow("Paint", cv2.WINDOW_NORMAL)
# cv2.setWindowProperty("Paint", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

while True:

    img = cv2.imread('saved/' + str(img_no)+'.jpg')
    # img = cv2.imread('saved/26.png')
    if img is None:
        img_no+=1
        continue
    # img = cv2.resize(img, (1920, 1080))
    img = cv2.resize(img, (1366, 768))

    while True:
        # Grab the current paintWindow
        (grabbed, frame) = camera.read()
        # frame = cv2.resize(frame, (1920,1080))
        frame = cv2.resize(frame, (1366,768))
        frame = cv2.flip(frame, 1)
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

        # img = cv2.rectangle(img, (40,1), (140,65), (122,122,122), -1)
        # img = cv2.rectangle(img, (160,1), (255,65), colors[0], -1)
        # img = cv2.rectangle(img, (275,1), (370,65), colors[1], -1)
        # img = cv2.rectangle(img, (390,1), (485,65), colors[2], -1)
        # img = cv2.rectangle(img, (505,1), (600,65), colors[3], -1)
        # cv2.putText(img, "CLEAR ALL", (49, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
        # cv2.putText(img, "BLUE", (185, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
        # cv2.putText(img, "GREEN", (298, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
        # cv2.putText(img, "RED", (420, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
        # cv2.putText(img, "YELLOW", (520, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (150,150,150), 2, cv2.LINE_AA)

        # Add the coloring options to the frame
        # frame = cv2.rectangle(frame, (40,1), (140,65), (122,122,122), -1)
        # frame = cv2.rectangle(frame, (160,1), (255,65), colors[0], -1)
        # frame = cv2.rectangle(frame, (275,1), (370,65), colors[1], -1)
        # frame = cv2.rectangle(frame, (390,1), (485,65), colors[2], -1)
        # frame = cv2.rectangle(frame, (505,1), (600,65), colors[3], -1)
        # cv2.putText(frame, "CLEAR ALL", (49, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
        # cv2.putText(frame, "BLUE", (185, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
        # cv2.putText(frame, "GREEN", (298, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
        # cv2.putText(frame, "RED", (420, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2, cv2.LINE_AA)
        # cv2.putText(frame, "YELLOW", (520, 33), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (150,150,150), 2, cv2.LINE_AA)

        # Check to see if we have reached the end of the video
        if not grabbed:
            break

        img = colour_points( hsv, img, darkblueLower, darkblueUpper, bpoints, bindex, gpoints, gindex, rpoints, rindex, ypoints, yindex, 0)
        img = colour_points( hsv, img, greenLower, greenUpper, bpoints, bindex, gpoints, gindex, rpoints, rindex, ypoints, yindex, 1)
        img = colour_points( hsv, img, redLower, redUpper, bpoints, bindex, gpoints, gindex, rpoints, rindex, ypoints, yindex, 2)
        img = colour_points( hsv, img, yellowLower, yellowUpper, bpoints, bindex, gpoints, gindex, rpoints, rindex, ypoints, yindex, 3)
        # img = colour_points( hsv, img, whiteLower, whiteUpper, bpoints, bindex, gpoints, gindex, rpoints, rindex, ypoints, yindex, 4)

        # Determine which pixels fall within the blue boundaries and then blur the binary image
        # blueMask = cv2.inRange(hsv, blueLower, blueUpper)
        # blueMask = cv2.erode(blueMask, kernel, iterations=2)
        # blueMask = cv2.morphologyEx(blueMask, cv2.MORPH_OPEN, kernel)
        # blueMask = cv2.dilate(blueMask, kernel, iterations=1)

        # # Find contours in the image
        # (cnts, _) = cv2.findContours(blueMask.copy(), cv2.RETR_EXTERNAL,
        #     cv2.CHAIN_APPROX_SIMPLE)
        # center = None

        # # Check to see if any contours were found
        # if len(cnts) > 0:
        #     # Sort the contours and find the largest one -- we
        #     # will assume this contour correspondes to the area of the bottle cap
        #     cnt = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
        #     # Get the radius of the enclosing circle around the found contour
        #     ((x, y), radius) = cv2.minEnclosingCircle(cnt)
        #     # Draw the circle around the contour
        #     cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
        #     # cv2.circle(img, (int(x), int(y)), int(radius), (0, 255, 255), 2)

        #     # Get the moments to calculate the center of the contour (in this case Circle)
        #     M = cv2.moments(cnt)
        #     center = (int(M['m10'] / M['m00']), int(M['m01'] / M['m00']))

        #     if center[1] <= 65:
        #         if 40 <= center[0] <= 140: # Clear All
        #             bpoints = [deque(maxlen=512)]
        #             gpoints = [deque(maxlen=512)]
        #             rpoints = [deque(maxlen=512)]
        #             ypoints = [deque(maxlen=512)]

        #             bindex = 0
        #             gindex = 0
        #             rindex = 0
        #             yindex = 0

        #             paintWindow[67:,:,:] = 255
        #         elif 160 <= center[0] <= 255:
        #                 colorIndex = 0 # Blue
        #         elif 275 <= center[0] <= 370:
        #                 colorIndex = 1 # Green
        #         elif 390 <= center[0] <= 485:
        #                 colorIndex = 2 # Red
        #         elif 505 <= center[0] <= 600:
        #                 colorIndex = 3 # Yellow
        #     else :
        #         if colorIndex == 0:
        #             bpoints[bindex].appendleft(center)
        #         elif colorIndex == 1:
        #             gpoints[gindex].appendleft(center)
        #         elif colorIndex == 2:
        #             rpoints[rindex].appendleft(center)
        #         elif colorIndex == 3:
        #             ypoints[yindex].appendleft(center)
        # # Append the next deque when no contours are detected (i.e., bottle cap reversed)
        # else:
        #     bpoints.append(deque(maxlen=512))
        #     bindex += 1
        #     gpoints.append(deque(maxlen=512))
        #     gindex += 1
        #     rpoints.append(deque(maxlen=512))
        #     rindex += 1
        #     ypoints.append(deque(maxlen=512))
        #     yindex += 1

        # # Draw lines of all the colors (Blue, Green, Red and Yellow)
        # points = [bpoints, gpoints, rpoints, ypoints]
        # for i in range(len(points)):
        #     for j in range(len(points[i])):
        #         for k in range(1, len(points[i][j])):
        #             if points[i][j][k - 1] is None or points[i][j][k] is None:
        #                 continue
        #             cv2.line(frame, points[i][j][k - 1], points[i][j][k], colors[i], 2)
        #             cv2.line(img, points[i][j][k - 1], points[i][j][k], colors[i], 2)
        #             cv2.line(paintWindow, points[i][j][k - 1], points[i][j][k], colors[i], 2)

        # Show the frame and the paintWindow image
        cv2.namedWindow("Paint", cv2.WND_PROP_FULLSCREEN)
        cv2.setWindowProperty("Paint", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
        cv2.imshow("Paint",img)
        cv2.imshow("Tracking", frame)
        # cv2.imshow("Paint", paintWindow)

        # If the 'q' key is pressed, stop the loop

        k = cv2.waitKey(5) & 0xFF

        if k == ord('s'):
            img_no+=1
            # print("s", img_no)
            if img_no==len(path)+1:
                img_no=1
            img = colour_points( hsv, img, whiteLower, whiteUpper, bpoints, bindex, gpoints, gindex, rpoints, rindex, ypoints, yindex, 4)
            break

        if k == ord('a'):
            img_no-=1
            # print("a", img_no)
            if img_no==0:
                img_no=len(path)
            img = colour_points( hsv, img, whiteLower, whiteUpper, bpoints, bindex, gpoints, gindex, rpoints, rindex, ypoints, yindex, 4)
            break

        if k == ord('d'):
            img = colour_points( hsv, img, whiteLower, whiteUpper, bpoints, bindex, gpoints, gindex, rpoints, rindex, ypoints, yindex, 4)
            break

        if k == ord("q"):
            done = 1
            # print("q", img_no)
            # break
            break

        # if cv2.waitKey(1) & 0xFF == ord("q"):
        #     break

    if done:
        break

    # print(img_no)

# Cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()


import pandas as pd
import pickle
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import find_peaks
from scipy.fft import fft, fftfreq
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA

def fourier_classify(U, threshold=2, plot=False, title = 'plot'):

    # Round to 5sf avoid picking up noise.
    U = [np.array([np.format_float_positional(conc, precision=5, unique=False, fractional=False, trim='k') for conc in morphogen]) for morphogen in U]

    # Compute the fourier transforms.
    transforms = [fft(i) for i in U]

    # Check for peaks.
    x_peaks = [abs(i) > threshold for i in transforms[0][1:]]
    y_peaks = [abs(i) > threshold for i in transforms[1][1:]]

    # Peaks must be found in both species to return True.
    peaks_found = False
    if sum(x_peaks) > 0 and sum(y_peaks) > 0:
        peaks_found = True

    # Plot the fourier transforms.
    if plot:
        for i in [0, 1]:

            amplitudes = transforms[i]
            freq = fftfreq(100, 0.2)
            plt.plot(freq[1:], abs(amplitudes)[1:])
            plt.xlim(0, )
            plt.ylim(1,)
            plt.title(f'Prominent frequency: {freq[np.argmax(abs(amplitudes)[1:])]}')
            plt.show()

    return peaks_found

def rmNoise(results):

    filtered = {}
    for i in results:
        if fourier_classify(results[i]['concs']):
            filtered[i] = results[i]

    return(filtered)

##################### IMPORT DATA ############################

# These files are pre-filtered hits from the 10/03 analysis.

infile = open('1003_LSA_hits.pkl', 'rb')
LSA = pickle.load(infile)
infile.close()

infile = open('1003_growth_fourier.pkl', 'rb')
growth = pickle.load(infile)
infile.close()

infile = open('1003_nongrowth_fourier.pkl', 'rb')
nongrowth = pickle.load(infile)
infile.close()

# Parameter set 42.

infile = open('1003_Results_ParamSet42/parameters.pkl', 'rb')
params = pickle.load(infile)
infile.close()

########################## PCA ############################

# Remove noise.
# LSA = rmNoise(LSA)

# Make df with parameters as headings.
category = []
tp_params = []

for i in LSA:

    # Add tp_class.
    category.append(LSA[i]['LSA']['system_class'])

    # Add parameters to list of dictionaries.
    tp_params.append(params[(i[0], 0)][0])

df = pd.DataFrame(tp_params)

# Scale the data.
scaler = StandardScaler()
scaler.fit(df)
df = scaler.fit_transform(df)

# PCA with 2 components.
pca = PCA(n_components = 2)
df_new = pca.fit_transform(df)
principalDf = pd.DataFrame(data = df_new, columns = ['PC1', 'PC2'])
finalDf = pd.concat([principalDf, pd.DataFrame(category, columns = ['Turing_Class'])], axis = 1)

# Show how effective the PCA is at explaining variance.
print('Explained variance: ', pca.explained_variance_ratio_)

# Plot the PCA outcome.
fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)

targets = ['turing I', 'turing II', 'turing I oscillatory', 'hopf', 'turing I hopf', 'turing II hopf']
colors = ['r', 'g', 'b', 'c', 'm', 'y']
for target, color in zip(targets,colors):
    indicesToKeep = finalDf['Turing_Class'] == target
    ax.scatter(finalDf.loc[indicesToKeep, 'PC1'], finalDf.loc[indicesToKeep, 'PC2'], c = color, s = 50)
ax.legend(targets)
ax.grid()


#!/usr/bin/python

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import requests
from bs4 import BeautifulSoup as bs

url = "http://www.delhimetrorail.com/metro-fares.aspx"

response = requests.get(url)
webpage = response.content
soup = bs(webpage, "html.parser")
stations = soup.select("#ctl00_MainContent_ddlFrom")
print(stations[0].text.strip())

browser = webdriver.Firefox()
browser.get("http://www.delhimetrorail.com/metro-fares.aspx")

text = input("From Station : ") # Source Station
currentselection = browser.find_element_by_id("ctl00_MainContent_ddlFrom")
select = Select(currentselection)
select.select_by_visible_text(text)

text = input("To Station : ") # Destination Station
currentselection = browser.find_element_by_id("ctl00_MainContent_ddlTo")
select = Select(currentselection)
select.select_by_visible_text(text)

enter = browser.find_element_by_css_selector("#ctl00_MainContent_btnShowFare")
enter.click()


# -*- coding: utf-8 -*-
"""
Created on Thu Jul 19 15:02:12 2018

@author: Sara
"""
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import dash
from dash.dependencies import Output, Event
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
from collections import deque
from flask import Flask
from flask import render_template
from flask import request

dataFrame =[]
specialty=list()
rate=list()
def count (filepath,city):
    dataFrame=pd.read_csv(filepath)
    newdata=dataFrame[dataFrame.job_location == city]
    newdata = newdata[newdata.job_date == 2018]
    return newdata

new = count(r'C:\Users\sara\Desktop\jobDataset_All ver.2.csv', 'Makkah')
def encode(data,column):
   col= dict()
   for q in range(len(data)):
       x = data.iloc[q][column]
       if not data.iloc[q][column] in col:
           col.update({x:1})
       else:
            col[x]=col[x]+1
   return col

countt=encode(new,'job_specialty')
sorted_by_value = sorted(countt.items(), key=lambda kv: kv[1],reverse=True)
#print (sorted_by_value)
c = 0
for key, value in sorted_by_value:
    if c < 10:
        specialty.append(key)
        rate.append(value)
        c = c+1

#https://medium.com/python-pandemonium/data-visualization-in-python-bar-graph-in-matplotlib-f1738602e9c4
def plot_bar_x():
    # this is for plotting purpose
    index = np.arange(len(specialty))
    plt.bar(index, rate, color = 'cgmbyr')
    plt.xlabel('Jobs', fontsize=5)
    plt.ylabel('Growth rate', fontsize=5)
    plt.xticks(index, specialty, fontsize=10, rotation=90)
    plt.title('Trending jobs')
    plt.show()

plot_bar_x()
#https://www.youtube.com/watch?v=J_Cy_QjG6NE&index=1&list=PLQVvvaa0QuDfsGImWNt1eUEveHOepkjqt
app = dash.Dash(__name__)
app.layout = html.Div(children=[
            html.H1(children='Dash Tutorials'),
            dcc.Graph(id='example',
                      figure={
                              'data': [{'x': specialty, 'y': rate, 'type': 'bar', 'name': 'Cars'},],
                              'layout': {'title': 'Trending jobs'}
                              })
    ])

if __name__ == '__main__':
    app.run_server(port = 4996)


"""CRUD utils for RSS posts table"""

import time

from httpx import delete
from sqlalchemy import select, text
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.orm import Session

from app.models.rss_post_model import RSSPostModel
from app.models.rss_source_model import RSSSourceModel
from app.schemas.rss_post_schema import RSSPostBase, RSSPostCreate
from app.settings import settings

class RSSPostsMethods:
    def __init__(self) -> None:
        pass

    def get_all_posts(self, db: Session):
        return (
            db.query(RSSPostModel)
            .filter(RSSPostModel.blacklisted != True)
            .order_by(-RSSPostModel.publish_date)
            .all()
        )

    def get_post_by_id(self, db: Session, post_id: int):
        result = (
            db.execute(select(RSSPostModel).where(RSSPostModel.id == post_id))
            .scalars()
            .first()
        )

        return result

    def get_posts_by_source_id(self, db: Session, source_id: int):
        return db.query(RSSPostModel).filter(RSSPostModel.source_id == source_id).all()

    def get_source_by_post_id(self, db: Session, post_id: int):
        post = self.get_post_by_id(db, post_id)

        return (
            db.query(RSSSourceModel).filter(RSSSourceModel.id == post.source_id).first()
        )

    def create_post(self, db: Session, post: RSSPostCreate):
        db_post = RSSPostModel(**post.dict())

        db.add(db_post)
        db.commit()
        db.refresh(db_post)

        return db_post

    def create_posts(self, db: Session, posts: list[RSSPostCreate]):
        for post in posts:
            add_post = (
                insert(RSSPostModel).values(**post.dict()).on_conflict_do_nothing()
            )

            db.execute(add_post)

        db.commit()

        return

    def update_post(self, db: Session, post: RSSPostBase, post_id: int):
        db.query(RSSPostModel).filter(RSSPostModel.id == post_id).update(
            values=post.dict()
        )
        db.commit()

    def delete_post(self, db: Session, post_id: int):
        db.query(RSSPostModel).filter(RSSPostModel.id == post_id).delete()
        db.commit()

    def block_post(self, db: Session, post_id: int):
        db.query(RSSPostModel).filter(RSSPostModel.id == post_id).update(
            values={"blacklisted": True}
        )
        db.commit()

    def unblock_post(self, db: Session, post_id: int):
        db.query(RSSPostModel).filter(RSSPostModel.id == post_id).update(
            values={"blacklisted": False}
        )
        db.commit()

    def get_blacklisted_posts(self, db: Session):
        return db.query(RSSPostModel).filter(RSSPostModel.blacklisted == True).all()

    def clear_old_posts(self, db: Session):
        db.query(RSSPostModel).filter(
            RSSPostModel.publish_date
            < int(time.time()) - settings.BG_CLEANUP_INTERVAL_SECONDS
        ).delete()
        db.commit()

        posts = db.query(RSSPostModel).all()

        return len(posts)

rss_posts_methods = RSSPostsMethods()


import pygame
import os
import time
import random
pygame.font.init()

WIDTH, HEIGHT = 720, 720
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Chess - A2")

LIGHT_SQUARE = pygame.image.load(os.path.join("PicsA2", "light square.png"))
DARK_SQUARE = pygame.image.load(os.path.join("PicsA2", "dark square.png"))
HIGHLIGHTED_SQUARE = pygame.image.load(os.path.join("PicsA2", "highlighted square.png"))

LIGHT_PAWN = pygame.image.load(os.path.join("PicsA2", "light pawn.png"))
LIGHT_CASTLE = pygame.image.load(os.path.join("PicsA2", "light castle.png"))
LIGHT_KNIGHT = pygame.image.load(os.path.join("PicsA2", "light knight.png"))
LIGHT_BISHOP = pygame.image.load(os.path.join("PicsA2", "light bishop.png"))
LIGHT_QUEEN = pygame.image.load(os.path.join("PicsA2", "light queen.png"))
LIGHT_KING = pygame.image.load(os.path.join("PicsA2", "light king.png"))

DARK_PAWN = pygame.image.load(os.path.join("PicsA2", "dark pawn.png"))
DARK_CASTLE = pygame.image.load(os.path.join("PicsA2", "dark castle.png"))
DARK_KNIGHT = pygame.image.load(os.path.join("PicsA2", "dark knight.png"))
DARK_BISHOP = pygame.image.load(os.path.join("PicsA2", "dark bishop.png"))
DARK_QUEEN = pygame.image.load(os.path.join("PicsA2", "dark queen.png"))
DARK_KING = pygame.image.load(os.path.join("PicsA2", "dark king.png"))

class Button():
    def __init__(self, color, x,y,width,height, text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text

    def draw(self,win,outline=None):
        #Call this method to draw the button on the screen

        pygame.draw.rect(win, self.color, (self.x,self.y,self.width,self.height),0)

    def isOver(self, pos):
        #Pos is the mouse position or a tuple of (x,y) coordinates
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True

        return False

class Index():
    def__init__(self,x,y,button,piece)

class Piece:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.piece_img = None
    def draw(self, window):
        window.blit(self.piece_img, (self.x, self.y))

class Pawn(Piece):
    COLOUR_MAP = {
        "light": (LIGHT_PAWN),
        "dark": (DARK_PAWN)
        }
    def __init__(self, x, y, colour):
        super().__init__(x,y)
        self.piece_img = self.COLOUR_MAP[colour]

class Castle(Piece):
    COLOUR_MAP = {
        "light": (LIGHT_CASTLE),
        "dark": (DARK_CASTLE)
        }
    def __init__(self, x, y, colour):
        super().__init__(x,y)
        self.piece_img = self.COLOUR_MAP[colour]

class Knight(Piece):
    COLOUR_MAP = {
        "light": (LIGHT_KNIGHT),
        "dark": (DARK_KNIGHT)
        }
    def __init__(self, x, y, colour):
        super().__init__(x,y)
        self.piece_img = self.COLOUR_MAP[colour]

class Bishop(Piece):
    COLOUR_MAP = {
        "light": (LIGHT_BISHOP),
        "dark": (DARK_BISHOP)
        }
    def __init__(self, x, y, colour):
        super().__init__(x,y)
        self.piece_img = self.COLOUR_MAP[colour]

class Queen(Piece):
    COLOUR_MAP = {
        "light": (LIGHT_QUEEN),
        "dark": (DARK_QUEEN)
        }
    def __init__(self, x, y, colour):
        super().__init__(x,y)
        self.piece_img = self.COLOUR_MAP[colour]

class King(Piece):
    COLOUR_MAP = {
        "light": (LIGHT_KING),
        "dark": (DARK_KING)
        }
    def __init__(self, x, y, colour):
        super().__init__(x,y)
        self.piece_img = self.COLOUR_MAP[colour]

def background():
    scale = 90
    for x in range(0,4):
        for y in range (0,4):
            WIN.blit(LIGHT_SQUARE,(2*x*scale,2*y*scale))
    for x in range(1,5):
        for y in range (1,5):
            WIN.blit(LIGHT_SQUARE,(((2*x)-1)*scale,((2*y)-1)*scale))
    for x in range(1,5):
        for y in range (0,4):
            WIN.blit(DARK_SQUARE,((2*x-1)*scale,2*y*scale))
    for x in range(0,4):
        for y in range (1,5):
            WIN.blit(DARK_SQUARE,(2*x*scale,(2*y-1)*scale))
    pygame.display.update()

def set_up():
    home_pieces = []
    away_pieces = []

    COLOUR_MAP = {
        "light": (LIGHT_PAWN, LIGHT_CASTLE, LIGHT_KNIGHT, LIGHT_BISHOP, LIGHT_QUEEN, LIGHT_KING),
        "dark": (DARK_PAWN, DARK_CASTLE, DARK_KNIGHT, DARK_BISHOP, DARK_QUEEN, DARK_KING)
        }

    #randomsing player colour
    home_colour = random.choice(["light", "dark"])
    if home_colour == "light":
        away_colour = "dark"
    else:
        away_colour = "light"

#pawns
    for i in range (0,8):
        piece = Pawn(i*90, 6*90, home_colour)
        home_pieces.append(piece)
    for i in range (0,8):
        piece = Pawn(i*90, 1*90, away_colour)
        away_pieces.append(piece)
#backrow
    #Castles
    piece = Castle(0*90, 7*90, home_colour)
    home_pieces.append(piece)
    piece = Castle(7*90, 7*90, home_colour)
    home_pieces.append(piece)
    piece = Castle(0*90, 0*90, away_colour)
    away_pieces.append(piece)
    piece = Castle(7*90, 0*90, away_colour)
    away_pieces.append(piece)
    #Knights
    piece = Knight(1*90, 7*90, home_colour)
    home_pieces.append(piece)
    piece = Knight(6*90, 7*90, home_colour)
    home_pieces.append(piece)
    piece = Knight(1*90, 0*90, away_colour)
    away_pieces.append(piece)
    piece = Knight(6*90, 0*90, away_colour)
    away_pieces.append(piece)
    #Bishops
    piece = Bishop(2*90, 7*90, home_colour)
    home_pieces.append(piece)
    piece = Bishop(5*90, 7*90, home_colour)
    home_pieces.append(piece)
    piece = Bishop(2*90, 0*90, away_colour)
    away_pieces.append(piece)
    piece = Bishop(5*90, 0*90, away_colour)
    away_pieces.append(piece)
    #Queens
    piece = Queen(3*90, 7*90, home_colour)
    home_pieces.append(piece)
    piece = Queen(3*90, 0*90, away_colour)
    away_pieces.append(piece)
    #Kings
    piece = King(4*90, 7*90, home_colour)
    home_pieces.append(piece)
    piece = King(4*90, 0*90, away_colour)
    away_pieces.append(piece)

    for piece in home_pieces:
         piece.draw(WIN)
    for piece in away_pieces:
         piece.draw(WIN)

    pygame.display.update()
    print(away_pieces)

def main():

    background()
    set_up()

main()


from django.contrib import admin
from django.urls import path
from . import views

from Abonne.views import registration_view

urlpatterns = [

    path('niveaulist/', views.niveaulist, name="niveaulist"),
    path('niveaudetails/<int:pk>', views.niveaudetails, name="niveaudetails"),
    path('niveauadd/', views.niveauadd, name="niveauadd"),
    path('niveauUpdate/<int:pk>', views.niveauUpdate, name="niveauUpdate"),
    path('niveauDelete/<int:pk>', views.niveauDelete, name="niveauDelete"),

    # url matiere
    path('matierelist/', views.matierelist, name="matierelist"),
    path('matiereadd/', views.matiereCreate, name="matiereadd"),
    path('matiereUpdate/<int:pk>', views.matiereUpdate, name="matiereUpdate"),
    path('matiereDelete/<int:pk>', views.matiereDelete, name="matiereDelete"),

    # url chapitre
    path('listChapter/', views.ChapitreList, name="chapitres"),
    path('detailChapter/<str:pk>/', views.ChapitreDetail, name="details"),
    path('createChapter/', views.ChapterCreate, name="create"),
    path('updateChapter/<str:pk>/', views.ChapterUpdate, name="update"),
    path('deleteChapter/<str:pk>/', views.ChapterDelete, name="delete"),

    # url video
    path('listVideo/', views.VideoList, name="ListedVideo"),
    path('detailVideo/<str:pk>/', views.VideoDetail, name="detailsV"),
    path('createVideo/', views.VideoCreate, name="createV"),
    path('updateVideo/<str:pk>/', views.VideoUpdate, name="updateV"),
    path('deleteVideo/<str:pk>/', views.VideoDelete, name="deleteV"),

    # doc url
    path('listDoc/', views.DocList, name="ListedVideo"),
    path('detailDoc/<str:pk>/', views.DocDetail, name="detailsV"),
    path('createDoc/', views.DocCreate, name="createV"),
    path('updateDoc/<str:pk>/', views.DocUpdate, name="updateV"),
    path('deleteDoc/<str:pk>/', views.DocDelete, name="deleteV"),

    # register
    path('register/', registration_view, name="register")
]


from StereoConvNet_AK import StereoConvNet as scn
from PIL import Image
import numpy as np
import os

def load_image(infilename: object) -> object:
    img = Image.open(infilename).convert('L')
    img.load()

    data = np.asarray(img, dtype="int32")
    return data

def load_input_dataset(path) -> object:
    files = os.listdir(path)
    x = np.zeros((len(files), int(100), int(300)))
    for i in range(len(files)):
        # img = load_image(path + "/" + files[i])
        img = Image.open(path + "/" + files[i]).convert('L')
        img.load()
        img = img.resize((x.shape[2], x.shape[1]), Image.ANTIALIAS)
        data = np.asarray(img, dtype="int32")
        x[i, :, :] = data

    return x

def load_output_dataset(path) -> object:
    files = os.listdir(path)
    x = np.zeros((len(files), int(100), int(150)))
    for i in range(len(files)):

        img = Image.open(path + "/" + files[i]).convert('L')
        img.load()
        img = img.resize((x.shape[2], x.shape[1]), Image.ANTIALIAS)
        data = np.asarray(img, dtype="int32")
        x[i, :, :] = data

    return x

path = "../../Dataset/DepthMap_dataset-master/Stereo"
x = load_input_dataset(path)
X = np.zeros((x.shape[0], x.shape[1], int(x.shape[2] / 2), 2))
X[:, :, :, 0] = x[:, :x.shape[1], :int(x.shape[2] / 2)]
X[:, :, :, 1] = x[:, :x.shape[1], int(x.shape[2] / 2):]

path = "../../Dataset/DepthMap_dataset-master/Depth_map"
y = load_output_dataset(path)
Y = np.zeros((y.shape[0], y.shape[1], y.shape[2], 1))
Y[:, :, :, 0] = y[:, :, :]

model = scn.get_model()
model.compile(optimizer=scn.get_optimizer(), loss=scn.get_loss_function(), metrics=['accuracy'])
model.fit(X, Y, batch_size=scn.get_batch_size(), epochs=scn.get_num_epoch())

# serialize model to JSON
model_json = model.to_json()
with open("model_StereoConvNet_1.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model_StereoConvNet_1.h5")
print("Saved model to disk")