#import cv2
#import sys
#import pdb
#import pickle
import numpy as np
import matplotlib
matplotlib.use('PDF')
import matplotlib.pyplot as plt
#import glob
#import json

#import suite2p
#import suite2p as s2p
import os
from ScanImageTiffReader import ScanImageTiffReader

#import tools.dataAnalysis as dataAnalysis

class caImagingSuite2p:
    def __init__(self, analysisLoc, figureLoc, showI = False):
        #  "/media/HDnyc_data/data_analysis/in_vivo_cerebellum_walking/LocoRungsData/170606_f37/170606_f37_2017.07.12_001_behavingMLI_000_raw_behavior.avi"
        self.analysisLocation = analysisLoc
        self.figureLocation = figureLoc
        #self.f = ff
        self.showImages = showI

        #self.ops = np.load('tools/ops1.npy').item()

    ############################################################
    def __del__(self):
        print('suite2p : on exit')

    ############################################################
    def decideWhichTiffFilesToUse(self,dataDir,tiffFiles,recList,whichRecordings,defaultNumber=5):
        specificList = []
        #availableIdxs = []
        recsDict = {'all910':'recs910', 'all820':'recs820'}
        if whichRecordings == 'all910' or whichRecordings == 'all820':
            if 'CaImgs' in recList[recsDict[whichRecordings]]:
                print('Ca imaging files index exists in config file')
                #print(recList[recsDict[whichRecordings]]['CaImgs'])
                if type(recList[recsDict[whichRecordings]]['CaImgs'])==int:
                    fileIdxs = [recList[recsDict[whichRecordings]]['CaImgs']]
                else:
                    fileIdxs = recList[recsDict[whichRecordings]]['CaImgs']
                ll = []
                nn = ''
                mm = ''
                for n in fileIdxs :
                    ll.append(tiffFiles[n])
                    nn+= str(tiffFiles[n][-5:-4])
                    mm+= str(n)
                specificList.append([ll,nn,mm])
                return specificList
        if whichRecordings == 'all':
            for k in recsDict:
                if 'CaImgs' in recList[recsDict[k]]:
                    print('Ca imaging files index exists in config file')
                    # print(recList[recsDict[whichRecordings]]['CaImgs'])
                    if type(recList[recsDict[k]]['CaImgs']) == int:
                        fileIdxs = [recList[recsDict[k]]['CaImgs']]
                    else:
                        fileIdxs = recList[recsDict[k]]['CaImgs']
                    ll = []
                    nn = ''
                    mm = ''
                    for n in fileIdxs:
                        ll.append(tiffFiles[n])
                        nn += str(tiffFiles[n][-5:-4])
                        mm += str(n)
                    specificList.append([ll, nn, mm])
            return specificList
        # first show available tiff files with index
        print('List of available tiff files with index :')
        for n in range(len(tiffFiles)):
            print(' ',tiffFiles[n],n)
        # in case of the default number of tiff-files per recording, no hand-selection is required
        if len(tiffFiles)==defaultNumber:
            ll = []
            nn = ''
            mm = ''
            for n in range(len(tiffFiles)):
                ll.append(tiffFiles[n])
                nn+= str(tiffFiles[n][-5:-4])
                mm+=str(n)
            specificList.append([ll,nn,mm])
        else: # pick the lists by hand in case of more than the default number of tiff-files
            nn = ''
            listIdxs = input('Enter indices of the tiff files to be analyzed together separated by a space, different lists are separated by coma (e.g. 1 2 3 4 5, 6):')   # Python 3
            separateLists = listIdxs.split(',')
            for i in range(len(separateLists)):
                sl = [int(s) for s in separateLists[i].split()]
                ll = []
                nn = ''
                mm = ''
                for j in sl:
                    ll.append(tiffFiles[j])
                    nn+= str(tiffFiles[j][-5:-4])
                    mm+= str(j)
                specificList.append([ll,nn])
        return specificList
    ############################################################
    def runSuite2pPipeline(self,pythonPath,dataDir,saveDir,tiffPaths=None):
        #fName = dataDir + '*.tif'
        #tiffsList = glob.glob(fName)
        # only run on specified tiffs
        os.system(pythonPath + ' tools/runSuite2p.py %s %s ' % (dataDir, saveDir) + ' '.join(tiffPaths))

    ############################################################
    def generateOverviewFigure(self,suite2pPath,tiffList,mouseID,recFolder):
        ops = np.load(suite2pPath+'suite2p/plane0/ops.npy',allow_pickle=True).item()

        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.set_title('animal %s, rec: %s' % (mouseID,recFolder))
        ax.imshow(ops['meanImg'],vmax=max(ops['meanImg'].flatten())*0.3)

        plt.savefig(suite2pPath + 'animal_%s_rec_%s.pdf' %(mouseID,recFolder))

    ############################################################
    def generateSummaryFigures(self):
        pass


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import division
from __future__ import print_function
from __future__ import absolute_import

from hamcrest import is_
from hamcrest import none
from hamcrest import not_none
from hamcrest import assert_that
from hamcrest import instance_of

from nose.tools import assert_raises

import os
import tempfile
import unittest

from sqlalchemy import create_engine

from nti.monkey.patch_sqlalchemy_on_import import geventMysqlclient_dialect
from nti.monkey.patch_sqlalchemy_on_import import geventSqliteclient_dialect

class TestPatch(unittest.TestCase):

    def test_mysql_patch(self):
        from nti.monkey.patch_sqlalchemy_on_import import patch
        patch()
        engine = create_engine('gevent+mysql:///testdb.db')
        assert_that(engine, not_none())
        assert_that(engine.dialect, instance_of(geventMysqlclient_dialect))

    def test_sqlite_patch(self):
        from nti.monkey.patch_sqlalchemy_on_import import patch
        patch()
        engine = create_engine('gevent+sqlite:///testdb.db')
        assert_that(engine, not_none())
        assert_that(engine.dialect, instance_of(geventSqliteclient_dialect))

        con = engine.connect()
        sqlite_con = con.connection.connection
        assert_that(sqlite_con.isolation_level, none())
        rows = sqlite_con.execute('pragma journal_mode').fetchall()
        mode, = rows[0]
        assert_that(mode, is_('wal'))

    def test_sqlalchemy_retry(self):
        from nti.monkey.patch_sqlalchemy_on_import import patch
        patch()

        from zope.sqlalchemy.datamanager import SessionDataManager
        import transaction
        import sqlalchemy.exc

        class MockSqlTransaction(object):

            def _iterate_parents(self):
                return [transaction.get()]

        class MockSession(object):

            @property
            def transaction(self):
                return MockSqlTransaction()

            def close(self):
                pass

        def do_test(root_exception, is_retryable=True):
            # This joins the session to the transaction manager
            manager = SessionDataManager(MockSession(),
                                         'status',
                                         transaction.manager)

            sql_exc = sqlalchemy.exc.IntegrityError('statement', 'params', root_exception)
            assert_that(transaction.manager.get().isRetryableError(sql_exc),
                        is_(is_retryable))

            transaction.abort()
            del manager

            # Without being in a transaction, the manager gives us False
            assert_that(transaction.manager.get().isRetryableError(sql_exc),
                        is_(False))

        from MySQLdb import IntegrityError
        retryable_exc = IntegrityError(1062, 'Duplicate error')
        do_test(retryable_exc)
        nonretryable_exc = IntegrityError(9999, 'Duplicate error')
        do_test(nonretryable_exc, is_retryable=False)

        # Sqlite
        import sqlite3
        from sqlite3 import OperationalError
        from sqlite3 import IntegrityError as sqlite_IntegrityError
        db_file = tempfile.mkstemp()[1]
        con1 = sqlite3.connect(db_file)
        con2 = sqlite3.connect(db_file)
        con2.execute('PRAGMA busy_timeout=1')
        try:
            con1.execute('CREATE TABLE TestRetry(id TEXT UNIQUE)')
            con1.execute('INSERT INTO TestRetry (id) VALUES ("key_val")')

            # Duplicate entry raises IntegrityError
            with assert_raises(sqlite_IntegrityError) as exception_context:
                con1.execute('INSERT INTO TestRetry (id) VALUES ("key_val")')
            retryable_exc = exception_context.exception
            do_test(retryable_exc)

            # Second connection attempts to execute while con1 has the lock
            with assert_raises(OperationalError) as exception_context:
                con2.execute('INSERT INTO TestRetry (id) VALUES ("key_val")')
            retryable_exc = exception_context.exception
            do_test(retryable_exc)
        finally:
            con1.close()
            con2.close()
            os.remove(db_file)

        nonretryable_exc = sqlite_IntegrityError('This is not retryable')
        do_test(nonretryable_exc, is_retryable=False)

        nonretryable_exc = OperationalError('This is not retryable')
        do_test(nonretryable_exc, is_retryable=False)


#!/usr/bin/env python
# coding: utf-8

import json
import torch
from torch.utils.data import Dataset
import numpy as np
import pickle
from torch.utils.data import Dataset,DataLoader
import torchvision.transforms as transforms
from scipy.spatial import cKDTree
import collections

root = './'    #根目录
pix3d = 'data/pix3d/train_test_data/'    #Pix3d数据目录
mod = 'train'

HEIGHT_PATCH = 256
WIDTH_PATCH = 256
pix3d_n_classes = 9
neighbors = 30
default_collate = torch.utils.data.dataloader.default_collate

non_trans = transforms.Compose([
    transforms.ToPILImage(),
    transforms.Resize((HEIGHT_PATCH, WIDTH_PATCH)),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

trans = transforms.Compose([
    transforms.ToPILImage(),
    transforms.Resize((280, 280)),
    transforms.RandomCrop((HEIGHT_PATCH, WIDTH_PATCH)),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

class PixDataset(Dataset):
    def __init__(self,root_path = root, pix3d_path = pix3d, mode = mod, transform = None):
        '''
        self.root_path:    Get the data_path
        self.sunrgbd_path = pix3d_path:    Get the Sunrgbd dataset path
        self.file_idx = []:    Get the file index, 但这里由于其train dataset 和 test dataset 选取数据非常混乱，因此直接load .json文件
        '''
        self.root_path = root_path
        self.pix3d_path = pix3d_path
        self.transform = transform

        if (mode == 'train'):
            self.transform = trans
            with open(self.root_path + 'data/pix3d/splits/train.json') as json_file:
                self.file_idx = json.load(json_file)
        elif (mode == 'test'):
            self.transform = non_trans
            with open(self.root_path + 'data/pix3d/splits/test.json') as json_file:
                self.file_idx = json.load(json_file)

    def __len__(self):
        return len(self.file_idx)

    def __getitem__(self,idx):
        '''
        dict_keys: 'class_id', 'gt_3dpoints', 'sample_id', 'img'

        In the keys, also dicts:

        class_id:    图像类别
        sample_id:    图像id，图像是数据集中第几个，那么id就是几
        Gt_3dpoints:   为10000个三维坐标数据，是一个二维列表[[x,y,z],…,[x',y',z']].
        img:    图像,三维列表 H*W*channel, 但每张图片大小并不相同.

        '''
        data_pkl = pickle.load(open(self.root_path + self.file_idx[idx][2:],'rb'))
        data_img = data_pkl['img']

        gt_points = data_pkl['gt_3dpoints']
        gt_points = torch.from_numpy(gt_points)

        data_class = data_pkl['class_id']

        cls_codes = torch.zeros(pix3d_n_classes)
        cls_codes[data_class] = 1

        '''

        dists是到最近邻居的距离，indices是每个邻居的索引，两者大小均为 10000*30
        离每一个3d_points最近的是它自己，因此dists[index][0] = 0.0, 因此计算密度是用dists[index][1]
        '''
        tree = cKDTree(gt_points)    #提高空间索引速度
        dists, indices = tree.query(gt_points, k=neighbors)
        densities = np.array([max(dists[point_set, 1]) ** 2 for point_set in indices])
        densities = torch.from_numpy(densities)
        '''
        Output:
            为每一个数据生成一个Sample,Sample中keys(): sequence_id, img, cls mesh_poings, densities;
            sequence_id: 数据在数据集中的序号，
            img: shape [batch_size, 3, 256, 256]
            cls: shape [batch_size, pix3d_n_classes] 独热向量, pix3d_n_classes = 9.
            mesh_points

        '''

        if self.transform is None:
            sample = {'sequence_id':data_pkl['sample_id'],
                      'img':data_img,
                      'cls':cls_codes,
                      'mesh_points':gt_points,
                      'densities': densities}
            return sample
        else:
            data_raw = data_img
            data_img = self.transform(data_img)
            sample = {'sequence_id':data_pkl['sample_id'],
                      'img':data_img,
                      'cls':cls_codes,
                      'mesh_points':gt_points,
                      'densities': densities}
            return sample

def recursive_convert_to_torch(elem):
    if torch.is_tensor(elem):
        return elem
    elif type(elem).__module__ == 'numpy':
        if elem.size == 0:
            return torch.zeros(elem.shape).type(torch.DoubleTensor)
        else:
            return torch.from_numpy(elem)
    elif isinstance(elem, int):
        return torch.LongTensor([elem])
    elif isinstance(elem, float):
        return torch.DoubleTensor([elem])
    elif isinstance(elem, collections.Mapping):
        return {key: recursive_convert_to_torch(elem[key]) for key in elem}
    elif isinstance(elem, collections.Sequence):
        return [recursive_convert_to_torch(samples) for samples in elem]
    else:
        return elem

def collate_fn(batch):
    """
    Data collater.

    Assumes each instance is a dict.
    Applies different collation rules for each field.
    Args:
        batches: List of loaded elements via Dataset.__getitem__
    """
    collated_batch = {}
    # iterate over keys
    for key in batch[0]:
        collated_batch[key] = default_collate([elem[key] for elem in batch])

    return collated_batch

def Pix3d_dataloader(mode=mod, batch_size = 1):
    dataloader = DataLoader(dataset = PixDataset(root_path= root,pix3d_path=pix3d,mode = mode, transform=trans),
                            shuffle=True,
                            batch_size=batch_size,
                            collate_fn=collate_fn)
    return dataloader

if __name__ == '__main__':
    dataset = PixDataset(transform=trans, root_path=root, pix3d_path=pix3d, mode=mod)
    dataloader = Pix3d_dataloader(mode = mod, batch_size=1)


#!/usr/bin/env python
from __future__ import print_function

import argparse
import json
import time

import chainer
import chainer.functions as F
import chainer.links as L
from chainer import training
from chainer.training import extensions

import chainermn
import echainer
from echainer import MetaCommunicator, ReformHandler, ClusterUpdatedException, CommException
from echainer.policies import MinMaxPolicy, MinMaxPolicy2

class MLP(chainer.Chain):

    def __init__(self, n_units, n_out):
        super(MLP, self).__init__(
            # the size of the inputs to each layer will be inferred
            l1=L.Linear(784, n_units),  # n_in -> n_units
            l2=L.Linear(n_units, n_units),  # n_units -> n_units
            l3=L.Linear(n_units, n_out),  # n_units -> n_out
        )

    def __call__(self, x):
        h1 = F.relu(self.l1(x))
        h2 = F.relu(self.l2(h1))
        return self.l3(h2)

def main():
    parser = argparse.ArgumentParser(description='ChainerMN example: MNIST')
    parser.add_argument('--batchsize', '-b', type=int, default=100,
                        help='Number of images in each mini-batch')
    parser.add_argument('--epoch', '-e', type=int, default=20,
                        help='Number of sweeps over the dataset to train')
    parser.add_argument('--gpu', '-g', action='store_true',
                        help='Use GPU')
    parser.add_argument('--out', '-o', default='result',
                        help='Directory to output the result')
    parser.add_argument('--resume', '-r', default='',
                        help='Resume the training from snapshot')
    parser.add_argument('--unit', '-u', type=int, default=1000,
                        help='Number of units')
    parser.add_argument('--np', '-n', type=int, required=True,
                        help='Minimum number of processes')
    parser.add_argument('--bind', '-p', type=str, required=True,
                        help='address to bind gRPC server')
    parser.add_argument('--etcd', '-c', type=str,
                        default='etcd://127.0.0.1:2379/train_mnist.py',
                        help='etcd location and path')
    args = parser.parse_args()

    # Prepare ChainerMN communicator.
    n = args.np
    bind = args.bind
    scale_policy = MinMaxPolicy(n, n, block=True)
    comm = None
    if args.gpu:
        from echainer import NcclCommunicator
        comm = NcclCommunicator(policy=scale_policy, bind=bind,
                                etcd=args.etcd)
    else:
        comm = MetaCommunicator(policy=scale_policy, bind=bind,
                                etcd=args.etcd)

    late = not comm.initial

    if comm.rank == 0:
        print('==========================================')
        print('Num process (COMM_WORLD): {}'.format(comm.size))
        if args.gpu:
            print('Using GPU ', comm.intra_rank)
        print('Num unit: {}'.format(args.unit))
        print('Num Minibatch-size: {}'.format(args.batchsize))
        print('Num epoch: {}'.format(args.epoch))
        print('==========================================')

    device = -1
    model = L.Classifier(MLP(args.unit, 10))
    if args.gpu:
        device = comm.intra_rank
        model.to_gpu(device=device)
    print('Using GPU ', device)

    # Create a multi node optimizer from a standard Chainer optimizer.
    optimizer = chainermn.create_multi_node_optimizer(
        chainer.optimizers.Adam(), comm)
    optimizer.setup(model)
    done = False
    retry = False
    while not done:
        if args.gpu and retry:
            device = comm.intra_rank
            print('Using GPU No.', comm.intra_rank)
            model.to_gpu(device=device)

            optimizer = chainermn.create_multi_node_optimizer(
                chainer.optimizers.Adam(), comm)
            optimizer.setup(model)

        # Split and distribute the dataset. Only worker 0 loads the whole dataset.
        # Datasets of worker 0 are evenly split and distributed to all workers.
        print('get dataset')
        if comm.rank == 0:
            train, test = chainer.datasets.get_mnist()
        else:
            train, test = None, None

        print('scatter dataset')
        train = chainermn.scatter_dataset(train, comm, shuffle=True)
        test = chainermn.scatter_dataset(test, comm, shuffle=True)

        print('create iterator')
        train_iter = chainer.iterators.SerialIterator(train, args.batchsize)
        test_iter = chainer.iterators.SerialIterator(test, args.batchsize,
                                                     repeat=False, shuffle=False)

        updater = training.StandardUpdater(train_iter, optimizer, device=device)
        trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)

        # Create a multi node evaluator from a standard Chainer evaluator.
        evaluator = extensions.Evaluator(test_iter, model, device=device)
        evaluator = chainermn.create_multi_node_evaluator(evaluator, comm)
        trainer.extend(evaluator)

        trainer.extend(ReformHandler())
        trainer.extend(comm.get_monitor())
        trainer.extend(comm.get_uninitializer(), trigger=(1, 'iteration'))

        # Some display and output extensions are necessary only for one worker.
        # (Otherwise, there would just be repeated outputs.)
        if comm.rank == 0:
            trainer.extend(extensions.dump_graph('main/loss'))
            trainer.extend(echainer.extension.Lineage(comm))
            trainer.extend(extensions.PrintReport(
                ['epoch', 'main/loss', 'validation/main/loss',
                 'main/accuracy', 'validation/main/accuracy', 'elapsed_time'],
                log_report='Lineage'))
            trainer.extend(extensions.ProgressBar())

        # Register extension to save trainer's progress (iteration) in communicator
        # trainer.extend(comm.get_progress_updater())

        if args.resume:
            chainer.serializers.load_npz(args.resume, trainer)

        # Optimizer includes model parameters and other params in optimizer
        comm.register_state('optimizer', optimizer)
        comm.register_state('model', model)
        # Iterators: Well if number of nodes changed then current
        # position becomes wrong but try to recover That's why
        # recoevering iterators are nonsense.
        # Trainer: Too large and it includes Iterators.
        print(updater.epoch, updater.iteration)

        if retry or late:
            (iteration, epoch) = comm.fetch_state('optimizer', optimizer)
            (iteration, epoch) = comm.fetch_state('model', model)
            train_iter.epoch = epoch
            updater.iteration = iteration

        optimizers = trainer.updater.get_all_optimizers()
        # bcast again anyway
        for name in optimizers.keys():
            optimizers[name].reset_prev_params()

        try:
            print('start trainer.run(), ', trainer.updater.iteration, trainer.updater.epoch)
            trainer.run()
            done = trainer._done
        except CommException as ce:
            print(">>>>>>>>>>>", ce, updater.iteration, updater.epoch)
            comm.save_all_states(updater.iteration, updater.epoch)
            # Here comm will be ready to accept fetch state calls and once all
            # nodes got catched up it'll return and continue to run: TODO
            comm.sync_cluster(trainer.updater.get_all_optimizers())
            retry = True
            continue
        except ClusterUpdatedException as ce:
            print(">>>>>>>>>>>", ce)
            comm.save_all_states(updater.iteration, updater.epoch)
            comm.sync_cluster(trainer.updater.get_all_optimizers())
            retry = True
            continue
        except Exception as e:
            print("Unexpected >>>>>>>>>>>", e)
            break

    # TODO: this should be called cleanly, unless it runs forever somehow...
    comm.leave()

if __name__ == '__main__':
    main()


class Person:
        def __init__(self):
                self._phoneNumber = "111-111-1111"
                self.__birthDate = "9/9/2020"

        def getbirthDate(self):
                print(self.__birthDate)

        def setbirthDate(self, private):
                self.__birthDate = private

obj = Person()
obj._phoneNumber = "222-222-2222"
print(obj._phoneNumber)         # Can change a Protected variable outside of the class.
obj.getbirthDate()
obj.setbirthDate("10/10/2020")  # Have to SET the method then GET the method to change Private variable ouside of class.
obj.getbirthDate()              # Private date gets changed to 10/10/2020.
obj.__birthDate = "123"         # Note: If try to change date outside of class it won't work.
obj.getbirthDate()              # Still 10/10/2020.


import pyds
import numpy as np

from gi.repository import Gst

from common.grpc_client import GrpcClient
from common.stupid_global_var import source2device, device2source

class ProbeBase:
    def __init__(self, config_dir) -> None:
        self.config_dir = config_dir

    def tiler_src_pad_buffer_probe(self, pad, info, u_data):
        """
        tiler_sink_pad_buffer_probe  will extract metadata received on OSD sink pad
        and update params for drawing rectangle, object information etc.
        """

        self.gst_buffer = info.get_buffer()
        if not self.gst_buffer:
            print("Unable to get GstBuffer ")
            return

        # Retrieve batch metadata from the gst_buffer
        # Note that pyds.gst_buffer_get_nvds_batch_meta() expects the
        # C address of gst_buffer as input, which is obtained with hash(gst_buffer)
        batch_meta = pyds.gst_buffer_get_nvds_batch_meta(hash(self.gst_buffer))

        l_frame = batch_meta.frame_meta_list

        while l_frame is not None:
            try:
                # Note that l_frame.data needs a cast to pyds.NvDsFrameMeta
                # The casting is done by pyds.NvDsFrameMeta.cast()
                # The casting also keeps ownership of the underlying memory
                # in the C code, so the Python garbage collector will leave
                # it alone.
                frame_meta = pyds.NvDsFrameMeta.cast(l_frame.data)
            except StopIteration:
                break

            l_obj = frame_meta.obj_meta_list

            object_list = []

            # Getting Image data using nvbufsurface
            # the input should be address of buffer and batch_id
            # convert python array into numy array format.
            n_frame = pyds.get_nvds_buf_surface(hash(self.gst_buffer), frame_meta.batch_id)
            frame_image = np.array(n_frame, copy=True, order='C')
            frame_meta_with_image = {"image": frame_image,
                                     "pad_index": frame_meta.pad_index,
                                     "source_id": frame_meta.source_id,
                                     "frame_num": frame_meta.frame_num}
            while l_obj is not None:
                try:
                    # Casting l_obj.data to pyds.NvDsObjectMeta
                    obj_meta = pyds.NvDsObjectMeta.cast(l_obj.data)
                    object_list.append(obj_meta)
                    l_obj = l_obj.next

                except StopIteration:
                    break

            self.frame_postprocess(pad, frame_meta_with_image, object_list)

            try:
                l_frame = l_frame.next
            except StopIteration:
                break

        return Gst.PadProbeReturn.OK

    def frame_postprocess(self, pad, frame_meta, object_list):
        """
        object.class_id
        object.confidence
        object.rect_params
        """
        pass

    def grpc_client_init(self, grpc_address="0.0.0.0:1111"):
        self.grpc_client = GrpcClient(grpc_address)

    def send_msg(self, frame, source_id):
        try:
            self.grpc_client.send_image(frame, device_id=source2device[source_id])
            print("send image. device_id= ", source2device[source_id])

        except Exception as e:
            print("grpc failed: ", e)


# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
# This file does only contain a selection of the most common options. For a
# full list see the documentation:
# http://www.sphinx-doc.org/en/master/config

import os

# -- Project information -----------------------------------------------------

project = 'kt.testing'
copyright = '2020, Keeper Technology LLC'
author = 'Keeper Technology'

# The full version, including alpha/beta/rc tags
release = os.environ.get('KT_VERSION') or ''
version = release or '(development)'

# -- General configuration ---------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.autodoc.typehints',
    'sphinx.ext.intersphinx',
]

intersphinx_mapping = {
    'python': (
        'https://docs.python.org/3', None),
    'zope.exceptions': (
        'https://zopeexceptions.readthedocs.io/en/latest', None),
    'zope.component': (
        'https://zopecomponent.readthedocs.io/en/latest', None),
    'zope.interface': (
        'https://zopeinterface.readthedocs.io/en/latest', None),
    'zope.schema': (
        'https://zopeschema.readthedocs.io/en/latest', None),
}

# Add any paths that contain templates here, relative to this directory.
templates_path = []

source_suffix = '.rst'
master_doc = 'index'
language = None
exclude_patterns = []

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = None

# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#
html_theme_options = {
    # 'issues_url': 'https://github.com/keepertech/kt.testing/issues/',
}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = []

html_last_updated_fmt = ''

# -- Options for HTMLHelp output ---------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'kt-testing'

# -- Options for LaTeX output ------------------------------------------------

latex_elements = {
    # The paper size ('letterpaper' or 'a4paper').
    #
    # 'papersize': 'letterpaper',

    # The font size ('10pt', '11pt' or '12pt').
    #
    # 'pointsize': '10pt',

    # Additional stuff for the LaTeX preamble.
    #
    # 'preamble': '',

    # Latex figure (float) alignment
    #
    # 'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    (master_doc, 'kt-testing.tex', 'kt.testing Documentation',
     'Keeper Technology', 'manual'),
]

autodoc_default_options = {
    'members': True,
    'inherited-members': True,
    'special-members': '__init__',
    # 'show-inheritance': False,
}

autoclass_content = 'class'
autodoc_member_order = 'bysource'
autodoc_typehints = 'description'

# Bibliographic Dublin Core info.
epub_title = 'kt.testing'

# A list of files that should not be packed into the epub file.
epub_exclude_files = ['search.html']


# LC 1335. Minimum Difficulty of a Job Schedule

'''
You want to schedule a list of jobs in d days. Jobs are dependent (i.e To work on the ith job, you have to finish all the jobs j where 0 <= j < i).

You have to finish at least one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the d days. The difficulty of a day is the maximum difficulty of a job done on that day.

You are given an integer array jobDifficulty and an integer d. The difficulty of the ith job is jobDifficulty[i].

Return the minimum difficulty of a job schedule. If you cannot find a schedule for the jobs return -1.

Example 1:

Input: jobDifficulty = [6,5,4,3,2,1], d = 2
Output: 7
Explanation: First day you can finish the first 5 jobs, total difficulty = 6.
Second day you can finish the last job, total difficulty = 1.
The difficulty of the schedule = 6 + 1 = 7

Example 2:

Input: jobDifficulty = [9,9,9], d = 4
Output: -1
Explanation: If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs.

Example 3:

Input: jobDifficulty = [1,1,1], d = 3
Output: 3
Explanation: The schedule is one job per day. total difficulty will be 3.

Constraints:

1 <= jobDifficulty.length <= 300
0 <= jobDifficulty[i] <= 1000
1 <= d <= 10
'''

class Solution:
    def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
        return self.bottomUp(jobDifficulty, d)

    def bottomUp(self, jobDifficulty: List[int], d: int) -> int:
        n = len(jobDifficulty)

        if n < d:
            return -1

        dp = [[float("inf")] * (d + 1) for _ in range(n)]

        dp[-1][d] = jobDifficulty[-1]

        for i in range(n - 2, -1, -1):
            dp[i][d] = max(dp[i+1][d], jobDifficulty[i])

        for day in range(d - 1, 0, -1):
            for i in range(day - 1, n - (d - day)):
                hardest = 0

                for j in range(i, n - (d - day)):
                    hardest = max(hardest, jobDifficulty[j])
                    dp[i][day] = min(dp[i][day], hardest + dp[j + 1][day + 1])

        return dp[0][1]

    def topDown(self, jobDifficulty: List[int], d: int) -> int:
        n = len(jobDifficulty)

        if n < d:
            return -1

        hardestJobRem = [0] * n
        hardest = 0

        for i in range(n-1,-1,-1):
            hardest = max(hardest, jobDifficulty[i])
            hardestJobRem[i] = hardest

        @lru_cache(None)
        def dp(i, day):
            if day == d:
                return hardestJobRem[i]

            minDifficulty = float("inf")
            hardest = 0
            for j in range(i, n - (d - day)):
                hardest = max(hardest, jobDifficulty[j])
                minDifficulty = min(minDifficulty, hardest + dp(j+1, day+1))

            return minDifficulty

        return dp(0, 1)

1. Problems/j. DP/b. 2D - Minimum Difficulty of a Job Schedule.py


import networkx as nx
import numpy as np
import utilities.const as const
import utilities.graph_utils as g_util

def initialize_x(n):
    #The state of the Mr X is defined by number of tokens and node position.
    x = np.array([0,0,0,0])
    x[0] = n
    x[1] = const.start_taxi_x
    x[2] = const.start_bus_x
    x[3] = const.start_underground_x
    return np.copy(x) #Returns an explicit copy and not a pointer.

def move_x(x,target_node,mode):
    #x is the list
    #target_node is the node number
    #mode is a 1-D array of length 3, [taxi,bus,underground] - 1 hot encoded

    if mode[0] == 1:
        x[1] = x[1] - 1
        x[0] = target_node
        return np.copy(x)
    elif mode[1] == 1:
        x[2] = x[2] - 1
        x[0] = target_node
        return np.copy(x)
    elif mode[2] == 1:
        x[3] = x[3] - 1
        x[0] = target_node
        return np.copy(x)
    return np.copy(x)

def x_valid_list(MRx,G):

    current_node = MRx[0]
    taxi = MRx[1]
    bus = MRx[2]
    underground = MRx[3]

    edges = g_util.connections(G,current_node)

    x_valid_list = []

    for edge in edges:
        if edge[2] == 1 and taxi > 0:
            x_valid_list.append(edge)
        elif edge[3] == 1 and bus > 0:
            x_valid_list.append(edge)
        elif edge[4] == 1 and underground > 0:
            x_valid_list.append(edge)

    return np.array(x_valid_list)


import sys
def printPairs(arr, arr_size, sum):

        s = set()
        res = [0,10000000000]

        for i in range(0,arr_size):
            temp = sum-arr[i]
            if (temp>=0 and temp in s):
                if(res[1]-res[0] >= abs(arr[i]-temp)):
                    res[1] = max(arr[i],temp)
                    res[0] = min(arr[i],temp)
            s.add(arr[i])
        return res

for line in sys.stdin:
    for N in line.split():
        N = int(N)
        cost = [int(i) for i in input().split()]
        Money = int(input())

        result = printPairs(cost,N,Money)
        print('Mohammad should buy books whose prices are '+str(result[0])+' and '+str(result[1])+'.\n')


"""
QAOA
====

Here we reproduce the figure in our preprint `arXiv:2212.11975 <https://arxiv.org/abs/2212.11975>`_.

You can run this code on your browser with `mybinder.org <https://mybinder.org/>`_ - click the badge below.

.. image:: https://mybinder.org/badge_logo.svg
 :target: https://mybinder.org/v2/gh/TeamGraphix/graphix-examples/HEAD?labpath=qaoa.ipynb

"""

from graphix import Circuit
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

n = 4
xi = np.random.rand(6)
theta = np.random.rand(4)
g = nx.complete_graph(n)
circuit = Circuit(n)
for i, (u, v) in enumerate(g.edges):
    circuit.cnot(u, v)
    circuit.rz(v, xi[i])
    circuit.cnot(u, v)
for v in g.nodes:
    circuit.rx(v, theta[v])

#%%
# transpile and get the graph state

pattern = circuit.transpile()
pattern.standardize()
pattern.shift_signals()
nodes, edges = pattern.get_graph()
g = nx.Graph()
g.add_nodes_from(nodes)
g.add_edges_from(edges)

#%%
# script to specify graph node positions and colors.
#
# We work out how to place nodes on the plot, by using the flow-finding algorithm.

from graphix.gflow import flow

f, l_k = flow(g, {0, 1, 2, 3}, set(pattern.output_nodes))

flow = [[i] for i in range(4)]
for i in range(4):
    contd = True
    val = i
    while contd:
        try:
            val = f[val]
            flow[i].append(val)
        except KeyError:
            contd = False
longest = np.max([len(flow[i]) for i in range(4)])

pos = dict()
for i in range(4):
    length = len(flow[i])
    fac = longest / (length - 1)
    for j in range(len(flow[i])):
        pos[flow[i][j]] = (fac * j, -i)

# determine wheher or not a node will be measured in Pauli basis
def get_clr_list(pattern):
    nodes, edges = pattern.get_graph()
    meas_list = pattern.get_measurement_commands()
    g = nx.Graph()
    g.add_nodes_from(nodes)
    g.add_edges_from(edges)
    clr_list = []
    for i in g.nodes:
        for cmd in meas_list:
            if cmd[1] == i:
                if cmd[3] in [-1, -0.5, 0, 0.5, 1]:
                    clr_list.append([0.5, 0.5, 0.5])
                else:
                    clr_list.append([1, 1, 1])
        if i in pattern.output_nodes:
            clr_list.append([0.8, 0.8, 0.8])
    return clr_list

graph_params = {"with_labels": False, "node_size": 150, "node_color": get_clr_list(pattern), "edgecolors": "k"}
nx.draw(g, pos=pos, **graph_params)

#%%
# perform Pauli measurements and plot the new (minimal) graph to perform the same quantum computation

pattern.perform_pauli_measurements()
nodes, edges = pattern.get_graph()
g = nx.Graph()
g.add_nodes_from(nodes)
g.add_edges_from(edges)
graph_params = {"with_labels": False, "node_size": 150, "node_color": get_clr_list(pattern), "edgecolors": "k"}
pos = { # hand-typed for better look
    40: (0, 0),
    5: (0, -1),
    11: (0, -2),
    17: (0, -3),
    23: (1, -2),
    29: (1, -3),
    35: (2, -3),
    42: (3, -1),
    44: (3, -2),
    46: (3, -3),
    41: (4, 0),
    43: (4, -1),
    45: (4, -2),
    47: (4, -3),
}
nx.draw(g, pos=pos, **graph_params)

#%%
# finally, simulate the QAOA circuit

out_state = pattern.simulate_pattern()
state = circuit.simulate_statevector()
print("overlap of states: ", np.abs(np.dot(state.psi.flatten().conjugate(), out_state.psi.flatten())))
# sphinx_gallery_thumbnail_number = 2


def get_right(point, gear_idx):
	right_idx = (point[gear_idx] + 2) % 8
	return gear[gear_idx][right_idx]

def get_left(point, gear_idx):
	left_idx = (point[gear_idx] + 6) % 8
	return gear[gear_idx][left_idx]

def turn(point, N, D):
	# 톱니바퀴 한개 움직이기
	left = get_left(point, N - 1)
	right = get_right(point, N - 1)
	point[N-1] = (point[N-1] + D + 8) % 8

	# 왼쪽에 있는 톱니바퀴들 움직이기
	turn_direct = D * -1
	for i in range(N-2, -1, -1):
		if get_right(point, i) == left:
			break
		left = get_left(point, i)
		point[i] = (point[i] + turn_direct + 8) % 8
		turn_direct *= -1

	# 오른쪽에 있는 톱니바퀴들 움직이기
	turn_direct = D * -1
	for i in range(N, 4):
		if get_left(point, i) == right:
			break
		right = get_right(point, i)
		point[i] = (point[i] + turn_direct + 8) % 8
		turn_direct *= -1

import math
if __name__ == "__main__":
	gear = []
	for _ in range(4):
		gear.append(input().split('\n')[0])
	K = int(input())
	point = [0, 0, 0, 0]
	for _ in range(K):
		N, D = map(int, input().split(' '))
		# 시계방향일때 -1, 반시계일때 1로
		turn(point, N, D * -1)
	answer = 0
	for idx in range(4):
		gear_idx = point[idx]
		answer += int(gear[idx][gear_idx]) * math.pow(2, idx)
	print(int(answer))


from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404, redirect
from django.template import loader
from django.http import HttpResponse
from django import template
import json
import os.path
from datetime import timedelta, datetime
import time
# some_file.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/home/optimizacion_final')

from elementos_modelo import mantenimiento as mto
from elementos_modelo import clientes as clt
import ACO
import PSO
import GA
import SA
import Agenda as ag
# import Funcion_objetivo as fo
# funcion=fo

mant=mto.generar_mantenimiento_planificado()
tareas=clt.generar_tareas_aleatorias()
consulta={}
consulta['query']={'tipo':'','hinicio':'','duracion':'','nombre':'','empleado':'','insumos':''}

@login_required(login_url="/login/")
def index(request):

    context = {}
    context['segment'] = 'index'

    html_template = loader.get_template( 'index.html' )
    return HttpResponse(html_template.render(context, request))

@login_required(login_url="/login/")
def pages(request):
    context = {}
    # All resource paths end in .html.
    # Pick out the html file name from the url. And load that template.
    try:

        load_template      = request.path.split('/')[-1]

        #print(cargar_registros())
        if request.method == "POST":
            data=request.POST
            #print(data)
        else:
            data=" "

        ejecutar_form(load_template,data)

        #print(data)

        context['segment'] = load_template
        context['tareas']=ctareas
        context['mantenimiento']=cmantenimiento
        context['empleados']=cpersonal
        context['almacen']=calmacen
        lista=separar_por_tipos(ctareas)
        context['listas']=lista
        context['estados']=cargar_estados()
        #registros=cargar_registros()
        context['dias']=recursos_por_Fecha()
        context['consultas']=consulta['query']
        context['chartga']=graficas['GA']
        context['chartsa']=graficas['SA']
        context['chartpso']=graficas['PSO']
        context['chartaco']=graficas['ACO']
        #print(graficas['GA'])

        html_template = loader.get_template( load_template )
        return HttpResponse(html_template.render(context, request))

    except template.TemplateDoesNotExist:

        html_template = loader.get_template( 'page-404.html' )
        return HttpResponse(html_template.render(context, request))

    except:

        html_template = loader.get_template( 'page-500.html' )
        return HttpResponse(html_template.render(context, request))

def ejecutar_form(a,data):
    if(a=='forma-tareas.html'):
        if(data!= " "):
            if(data['nombre']!= " " and data['contratista']!= " " and data['recepcion']!= " " and data['cantidad']!= " "):
                fecha=datetime.strptime(data['recepcion'],'%Y-%m-%d')
                limite=timedelta(days=7)
                limite=fecha+limite
                limite=limite.strftime("%m %d %Y")
                recepcion=fecha.strftime("%m %d %Y")
                datos={"id": 0, "tipo": data['tipot']+" "+data['tipol']+data['tipob'], "cantidad": data['cantidad'], "nombre": data['nombre'],
                "descripcion": data['descripcion'], "contratista": data['contratista'], "fecha_inicio": "---",
                "fecha_finalizacion": "---", "fecha_limite": limite, "fecha_recepcion": recepcion, "penalizacion": 1000,
                "comentarios": data['comment']}
                #print(datos)
                agregar_tarea(datos)

    if(a=='forma-mantenimiento.html'):
        if(data!= " "):
            if(data['encargado']!= " " and data['eresp']!= " " and data['fechaplan']!= " " and data['fuentecost']!= " "):
                fecha=datetime.strptime(data['fechaplan'],'%Y-%m-%d')
                fecha=fecha.strftime("%m %d %Y")
                datos={"id": 0, "tipo": data['tipo'], "objeto": data['estacion'],
                "encargado": data['encargado'], "descripcion": "informacion a agregar",
                "actividades": data['actividades'], "fecha planificada": fecha,
                "fuente de costos": data['fuentecost'], "empresa_responsable": data['eresp']}

                #print(datos)
                agregar_orden_mto(datos)

    if(a=='Horarios-Personal.html'):
        print('hp')
    if(a=='opt-ga.html'):
        if(data!= " "):
            if(data['tp']!= " " and data['tc']!= " " and data['tm']!= " " and data['ts']!= " " and data['maxk']!= " "):
                datos={"tp":data['tp'],"tc":data['tc'],"tm":data['tm'],"ts":data['ts'],"maxk":data['maxk']}
                actualizar_opt_Settings(datos,'ga')
                print('ga')
    if(a=='opt-SA.html'):
        if(data!= " "):
            if(data['nv']!= " " and data['maxk']!= " "):
                datos={"nv":data['nv'],"maxk":data['maxk']}
                actualizar_opt_Settings(datos,'sa')
                print('sa')
    if(a=='opt-PSO.html'):
        if(data!= " "):
            if(data['tp']!= " " and data['w']!= " " and data['c1']!= " " and data['c2']!= " " and data['maxk']!= " "):
                datos={"tp":data['tp'],"w":data['w'],"c1":data['c1'],"c2":data['c2'],"maxk":data['maxk']}
                actualizar_opt_Settings(datos,'pso')
                print('pso')
    if(a=='opt-ACO.html'):
        if(data!= " "):
            if(data['tp']!= " " and data['maxk']!= " "):
                datos={"tp":data['tp'],"maxk":data['maxk']}
                actualizar_opt_Settings(datos,'aco')
                print('aco')
    if(a=='opt-NSGA.html'):
        if(data!= " "):
            if(data['tp']!= " " and data['tc']!= " " and data['tm']!= " " and data['ts']!= " " and data['maxk']!= " "):
                datos={"tp":data['tp'],"tc":data['tc'],"tm":data['tm'],"ts":data['ts'],"maxk":data['maxk']}
                actualizar_opt_Settings(datos,'nsga')
                print('nsga')
    if(a=='Registros-totales.html'):
        if(data!= " "):
            if(data['tipof']!= " "):
                lista=[]
                for i in libro.bookR:

                    if(i.fecha.strftime("%m %d %Y") == data['tipof']):
                        print('si lo encontro')
                        print(i.fecha.strftime("%m %d %Y"))
                        print(i.recursoA)
                        print(i.recursoB1)
                        print(i.recursoB2)
                        print(i.recursoB3)
                        print(i.recursoB4)
                        print(i.recursoC)
                        if(i.recursoA[0]['TAREA']!={}):
                            for x in i.recursoA:
                                if(x['EMPLEADO']==0):
                                    empleado=0
                                else:
                                    empleado=x['EMPLEADO']['NOMBRE']
                                lista.append({'tipo':x['TIPO'],'hinicio':x['HINICIO'],'duracion':x['HORAS'],'nombre':x['TAREA']['nombre'],'empleado':empleado,'insumos':x['INSUMOS']})
                        if(i.recursoB1['TAREA']!={}):
                            lista.append({'tipo':i.recursoB1['TIPO'],'hinicio':i.recursoB1['HINICIO'],'duracion':i.recursoB1['HORAS'],'nombre':i.recursoB1['TAREA']['nombre'],'empleado':0,'insumos':i.recursoB1['INSUMOS']})
                        if(i.recursoB2['TAREA']!={}):
                            lista.append({'tipo':i.recursoB2['TIPO'],'hinicio':i.recursoB2['HINICIO'],'duracion':i.recursoB2['HORAS'],'nombre':i.recursoB2['TAREA']['nombre'],'empleado':0,'insumos':i.recursoB2['INSUMOS']})
                        if(i.recursoB3['TAREA']!={}):
                            lista.append({'tipo':i.recursoB3['TIPO'],'hinicio':i.recursoB3['HINICIO'],'duracion':i.recursoB3['HORAS'],'nombre':i.recursoB3['TAREA']['nombre'],'empleado':0,'insumos':i.recursoB3['INSUMOS']})
                        if(i.recursoB4['TAREA']!={}):
                            lista.append({'tipo':i.recursoB4['TIPO'],'hinicio':i.recursoB4['HINICIO'],'duracion':i.recursoB4['HORAS'],'nombre':i.recursoB4['TAREA']['nombre'],'empleado':0,'insumos':i.recursoB4['INSUMOS']})
                        if(i.recursoC[0]['TAREA']!={}):

                            for x in i.recursoC:
                                if(x['EMPLEADO']==0):
                                    empleado=0
                                else:
                                    empleado=x['EMPLEADO']['NOMBRE']
                                lista.append({'tipo':x['TIPO'],'hinicio':x['HINICIO'],'duracion':x['HORAS'],'nombre':x['TAREA']['nombre'],'empleado':empleado,'insumos':x['INSUMOS']})
                if (lista == []):
                    lista.append({'tipo':'','hinicio':'','duracion':'','nombre':'','empleado':'','insumos':''})
                consulta['query']=lista
                print(lista)
    if(a=='opt-solver.html'):
        if(data!= " "):
            botones=['btnGA','btnSA','btnPSO','btnACO']
            sel=['selGA','selSA','selPSO','selACO']
            labels=['GA','SA','PSO','ACO']
            ind=0
            for x in botones:
                if x in data.keys():
                    ejecutando_algoritmo(labels[ind])
                ind+=1

            ind=0
            for x in sel:
                if x in data.keys():
                    selecionar_Resultado(labels[ind])
                ind+=1

def selecionar_Resultado(a):

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'estado'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        #se le pasa el vector a la funcion planificador
        x=ctareas
        vector=data[a]['resultado']
        cargar_nueva_configuracion(vector,x)

def cargar_nueva_configuracion(state,tareas):

    x=ordenar_Tareas(state,tareas)
    a=funcion.Calcular_Costo(x,True)

def ordenar_Tareas(state,tareas):
    h={}
    newtareas=[]
    for i in state:
        for x in tareas:
            if(i == x['id']):
                newtareas.append(x)
    h['pedidos']=newtareas
    return h

def ejecutando_algoritmo(a):

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'estado'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        data[a]['estado']='ejecutando'

        with open(completeName,'w') as outfile:
            json.dump(data,outfile)

        if(a=='GA'):
            GA.ejecutarGA()

        if(a=='SA'):
            SA.ejecutarSA()

        if(a=='PSO'):
            PSO.ejecutarPSO()

        if(a=='ACO'):
            ACO.ejecutarACO()

        #se ejecuta el algoritmo en cuestion

def cargar_estados():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'estado'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        return data

def cargar_tareas():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'data'

        completeName = os.path.join(save_path, name_of_file+".json")

        with open(completeName) as json_file:
            data = json.load(json_file)

        return data['pedidos']

def separar_por_tipos(tareas):

    vino=[]
    agua=[]
    jugo=[]
    #print(tareas)
    for val in tareas:
        tipo=val['tipo'].split()
        if(tipo[0]=='vino'):
            for x in range(val['cantidad']):
                k=val.copy()
                k['cantidad']=1
                vino.append(k)
            #print('v')
        if(tipo[0]=='agua'):
            for x in range(val['cantidad']):
                k=val.copy()
                k['cantidad']=1
                agua.append(k)
            #print('a')
        if(tipo[0]=='banano' or tipo[0]=='guanabana'):
            for x in range(val['cantidad']):
                k=val.copy()
                k['cantidad']=1
                jugo.append(k)

    nummax=len(vino)
    if(nummax < len(jugo)):
        nummax=len(jugo)
    if(nummax < len(agua)):
        nummax=len(agua)

    lista=[]
    for i in range(nummax):
        #print(i)

        if(i>=(len(jugo)-1)):
            jugov=""
        else:
            jugov=jugo[i]

        if(i>=(len(vino)-1)):
            vinov=""
        else:
            vinov=vino[i]

        if(i>=(len(agua)-1)):
            aguav=""
        else:
            aguav=agua[i]

        lista.append([jugov,vinov,aguav])

    return lista

def cargar_registros():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'registro'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        return data

def actualizar_opt_Settings(datos,name):

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'optsettings'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        data['opt'][name]=datos

        with open(completeName,'w') as outfile:
            json.dump(data,outfile)

def agregar_tarea(datos):

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'data'

        completeName = os.path.join(save_path, name_of_file+".json")

        with open(completeName) as json_file:
            data = json.load(json_file)

        maxvalue = max(data['pedidos'], key=lambda x:x['id'])

        datos['id']=maxvalue['id']+1

        data['pedidos'].append(datos)

        with open(completeName,'w') as outfile:
            json.dump(data,outfile)

def agregar_orden_mto(datos):

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'datam'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        maxvalue = max(data['preventivo'], key=lambda x:x['id'])

        datos['id']=maxvalue['id']+1

        data['preventivo'].append(datos)

        with open(completeName,'w') as outfile:
            json.dump(data,outfile)

def cargar_mantenimiento():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'datam'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        return data['preventivo']

def cargar_personal():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'datae'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        return data['empleados']

def cargar_almacen():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'Punta Delicia'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        return data['racks']
def cargar_book():
    save_path = '/home/optimizacion_final/datos_json'
    name_of_file = 'book_pedidos'
    completeName = os.path.join(save_path, name_of_file+".json")
    with open(completeName) as json_file:
        pedidos = json.load(json_file)

    name_of_file = 'book_recursos'
    completeName = os.path.join(save_path, name_of_file+".json")
    with open(completeName) as json_file:
        recursos = json.load(json_file)

    return pedidos,recursos

def cargar_datos():

        save_path = '/home/optimizacion_final/datos_json'

        name_of_file = 'charts'

        completeName = os.path.join(save_path, name_of_file+".txt")

        with open(completeName) as json_file:
            data = json.load(json_file)

        pasoga=int(len(data['GA']['y'])/100)
        #pasoga=0
        #print(pasoga)
        y=[]
        x=[]
        cont=0
        for i in range(len(data['GA']['y'])):
            if(cont==pasoga):
                y.append(data['GA']['y'][i])
                x.append(data['GA']['x'][i])
                cont=0
            cont+=1
        data['GA']['y']=y
        data['GA']['x']=x

        pasoga=int(len(data['SA']['y'])/100)
        #pasoga=0
        #print(pasoga)
        y=[]
        x=[]
        cont=0
        for i in range(len(data['SA']['y'])):
            if(cont==pasoga):
                y.append(data['SA']['y'][i])
                x.append(data['SA']['x'][i])
                cont=0
            cont+=1
        data['SA']['y']=y
        data['SA']['x']=x

        pasoga=int(len(data['PSO']['y'])/100)
        #pasoga=0
        #print(pasoga)
        y=[]
        x=[]
        cont=0
        for i in range(len(data['PSO']['y'])):
            if(cont==pasoga):
                y.append(data['PSO']['y'][i])
                x.append(data['PSO']['x'][i])
                cont=0
            cont+=1
        data['PSO']['y']=y
        data['PSO']['x']=x

        pasoga=int(len(data['ACO']['y'])/100)
        #pasoga=0
        #print(pasoga)
        y=[]
        x=[]
        cont=0
        for i in range(len(data['ACO']['y'])):
            if(cont==pasoga):
                y.append(data['ACO']['y'][i])
                x.append(data['ACO']['x'][i])
                cont=0
            cont+=1
        data['ACO']['y']=y
        data['ACO']['x']=x

        return data

def recursos_por_Fecha():
    inicio=datetime.strptime(brecursos['calendario'][0]['fecha'],'%m %d %Y')
    final=datetime.strptime(brecursos['calendario'][-1]['fecha'],'%m %d %Y')
    # inicio=datetime.strptime(registros['fecha_inicio'],'%m %d %Y')
    # final=datetime.strptime(registros['fecha_final'],'%m %d %Y')
    valor=final-inicio
    lista=[]
    for i in range(valor.days+1):
        dia=inicio + timedelta(days=(i))
        lista.append(dia.strftime("%m %d %Y"))

    return lista

registros=cargar_registros()
ctareas=cargar_tareas()
cmantenimiento=cargar_mantenimiento()
cpersonal=cargar_personal()
calmacen=cargar_almacen()
graficas=cargar_datos()
bpedidos,brecursos=cargar_book()
diao=datetime.strptime(brecursos['calendario'][0]['fecha'],'%m %d %Y')
diaf=datetime.strptime(brecursos['calendario'][-1]['fecha'],'%m %d %Y')
libro=ag.agenda()
libro.crear_AgendaR(diao,diaf)
libro.crear_AgendaP(diao,diaf)
libro.cargarP(bpedidos['calendario'])
libro.cargarR(brecursos['calendario'])


import json
import os

DATA_FILE = "expense_data.json"

def load_data():
    # Load existing data from the file or initialize a new data structure
    if os.path.exists(DATA_FILE):
        with open(DATA_FILE, "r") as file:
            return json.load(file)
    else:
        # Default data structure with predefined categories
        return {"expenses": [], "categories": ["Groceries", "Transportation", "Entertainment", "Utilities"]}

def save_data(data):
    # Save the updated data to the file
    with open(DATA_FILE, "w") as file:
        json.dump(data, file, indent=2)

def display_menu():
    # Display the main menu options
    print("\nExpense Tracker Menu:")
    print("1. Add Expense")
    print("2. View Expenses")
    print("3. View Category Summaries")
    print("4. Manage Categories")
    print("5. Exit")

def add_expense(data):
    # Allow the user to input details for a new expense, including category selection
    print("\nEnter Expense Details:")
    description = input("Description: ")
    amount = float(input("Amount: "))

    # Display categories for user selection
    print("\nSelect a Category:")
    for i, category in enumerate(data["categories"], start=1):
        print(f"{i}. {category}")

    # Get user input for category and validate
    category_index = int(input("Enter the number corresponding to the category: ")) - 1
    category = data["categories"][category_index]

    # Create a new expense and append it to the data
    expense = {"description": description, "amount": amount, "category": category}
    data["expenses"].append(expense)
    save_data(data)
    print("Expense added successfully.")

def view_expenses(data):
    # Display all recorded expenses
    if not data["expenses"]:
        print("No expenses recorded.")
        return

    print("\nAll Expenses:")
    for expense in data["expenses"]:
        print(f"{expense['description']} - {expense['amount']}/- - {expense['category']}")

def view_category_summaries(data):
    # Display summaries for each expense category
    if not data["expenses"]:
        print("No expenses recorded.")
        return

    print("\nCategory Summaries:")
    for category in data["categories"]:
        expenses_in_category = [expense["amount"] for expense in data["expenses"] if expense["category"] == category]
        total_spending = sum(expenses_in_category)
        average_spending = total_spending / len(expenses_in_category) if expenses_in_category else 0
        print(f"{category}: Total - {total_spending}/-, Average - {average_spending:.2f}/-")

def manage_categories(data):
    # Allow the user to manage expense categories
    print("\nManage Categories:")
    print("1. Add Category")
    print("2. Edit Category")
    print("3. Remove Category")
    print("4. Back to Main Menu")

    choice = input("Enter your choice (1-4): ")

    if choice == "1":
        add_category(data)
    elif choice == "2":
        edit_category(data)
    elif choice == "3":
        remove_category(data)
    elif choice == "4":
        return
    else:
        print("Invalid choice. Please try again.")

def add_category(data):
    # Allow the user to add a new category
    new_category = input("Enter the new category name: ")
    data["categories"].append(new_category)
    save_data(data)
    print(f"Category '{new_category}' added successfully.")

def edit_category(data):
    # Allow the user to edit an existing category
    print("\nExisting Categories:")
    for i, category in enumerate(data["categories"], start=1):
        print(f"{i}. {category}")

    category_index = int(input("Enter the number corresponding to the category to edit: ")) - 1

    new_category = input("Enter the new category name: ")
    data["categories"][category_index] = new_category
    save_data(data)
    print("Category edited successfully.")

def remove_category(data):
    # Allow the user to remove an existing category
    print("\nExisting Categories:")
    for i, category in enumerate(data["categories"], start=1):
        print(f"{i}. {category}")

    category_index = int(input("Enter the number corresponding to the category to remove: ")) - 1
    removed_category = data["categories"].pop(category_index)

    # Remove the category from all expenses
    data["expenses"] = [expense for expense in data["expenses"] if expense["category"] != removed_category]

    save_data(data)
    print(f"Category '{removed_category}' removed successfully.")

if __name__ == "__main__":
    # Load existing data or initialize a new data structure
    expense_data = load_data()

    while True:
        display_menu()
        choice = input("Enter your choice (1-5): ")

        if choice == "1":
            add_expense(expense_data)
        elif choice == "2":
            view_expenses(expense_data)
        elif choice == "3":
            view_category_summaries(expense_data)
        elif choice == "4":
            manage_categories(expense_data)
        elif choice == "5":
            print("Exiting the Expense Tracker. Goodbye!")
            break
        else:
            print("Invalid choice. Please try again.")


def threeSum(nums: list[int]) -> list[list[int]]:

    nums = sorted(nums)
    i, j, k, arr = 0, 0, 0, []

    while i <= len(nums)-3:
        if i-1 >= 0 and nums[i] == nums[i-1]:
            i += 1
            continue
        j, k = i+1, len(nums)-1
        while j < k:
            if nums[j] + nums[k] + nums[i] < 0: j += 1
            elif nums[j] + nums[k] + nums[i] > 0: k -= 1
            else:
                arr.append([nums[i], nums[j], nums[k]])
                j += 1
                while nums[j] == nums[j-1] and j < k:
                    j += 1
        i += 1

    return arr

numList = [-2, -2, 0, 0, 2, 2]
print(threeSum(numList))

2.Two Pointers/3.Three Sum/3.Three Sum - ans.py


ENDOGENOUS_TYPE = "endogenous"
EXOGENOUS_TYPE = "exogenous"

from typing import Dict

class Variable:
    def __init__(self, name: str, type: str, config: Dict[str, float] = None):
        self.type = type
        self.name = name
        self.config = config


from collections import defaultdict

def multilevel_group_by(iterable, groupers=[], getter=getattr):
    grouper = groupers.pop(0)

    result = defaultdict(list)
    for item in iterable:
        key = getter(item, grouper)

        result[key].append(item)

    if groupers:
        for key, value in result.iteritems():
            result[key] = multilevel_group_by(value, groupers, getter)

    return result


import torch.nn as nn
import torch

def conv(in_channels, out_channels):
    return nn.Sequential(
        nn.Conv2d(in_channels, out_channels, 3, padding=1),  # 3은 kernel size
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True),
        nn.Conv2d(out_channels, out_channels, 3, padding=1),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True)
    )

class plain_unet(nn.Module):
    def __init__(self, n_classes=50):
        super(plain_unet, self).__init__()
        self.n_classes = n_classes
        self.convDown1 = conv(9, 64)
        self.convDown2 = conv(64, 128)
        self.convDown3 = conv(128, 256)
        self.convDown4 = conv(256, 512)
        self.convDown5 = conv(512, 1024)
        self.maxpool = nn.MaxPool2d(2, stride=2)

        self.upsample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
        self.convUp4 = conv(1024+512, 512)
        self.convUp3 = conv(512+256, 256)
        self.convUp2 = conv(256+128, 128)
        self.convUp1 = conv(128+64, 64)
        self.convUp_fin = nn.Conv2d(64, self.n_classes, 1)

    def forward(self, x):
        conv1 = self.convDown1(x)
        x = self.maxpool(conv1)
        conv2 = self.convDown2(x)
        x = self.maxpool(conv2)
        conv3 = self.convDown3(x)
        x = self.maxpool(conv3)
        conv4 = self.convDown4(x)
        x = self.maxpool(conv4)

        conv5 = self.convDown5(x)
        print("conv5: ",conv5.shape)
        x = self.upsample(conv5)
        print("after upsample: ", x.shape)
        x = torch.cat((x,conv4), dim=1)

        x = self.convUp4(x)
        print("conv5: ",conv5.shape)
        x = self.upsample(x) #1, 512, 64, 64
        x = torch.cat((x,conv3), dim=1)

        x = self.convUp3(x)
        x = self.upsample(x) #1, 256, 128, 128
        x = torch.cat((x,conv2), dim=1)

        x = self.convUp2(x)
        x = self.upsample(x) #1, 128, 256, 256
        x = torch.cat((x,conv1), dim=1)

        x = self.convUp1(x) #1, 64, 256, 256

        out = self.convUp_fin(x) #1, 50, 256, 256
        return out


import sys
from collections import deque

input = sys.stdin.readline
n, m = map(int, input().split())
graph1 = []
graph2 = []
for _ in range(n):
    graph1.append(list(map(int, input().split())))

for _ in range(n):
    graph2.append(list(map(int, input().split())))
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

def bfs(ax, ay):
    q = deque([(ax, ay)])
    copy = graph1[ax][ay]
    graph1[ax][ay] = graph2[ax][ay]

    while q:
        x, y = q.popleft()

        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if nx < 0 or nx >= n or ny < 0 or ny >= m:
                continue

            if graph1[nx][ny] == copy:
                graph1[nx][ny] = graph1[x][y]
                q.append((nx, ny))

is_break = False
for i in range(n):
    for j in range(m):
        if graph1[i][j] != graph2[i][j]:
            bfs(i, j)
            is_break = True
    if is_break == True:
        break

if graph1 == graph2:
    print("YES")
else:
    print("NO")


#==========================================
# Author: Shierene Cervantes
#==========================================
# python script

import pandas as pd
tr_data = pd.read_csv('categorization_data.csv', encoding="latin1")

tr_data['title_and_description'] = tr_data[['Title', 'Description']].apply(tuple, axis=1)

tr_data.to_csv('categorizationdata_withtad.csv')

from io import StringIO
col = ['CATEGORY', 'title_and_description']
tr_data = tr_data[col]
tr_data = tr_data[pd.notnull(tr_data['title_and_description'])]
tr_data.columns = ['CATEGORY', 'title_and_description']
tr_data['Number'] = tr_data['CATEGORY'].factorize()[0]
category_id_df = tr_data[['CATEGORY', 'Number']].drop_duplicates().sort_values('Number')
category_to_id = dict(category_id_df.values)
id_to_category = dict(category_id_df[['Number', 'CATEGORY']].values)
tr_data.head()

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,6))
tr_data.groupby('CATEGORY').title_and_description.count().plot.bar(ylim=0)
plt.show()

from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer(sublinear_tf=True, min_df=5, norm='l2', encoding='latin-1', ngram_range=(1, 2), stop_words='english')
features = tfidf.fit_transform(tr_data.title_and_description).toarray()
labels = tr_data.Number
features.shape

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import LinearSVC

X_train, X_test, y_train, y_test = train_test_split(tr_data['title_and_description'], tr_data['CATEGORY'], random_state = 0)
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(X_train)
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
clf = MultinomialNB().fit(X_train_tfidf, y_train)
svc = LinearSVC().fit(X_train_tfidf, y_train)

from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import LinearSVC
from sklearn.model_selection import cross_val_score
models = [
    RandomForestClassifier(n_estimators=200, max_depth=3, random_state=0),
    LinearSVC(),
    MultinomialNB(),
    LogisticRegression(random_state=0),
]
CV = 2
cv_df = pd.DataFrame(index=range(CV * len(models)))
entries = []
for model in models:
  model_name = model.__class__.__name__
  accuracies = cross_val_score(model, features, labels, scoring='accuracy', cv=CV)
  for fold_idx, accuracy in enumerate(accuracies):
    entries.append((model_name, fold_idx, accuracy))
cv_df = pd.DataFrame(entries, columns=['model_name', 'fold_idx', 'accuracy'])
import seaborn as sns
sns.boxplot(x='model_name', y='accuracy', data=cv_df)
sns.stripplot(x='model_name', y='accuracy', data=cv_df,
              size=8, jitter=True, edgecolor="gray", linewidth=2)
plt.show()

cv_df.groupby('model_name').accuracy.mean()

model = LinearSVC()
X_train, X_test, y_train, y_test, indices_train, indices_test = train_test_split(features, labels, tr_data.index, test_size=0.4, random_state=0)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y_test, y_pred)
fig, ax = plt.subplots(figsize=(10,10))
sns.heatmap(conf_mat, annot=True, fmt='d',
            xticklabels=category_id_df.CATEGORY.values, yticklabels=category_id_df.CATEGORY.values)
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.show()

from sklearn import metrics
print(metrics.classification_report(y_test, y_pred, target_names=tr_data['CATEGORY'].unique()))

test_set = pd.read_csv('categorizationdata_withtad.csv', encoding="latin1")
test_set

test_set['Combined MNB New Predicted Values'] = clf.predict(count_vect.transform(test_set['title_and_description'].values.astype('U')))

test_set['Combined SVC New Predicted Values'] = svc.predict(count_vect.transform(test_set['title_and_description'].values.astype('U')))

test_set['Title MNB New Predicted Values'] = clf.predict(count_vect.transform(test_set['Title'].values.astype('U')))

test_set['Title SVC New Predicted Values'] = svc.predict(count_vect.transform(test_set['Title'].values.astype('U')))

test_set['comparetitlesvc'] = test_set['Combined SVC New Predicted Values'] == test_set['CATEGORY']

test_set['comparetitlesvc'].value_counts(normalize=True)*100

test_set.to_csv('ModelComparison_SVC_MNB.csv')


from nmigen import *

__all__ = ["LcdHD44780"]

DATA_WIDTH = 8

SETUP_CLOCKS  = 5
HOLD_CLOCKS   = 20
MAX_DELAY     = 50 * 1000 * 5 # Five milliseconds

# Comb decoder to turn a data or command into however many clocks the write should take
class LcdDelayDecoder(Elaboratable):

    def __init__(self):

        self.data = Signal(DATA_WIDTH)
        self.rs = Signal()

        self.delay_clks = Signal(range(MAX_DELAY))

        self.ports = [
            self.data,
            self.rs,
            self.delay_clks,
        ]

    def elaborate(self, platform):

        CLK_FACTOR = 50 # How many clocks per usec (FIXME)

        # Datasheet values plus a small fudge factor
        RESET_DELAY   = 1600 * CLK_FACTOR # Clear Display / Return Home (RS=0)
        COMMAND_DELAY =   50 * CLK_FACTOR # All other commands          (RS=0)
        CHAR_DELAY    =   50 * CLK_FACTOR # Character writes            (RS=1)

        m = Module()

        # In case nothing gets handled, use a 5 millisecond delay.
        m.d.comb += self.delay_clks.eq(MAX_DELAY)

        with m.If(self.rs):
            # Data
            m.d.comb += self.delay_clks.eq(CHAR_DELAY)
        with m.Elif(self.data[2:7] == 0):
            # Reset / Home commands.
            m.d.comb += self.delay_clks.eq(RESET_DELAY)
        with m.Else():
            # Other commands.
            m.d.comb += self.delay_clks.eq(COMMAND_DELAY)

        return m

class LcdController(Elaboratable):

    def __init__(self):
        pass

        # 8 bit data + RS signal (1=char, 0=cmd)
        self.data = Signal(DATA_WIDTH + 1)
        self.start = Signal()
        self.ready = Signal()

        self.lcd_data = Signal(DATA_WIDTH)
        self.lcd_en   = Signal()
        self.lcd_rs   = Signal()

        self.ports = [
            self.data,
            self.start,
            self.ready,

            self.lcd_data,
            self.lcd_en,
            self.lcd_rs,
        ]

        # Internal State

        self.delay_ctr = Signal(range(MAX_DELAY * 4)) # TODO: figure out clock rate

    def elaborate(self, platform):
        m = Module()

        # Split out the RS/DATA signals.
        lcd_data   = self.data[:DATA_WIDTH]
        lcd_rs     = self.data[DATA_WIDTH]
        delay_done = self.delay_ctr == 0

        # Instantiate the delay decoder.
        delay_decoder = LcdDelayDecoder()
        m.submodules += delay_decoder
        m.d.comb += [
            delay_decoder.data.eq(lcd_data),
            delay_decoder.rs.eq(lcd_rs),
        ]

        # Default signal assignments unless overridden in the FSM.
        m.d.comb += self.ready.eq(0)

        # If delay line is non-zero, decrement it.
        # This is used to advance to the next state
        with m.If(self.delay_ctr > 0):
            m.d.sync += self.delay_ctr.eq(self.delay_ctr - 1)

        with m.FSM():
            with m.State('IDLE'):
                m.d.comb += self.ready.eq(delay_done & ~self.start)
                with m.If(self.start & delay_done):
                    # Set up a new transaction. En isn't pulsed until the next
                    m.next = 'SETUP'
                    m.d.sync += [
                        self.delay_ctr.eq(SETUP_CLOCKS),
                        self.lcd_data.eq(lcd_data),
                        self.lcd_rs.eq(lcd_rs),
                    ]

            with m.State('SETUP'):
                with m.If(delay_done):
                    m.next = 'HOLD'
                    m.d.sync += [
                        self.delay_ctr.eq(HOLD_CLOCKS),
                        self.lcd_en.eq(1),
                    ]

            with m.State('HOLD'):
                with m.If(delay_done):
                    m.next = 'IDLE'
                    m.d.sync += [
                        self.delay_ctr.eq(delay_decoder.delay_clks),
                        self.lcd_en.eq(0),
                    ]

        return m

class LcdHD44780(Elaboratable):

    def __init__(self, *, print_test_text=False):

        self.lcd_data = Signal(DATA_WIDTH)
        self.lcd_en   = Signal()
        self.lcd_rs   = Signal()

        self.ports = [

            # TODO user ports.

            # Hardware ports.
            self.lcd_data,
            self.lcd_en,
            self.lcd_rs,
        ]

        self.LCD_INIT = [
            0x038, # Function set (8-bit, 2 lines, 5x8)
            0x00C, # Display On, cursor off, blinking off
            0x001, # Clear display
            0x006, # Cursor increment, no shift

            0x080, # Set DDRAM to 0x00 (start of first line)
        ]

        if print_test_text:
            from nmigen import __version__ as nmigen_version_str

            def enc_str(s):
                chars = [0x100 + ord(c) for c in s]

                if len(chars) > 16:
                    print('Warning: truncating enc_str()')

                return chars[:16]

            version_split = nmigen_version_str.split('+')
            nm_version = version_split[0]
            nm_gitrev  = version_split[1]

            self.LCD_INIT.extend([
                #0x080,
                *enc_str('nMigen {}'.format(nm_gitrev)),

                0x0C0,
                *enc_str(nm_version)
            ])

        self.init = Signal()
        self.init_idx = Signal(range(len(self.LCD_INIT)))

    def elaborate(self, platform):
        m = Module()

        # Instantiate the low-level LCD controller.
        cont = LcdController()
        m.submodules += cont

        # Static connections to low-level controller.
        m.d.comb += [
            self.lcd_en.eq(cont.lcd_en),
            self.lcd_data.eq(cont.lcd_data),
            self.lcd_rs.eq(cont.lcd_rs),
        ]

        # Instantiate the initialization code ROM.
        init_rom = Memory(width=DATA_WIDTH+1, depth=len(self.LCD_INIT), init=self.LCD_INIT).read_port(domain='comb')
        m.submodules += init_rom

        m.d.comb += init_rom.addr.eq(self.init_idx)
        m.d.comb += self.init.eq(self.init_idx < len(self.LCD_INIT))

        # Default value for start at every clock cycle.
        m.d.sync += cont.start.eq(0)

        with m.If(self.init):
            # Handle the initialization process.

            # Are we ready for the next transaction?
            with m.If(cont.ready):
                m.d.sync += [
                    cont.data.eq(init_rom.data),
                    cont.start.eq(1),
                    self.init_idx.eq(self.init_idx + 1),
                ]

        with m.Else():
            pass # TODO accept user input

        return m

if __name__ == '__main__':
    # Do an initialization simulation.
    from nmigen.sim import *

    # Override the MAX_DELAY to something more reasonable...
    MAX_DELAY = 2000*50

    dut = LcdHD44780()

    def process():

        # Give us enough cycles to complete the initialization.
        MAX_CYCLES = (len(dut.LCD_INIT) * (SETUP_CLOCKS+HOLD_CLOCKS+MAX_DELAY)) + 1000

        for _ in range(MAX_CYCLES):
            yield

            # Check if init has finished.
            init = yield dut.init
            if not init:
                print('LCD init finished.')
                # Spin for a number of clocks.
                for _ in range(100):
                    yield

                return

        print('LCD init timed out.')

    sim = Simulator(dut)
    sim.add_clock(1/50.E6) # Add 50 MHz clock.
    sim.add_sync_process(process)

    with sim.write_vcd('lcd.vcd'):
        sim.run()


import turtle
import random

# set up the screen
window = turtle.Screen() # creates a window
window.title("Likun's Snake Game") # give title to the window
window.bgcolor("black") # set the background color
window.setup(width=600, height=600) # sets the window dimensions
window.tracer(0) # turns off the screen updates

# create snake's head
head = turtle.Turtle() # create a turtle (python notation)
head.speed(0) # we don't want the head to move
head.shape("square") # set the head shape
head.color("white") # set the head color
head.penup() # we don't want the path to be drawn
head.goto(0, 100) # position of the snake's head
head.direction = "stop"

# functions to move the snake
def move():
    if head.direction == "up":
        y = head.ycor() # y coordinate of the turtle
        head.sety(y+20)

    if head.direction == "down":
        y = head.ycor() # y coordinate of the turtle
        head.sety(y-20)

    if head.direction == "right":
        x = head.xcor() # x coordinate of the turtle
        head.setx(x+20)

    if head.direction == "left":
        x = head.xcor() # x coordinate of the turtle
        head.setx(x-20)

import time # to represent time in code
delay = 0.1

def move_up():
    # the snake cannot go up from down and vica-versa
    if head.direction != "down":
        head.direction = "up"

def move_down():
    # the snake cannot go down from up and vica-versa
    if head.direction != "up":
        head.direction = "down"

def move_right():
    # the snake cannot go right from left and vica-versa
    if head.direction != "left":
        head.direction = "right"

def move_left():
    # the snake cannot go left from right and vica-versa
    if head.direction != "right":
        head.direction = "left"

# keyboard bindings
window.listen() # the program now listens to key press
# assigning keys to function calls
window.onkey(move_up, "Up")
window.onkey(move_down, "Down")
window.onkey(move_right, "Right")
window.onkey(move_left, "Left")

# food for our snake
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.shapesize(0.50, 0.50) # stretch value in width and length
food.goto(0, 0)

# initialize an empty list for snake's body
segs = []

# scores
c_score = 0
high_score = 0

# scoring system
score = turtle.Turtle()
score.speed(0)
score.shape("square")
score.color("white")
score.penup()
score.hideturtle()
score.goto(0, 260)
score.write("Score: 0 High Score: 0", align="center", font=("Courier", 20, "normal"))

# main game loop
while True:
    window.update()

    time.sleep(delay)
    # sleep function in time module adds delay to the code
    # halts the excecution of the program for the given no of secs

    # when snake eats the food
    if head.distance(food) < 15.75:
        # relocate the food to a random position
        # randomization
        x = random.randint(-280, 280)
        y = random.randint(-280, 280)
        food.goto(x, y) # relocate coordinates

        # add a segment
        new_seg = turtle.Turtle()
        new_seg.speed(0)
        new_seg.shape("square")
        new_seg.color("grey")
        new_seg.penup()
        # elongation of snake's body
        segs.append(new_seg)

        # as the snake eats food, it's speed increases
        delay -= 0.001

        # update current score
        c_score = c_score+10

        if c_score > high_score:
            high_score = c_score

        # update score tab
        score.clear()
        score.write("Score: {} High Score: {}".format(c_score, high_score), align="center", font=("Courier", 20, "normal"))

    # move the end segments first in reverse order
    for index in range(len(segs)-1, 0, -1):
        x = segs[index-1].xcor()
        y = segs[index-1].ycor()
        segs[index].goto(x, y)

    # move segment 0 to where the head is
    if len(segs) > 0:
        x = head.xcor()
        y = head.ycor()
        segs[0].goto(x, y)

    # case handling for border collision
    if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
        time.sleep(1)
        head.goto(0, 0)
        head.direction = "stop"

        # relocate the segments outside the window coordinates
        for s in segs:
            s.goto(1000, 1000)

        # clear the segs list
        segs.clear()

        # reset current score
        c_score = 0

        # reset delay
        delay = 0.1

        # update score tab
        score.clear()
        score.write("Score: {} High Score: {}".format(c_score, high_score), align="center", font=("Courier", 20, "normal"))

    move()

    # when snake bites itself
    for segment in segs:
        if segment.distance(head) < 20:
            time.sleep(1)
            head.goto(0,0)
            head.direction = "stop"

            # hide the segments
            for segment in segs:
                segment.goto(1000, 1000)

            # clear the segments list
            segs.clear()

            # reset current score
            c_score = 0

            # reset delay
            delay = 0.1

            # update score tab
            score.clear()
            score.write("Score: {} High Score: {}".format(c_score, high_score), align="center", font=("Courier", 20, "normal"))


# 20. Valid Parentheses (Easy)
'''
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:
    Open brackets must be closed by the same type of brackets.
    Open brackets must be closed in the correct order.

Example 1:
    Input: s = "()"
    Output: true

Example 2:
    Input: s = "()[]{}"
    Output: true

Example 3:
    Input: s = "(]"
    Output: false

Example 4:
    Input: s = "([)]"
    Output: false

Example 5:
    Input: s = "{[]}"
    Output: true

Constraints:
    1 <= s.length <= 10**4
    s consists of parentheses only '()[]{}'.
'''

# Solution 1 by Yifor
class Solution:
    def isValid(self, s: str) -> bool:
        if not len(s) % 2:
            for i in range(len(s) // 2):
                s = s.replace('()', '')
                s = s.replace('[]', '')
                s = s.replace('{}', '')
            n = len(s)
            return s[:n // 2] == s[n // 2::-1] if n else True
        else:
            return False
'''time complexity: O(n**2) , space complexity: O(1)'''

# Solution 2 by Yifor
class Solution:
    def isValid(self, s: str) -> bool:
        reverse_map = {'(': ')', '[': ']', '{': '}'}
        watch = []
        if (s[0] in reverse_map) and (not len(s) % 2):
            for ele in s:
                if ele in ['(', '[', '{']:
                    watch.append(ele)
                else:
                    try:
                        if reverse_map.get(watch[-1]) != ele:
                            return False
                        else:
                            watch.pop()
                    except:
                        return False
            return watch == []
        else:
            return False
'''time complexity: O(n) , space complexity: O(n)'''


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('main', '0004_show_eps'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='show',
            name='episodes',
        ),
    ]


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import typing as tg
import numpy as np

from modules import *

class MLP(object):
    """
    This class implements a Multi-layer Perceptron in NumPy.
    It handles the different layers and parameters of the model.
    Once initialized an MLP object can perform forward and backward.
    """

    def __init__(self, n_inputs: int, n_hidden: tg.List[int], n_classes: int):
        """
        Initializes MLP object.

        Parameters
        -----------
        n_inputs : int
            number of inputs.
        n_hidden : list of int
            specifies the number of units in each linear layer.
            If the list is empty, the MLP will not have any
            linear layers, and the model will simply perform
            a multinomial logistic regression.
        n_classes: int
            number of classes of the classification problem.
            This number is required in order to specify the
            output dimensions of the MLP
        """

        #######################
        # PUT YOUR CODE HERE  #
        #######################
        self.modules = []
        # getting all dims
        dims = [n_inputs] + n_hidden + [n_classes]
        # handle first layer specifically
        self.modules.append(LinearModule(n_inputs, dims[1], True))
        # handle hidden layers + output
        for i in range(1, len(dims) - 1):
            self.modules.append(ReLUModule())
            self.modules.append(LinearModule(dims[i], dims[i + 1]))
        # handle last layer (diff activation)
        self.modules.append(SoftMaxModule())
        #######################
        # END OF YOUR CODE    #
        #######################

    def forward(self, x: np.ndarray):
        """
        Performs forward pass of the input.
        Here an input tensor x is transformed through
        several layer transformations.

        Parameters
        ----------
        x : np.ndarray
            (n_samples, n_input) input to the network

        Returns
        -------
        out : np.ndarray
            (n_samples, n_output) outputs of the network
        """

        #######################
        # PUT YOUR CODE HERE  #
        #######################
        x = x.reshape(x.shape[0], -1)
        for module in self.modules:
            x = module.forward(x)
        out = x
        #######################
        # END OF YOUR CODE    #
        #######################

        return out

    def backward(self, dout: np.ndarray):
        """
        Performs backward pass given the gradients of the loss.

        Parameters
        ----------
        dout : np.ndarray
            gradients of the loss
        """

        #######################
        # PUT YOUR CODE HERE  #
        #######################
        for module in reversed(self.modules):
            dout = module.backward(dout)
        #######################
        # END OF YOUR CODE    #
        #######################

    def clear_cache(self):
        """
        Remove any saved tensors for the backward pass from any module.
        Used to clean-up model from any remaining input data when we want to save it.
        """

        #######################
        # PUT YOUR CODE HERE  #
        #######################
        for module in self.modules:
            module.clear_cache()
        #######################
        # END OF YOUR CODE    #
        #######################

# if __name__ == "__main__":
#     mlp = MLP(784, [3, 5, 6], 10)
#     x = np.random.randn(7, 784)
#     y = mlp.forward(x)
#     # simulate backprop
#     dout = np.random.randn(7, 10)
#     mlp.backward(dout)
#     print(f"forward output shape: {y.shape}")
#     print(f"forward output: {y}")
#     print(f"forward output sum over axis 1: {y.sum(axis=1)}")
#     print(f"first layer bias shape {mlp.modules[2].params['bias'].shape}")
#     print(f"first layer bias grad shape {mlp.modules[2].grads['bias'].shape}")
#     print(f"first layer weight shape {mlp.modules[2].params['weight'].shape}")
#     print(f"first layer weight grad shape {mlp.modules[2].grads['weight'].shape}")


from __future__ import absolute_import

import numpy as np
import warnings

from .base import Model

class PyTorchModel(Model):
    """Creates a :class:`Model` instance from a `PyTorch` module.
    Parameters
    ----------
    model : `torch.nn.Module`
        The PyTorch model that should be attacked.
    num_classes : int
        Number of classes for which the model will output predictions.
    channel_axis : int
        The index of the axis that represents color channels.
    device : string
        A string specifying the device to do computation on.
        If None, will default to "cuda:0" if torch.cuda.is_available()
        or "cpu" if not.
    preprocessing: 2-element tuple with floats or numpy arrays
        Elementwises preprocessing of input; we first subtract the first
        element of preprocessing from the input and then divide the input by
        the second element.
    """

    def __init__(
            self,
            model,
            num_classes,
            device=None):

        # lazy import
        import torch

        super(PyTorchModel, self).__init__()

        self._num_classes = num_classes

        if device is None:
            self.device = torch.device(
                "cuda:0" if torch.cuda.is_available() else "cpu")
        elif isinstance(device, str):
            self.device = torch.device(device)
        else:
            self.device = device
        self._model = model.eval().to(self.device)

        if model.training:
            warnings.warn(
                'The PyTorch model is in training mode and therefore might'
                ' not be deterministic. Call the eval() method to set it in'
                ' evaluation mode if this is not intended.')

    def forward(self, inputs):
        # lazy import
        import torch

        # inputs, _ = self._process_input(inputs)
        n = len(inputs)
        inputs = torch.from_numpy(inputs).to(self.device)

        predictions = self._model(inputs)
        predictions = predictions.detach().cpu().numpy()

        assert predictions.ndim == 2
        assert predictions.shape == (n, self.num_classes())
        return predictions

    def evaluate(self, test_dataset):
        pass

    def num_classes(self):
        return self._num_classes


import numpy as np
import pandas as pd

np.random.seed(42)

# %% Połącz te dwa obiekty w jeden obiekt typu DataFrame (jako dwie kolumny)
#    i przypisz do zmiennej df. Nadaj nazwy kolumn odpowiednio col1 oraz col2
s1 = pd.Series(np.random.rand(20))
s2 = pd.Series(np.random.randn(20))
dict_s3 = {'col1':s1, 'col2':s2}
df = pd.DataFrame(dict_s3)
df
# %% Wytnij z obiektu df wiersze, dla których wartość w kolumnie col2 jest
#    pomiędzy 0.0 a 1.0 (włącznie).
df_col2_0_1 = df.loc[(df['col2'] >= 0) & (df['col2'] <= 1)]
df_col2_0_1
# df[df['col2].between(0.0,1.0)]

# %% Przypisz nową kolumnę o nazwie col3, która przyjmie wartość 1 w momencie
#    gdy w kolumnie col2 jest wartość nieujemna i przeciwnie -1.
df2 = pd.DataFrame({'col3':df['col2']})
df = pd.concat([df,df2], axis =1)
df['col3'] = df['col3'].where(df['col3'] >= 0, -1).where(df['col3'] < 0, 1).astype(int)
print(df)
# df['col3'] = df['col2'].map(lambda x: 1 if x >= 0 else -1)

# %% Przypisz nową kolumnę o nazwie col4, która przytnie wartości z kolumny
#    col2 do przedziału  [−1.0,1.0] . Innymi słowy, dla wartości poniżej -1.0
#     należy wstawić wartość -1.0, dla wartości powyżej 1.0 wartość 1.0.
df4 = pd.DataFrame({'col4':df['col2']})
df = pd.concat([df,df4], axis =1)
df['col4'] = df['col4'].where(df['col4'] >= -1, -1).where(df['col4'] < 1, 1)
print(df)
# df['col4'] = df['col2'].clip(-1.0, 1.0)

# %% Znajdź 5 największych wartości dla kolumny col2.
col_2_max_5 = df.nlargest(5, ['col2'])
col_2_max_5
# %% Znajdź 5 najmniejszych wartości dla kolumny col2.
col_2_min_5 = df.nsmallest(5, ['col2'])
col_2_min_5

# %% Wyznacz skumulowaną sumę dla każdej kolumny.
cumsum = pd.DataFrame.cumsum(df)
cumsum

# %% Wyznacz medianę dla zmiennej col2 (inaczej kwantyl rzędu 0.5).
col_2_median = df['col2'].median()
col_2_median

# %% Wytnij wiersze, dla których zmienna col2 przyjmuje wartości większe od 0.0
col2_higher_0 = df[df['col2'] > 0.0]
col2_higher_0
# df.query()

# %% Wytnij 5 pierwszych wierszy obiektu df i przekształć je do słownika.
rows_5 = df.loc[0:5]
dict_5_rows = rows_5.to_dict('series')
dict_5_rows

# %% Wytnij 5 pierwszych wierszy obiektu df, przekształć je do formatu
#    Markdown i przypisz do zmiennej df_markdown.
rows_5 = df.loc[0:5]
df_markdown = rows_5.to_markdown()
df_markdown
# %% Wydrukuj do konsoli zawartość zmiennej df_markdown.
print(df_markdown)


def getDeg(p, i):
    cans = 0
    while i % p == 0:
        cans += 1
        i //= p
    return cans

def getM(p, l):
    cans = 0
    for i in l:
        cans = max(cans, getDeg(p, i))
    return cans

def getNums(p):
    m5 = getM(5, p)
    m3 = getM(3, p)
    m2 = getM(2, p)
    ans = [5]*m5 + [3]*m3
    nd = 3 - len(ans)
    ans += [4]*(m2 - nd)
    nd = 3 - len(ans)
    ans += [2]*nd
    return ans

def getAns(l):
    return "\n".join(["".join(map(str, getNums(l[i]))) for i in range(len(l))])

def main():
    FIN = open("input.txt", "r")
    FOUT = open("output.txt", "w")
    T = int(FIN.readline())
    for i in range(T):
        r, n, m, k = map(int, FIN.readline().split())
        l = [list(map(int, FIN.readline().split())) for i in range(r)]
        print("Case {0}#\n{1}".format(i + 1, getAns(l)), file = FOUT)
    FIN.close()
    FOUT.close()

if __name__ == "__main__":
    main()


import re

class X:
    '''
    每一个标签具有的通用性质
    - 标签名
    - 以期望的形式打印本身信息
    - 标签行去除 TAG_NAME: 部分
    '''
    def __init__(self, TAG_NAME: str = 'X'):
        self.TAG_NAME = TAG_NAME
        self.known_attrs = {}

    def __repr__(self):
        return f'{self.TAG_NAME}'

    def __strip(self, line: str):
        # return line[len(self.TAG_NAME) + 1:]
        data = line.split(':', maxsplit=1)
        if len(data) == 2:
            _, no_tag_line = data
        elif len(data) == 0:
            raise 'm3u8格式错误 无法处理的异常'
        else:
            no_tag_line = data[0]
        return no_tag_line

    def get_tag_info(self, line: str):
        return self.__strip(line)

    def format_key(self, key: str):
        return key.replace('-', '_').lower()

    def convert_type(self, name: str, value: str, _type: type):
        self.__setattr__(self.format_key(name), _type(value))

    def regex_attrs(self, info: str) -> list:
        if info.endswith(',') is False:
            info += ','
        return re.findall('(.*?)=("[^"]*?"|[^,]*?),', info)

    def set_attrs_from_line(self, line: str):
        '''
        https://stackoverflow.com/questions/34081567
        re.findall('([A-Z]+[0-9]*)=("[^"]*"|[^,]*)', s)
        '''
        info = self.get_tag_info(line)
        for key, value in self.regex_attrs(info):
            value = value.strip('"')
            if key in self.known_attrs:
                if isinstance(self.known_attrs[key], str):
                    self.__setattr__(self.known_attrs[key], value)
                elif isinstance(self.known_attrs[key], type):
                    self.convert_type(key, value, self.known_attrs[key])
                else:
                    self.known_attrs[key](value)
            else:
                print(f'unknown attr -> {key} <- of {self.TAG_NAME}')
        return self