"""
Design-of-Experiments Driver.
"""
from __future__ import print_function

import traceback
import inspect

from openmdao.core.driver import Driver, RecordingDebugging
from openmdao.core.analysis_error import AnalysisError

from openmdao.utils.mpi import MPI

from openmdao.recorders.sqlite_recorder import SqliteRecorder

class DOEGenerator(object):
    """
    Base class for a callable object that generates cases for a DOEDriver.

    Attributes
    ----------
    _num_samples : int
        The number of samples generated (available after generator has been called).
    """

    def __init__(self):
        """
        Initialize the DOEGenerator.
        """
        self._num_samples = 0

    def __call__(self, design_vars):
        """
        Generate case.

        Parameters
        ----------
        design_vars : dict
            Dictionary of design variables for which to generate values.

        Returns
        -------
        list
            list of name, value tuples for the design variables.
        """
        return []

class DOEDriver(Driver):
    """
    Design-of-Experiments Driver.

    Attributes
    ----------
    _name : str
        The name used to identify this driver in recorded cases.
    _recorders : list
        List of case recorders that have been added to this driver.
    """

    def __init__(self, generator=None, **kwargs):
        """
        Constructor.

        Parameters
        ----------
        generator : DOEGenerator or None
            The case generator.

        **kwargs : dict of keyword arguments
            Keyword arguments that will be mapped into the Driver options.
        """
        if generator and not isinstance(generator, DOEGenerator):
            if inspect.isclass(generator):
                raise TypeError("DOEDriver requires an instance of DOEGenerator, "
                                "but a class object was found: %s"
                                % generator.__name__)
            else:
                raise TypeError("DOEDriver requires an instance of DOEGenerator, "
                                "but an instance of %s was found."
                                % type(generator).__name__)

        super(DOEDriver, self).__init__(**kwargs)

        if generator is not None:
            self.options['generator'] = generator

        self._name = ''
        self._recorders = []

    def _declare_options(self):
        """
        Declare options before kwargs are processed in the init method.
        """
        self.options.declare('generator', types=(DOEGenerator), default=DOEGenerator(),
                             desc='The case generator. If default, no cases are generated.')

        self.options.declare('parallel', default=False, types=(bool, int), lower=0,
                             desc='True or number of cases to run in parallel. '
                                  'If True, cases will be run on all available processors. '
                                  'If an integer, each case will get COMM.size/<number> '
                                  'processors and <number> of cases will be run in parallel')

    def _setup_comm(self, comm):
        """
        Perform any driver-specific setup of communicators for the model.

        Parameters
        ----------
        comm : MPI.Comm or <FakeComm> or None
            The communicator for the Problem.

        Returns
        -------
        MPI.Comm or <FakeComm> or None
            The communicator for the Problem model.
        """
        parallel = self.options['parallel']
        if MPI and parallel:
            self._comm = comm

            if parallel == 1:  # True == 1
                size = 1
                color = self._color = comm.rank
            else:
                comm_size = comm.size
                size = comm_size // parallel
                if comm_size != size * parallel:
                    raise RuntimeError("The number of processors is not evenly divisable by "
                                       "the specified number of parallel cases.\n Provide a "
                                       "number of processors that is a multiple of %d, or "
                                       "specify a number of parallel cases that divides "
                                       "into %d." % (parallel, comm_size))
                color = self._color = comm.rank % size

            model_comm = comm.Split(color)
        else:
            self._comm = None
            model_comm = comm

        return model_comm

    def _set_name(self):
        """
        Set the name of this DOE driver and its case generator.

        Returns
        -------
        str
            The name of this DOE driver and its case generator.
        """
        generator = self.options['generator']

        gen_type = type(generator).__name__.replace('Generator', '')
        if gen_type == 'DOEGenerator':
            self._name = 'DOEDriver'  # Empty generator
        else:
            self._name = 'DOEDriver_' + gen_type

        return self._name

    def _get_name(self):
        """
        Get the name of this DOE driver and its case generator.

        Returns
        -------
        str
            The name of this DOE driver and its case generator.
        """
        return self._name

    def run(self):
        """
        Generate cases and run the model for each set of generated input values.

        Returns
        -------
        boolean
            Failure flag; True if failed to converge, False is successful.
        """
        self.iter_count = 0

        # set driver name with current generator
        self._set_name()

        if self._comm:
            case_gen = self._parallel_generator
        else:
            case_gen = self.options['generator']

        for case in case_gen(self._designvars):
            self._run_case(case)
            self.iter_count += 1

        return False

    def _run_case(self, case):
        """
        Run case, save exception info and mark the metadata if the case fails.

        Parameters
        ----------
        case : list
            list of name, value tuples for the design variables.
        """
        metadata = {}

        for dv_name, dv_val in case:
            self.set_design_var(dv_name, dv_val)

        with RecordingDebugging(self._name, self.iter_count, self) as rec:
            try:
                failure_flag, _, _ = self._problem.model._solve_nonlinear()
                metadata['success'] = not failure_flag
                metadata['msg'] = ''
            except AnalysisError:
                metadata['success'] = 0
                metadata['msg'] = traceback.format_exc()
            except Exception:
                metadata['success'] = 0
                metadata['msg'] = traceback.format_exc()
                print(metadata['msg'])

            # save reference to metadata for use in record_iteration
            self._metadata = metadata

    def _parallel_generator(self, design_vars):
        """
        Generate case for this processor when running under MPI.

        Parameters
        ----------
        design_vars : dict
            Dictionary of design variables for which to generate values.

        Yields
        ------
        list
            list of name, value tuples for the design variables.
        """
        size = self._comm.size // self.options['parallel']
        color = self._color

        generator = self.options['generator']
        for i, case in enumerate(generator(design_vars)):
            if i % size == color:
                yield case

    def add_recorder(self, recorder):
        """
        Add a recorder to the driver.

        Parameters
        ----------
        recorder : BaseRecorder
           A recorder instance.
        """
        # keep track of recorders so we can flag them as parallel
        # if we end up running in parallel
        self._recorders.append(recorder)

        super(DOEDriver, self).add_recorder(recorder)

    def _setup_recording(self):
        """
        Set up case recording.
        """
        parallel = self.options['parallel']
        if MPI and parallel:
            for recorder in self._recorders:
                recorder._parallel = True

                # if SqliteRecorder, write cases only on procs up to the number
                # of parallel DOEs (i.e. on the root procs for the cases)
                if isinstance(recorder, SqliteRecorder):
                    if parallel is True or self._comm.rank < parallel:
                        recorder._record_on_proc = True
                    else:
                        recorder._record_on_proc = False

        super(DOEDriver, self)._setup_recording()

    def record_iteration(self):
        """
        Record an iteration of the current Driver.
        """
        if not self._rec_mgr._recorders:
            return

        # Get the data to record (collective calls that get across all ranks)
        opts = self.recording_options
        filt = self._filtered_vars_to_record

        if opts['record_desvars']:
            des_vars = self.get_design_var_values(filt['des'])
        else:
            des_vars = {}

        if opts['record_objectives']:
            obj_vars = self.get_objective_values(filt['obj'])
        else:
            obj_vars = {}

        if opts['record_constraints']:
            con_vars = self.get_constraint_values(filt['con'])
        else:
            con_vars = {}

        if opts['record_responses']:
            # res_vars = self.get_response_values(filt['res'])  # not really working yet
            res_vars = {}
        else:
            res_vars = {}

        model = self._problem.model

        sys_vars = {}
        in_vars = {}
        outputs = model._outputs
        inputs = model._inputs
        views = outputs._views
        views_in = inputs._views
        sys_vars = {name: views[name] for name in outputs._names if name in filt['sys']}
        if self.recording_options['record_inputs']:
            in_vars = {name: views_in[name] for name in inputs._names if name in filt['in']}

        outs = des_vars
        outs.update(res_vars)
        outs.update(obj_vars)
        outs.update(con_vars)
        outs.update(sys_vars)

        data = {
            'out': outs,
            'in': in_vars
        }

        self._rec_mgr.record_iteration(self, data, self._metadata)


import numpy as np

with open('in.txt') as f:
  lines = f.read().strip().splitlines()

s = set([0])
h = 10 * [0]

for line in lines:
  dir, cnt = line.split()
  cnt = int(cnt)
  for _ in range(cnt):
    h[0] += {
      'U': -1j,
      'D': 1j,
      'R': 1,
      'L': -1
    }[dir]
    for i in range(9):
      if abs(h[i + 1] - h[i]) < 2:
        break
      delta = h[i] - h[i + 1]
      h[i + 1] += np.sign(delta.real) + 1j * np.sign(delta.imag)
    s.add(h[-1])

print(len(s))


#! /usr/bin/env python
'''
A script that will compare two Dawn IMG files.
'''
import sys
import os
import os.path
import dawn

def main(argv=None):
    '''
    Receives two dawn image filenames from the command line and compares the
    data areas.
    '''
    if argv is None:
        argv = sys.argv

    direcory_name1 = argv[1]
    direcory_name2 = argv[2]

    compare_directories(direcory_name1, direcory_name2)

def compare_directories(directory1, directory2):
    '''
    Compares two directories of dawn image files.
    Both directories must have the same structure.
    '''
    dirlist1 = get_dir_list(directory1)
    dirlist2 = get_dir_list(directory2)

    dirdict1 = dict(dirlist1)
    dirdict2 = dict(dirlist2)

    merged_dict = merge_dicts(dirdict1, dirdict2)

    compare_contents(directory1, directory2, merged_dict)

def strip_version(filename):
    return filename[:-5]

def merge_dicts(dirdict1, dirdict2):
    result = {}
    for dirname in dirdict1.keys() + dirdict2.keys():
        if dirname in dirdict1 and dirname in dirdict2:
            filedict1 = dict([(strip_version(x), x) for x in dirdict1[dirname]])
            filedict2 = dict([(strip_version(x), x) for x in dirdict2[dirname]])

            result[dirname] = dict([
                (filedict1[x], filedict2[x])
                for x in filedict1.keys()
                if x in filedict1 and x in filedict2
            ])
    return result

def compare_contents(directory1, directory2, dirdict):
    for dirname in dirdict.keys():
        filepairs = dirdict[dirname]

        for filename1 in filepairs.keys():
            filename2 = filepairs[filename1]
            file1 = os.path.join(directory1, dirname, filename1)
            file2 = os.path.join(directory2, dirname, filename2)

            if os.path.exists(file1) and os.path.exists(file2):
                dawn.compare_files(file1, file2)
            elif os.path.exists(file1):
                print(file1 + " <- " + file2)
            elif os.path.exists(file2):
                print(file1 + " -> " + file2)
            else:
                print("No files exist")

def is_dawn(filename):
    return filename.lower().endswith('.img')

def get_dir_list(directory):
    walkresult = [(dirpath, [x for x in filenames if is_dawn(x)]) for dirpath, _, filenames in os.walk(directory)]
    return [(dirpath.replace(directory, '') , filenames) for dirpath, filenames in walkresult if filenames]

if __name__ == '__main__':
    sys.exit(main())


# https://leetcode.com/problems/reverse-linked-list/

from typing import Optional

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def reverseList_(self, head: Optional[ListNode]) -> ListNode: # input as list
        # empty head
        if len(head) <= 1:
            return head

        # first element in head
        first_node = ListNode(val = head[0], next=None)

        prev_node = first_node
        list_val = [first_node.val]

        # len(head) > 1
        if len(head) > 1:
            for i in range(1, len(head)):
                curr_node = ListNode(val = head[i], next=None)
                list_val.append(curr_node.val)
                prev_node.next = curr_node
                prev_node = curr_node

            # # traverse forward
            # next_node = first_node.next

            # while next_node != None:
            #     # print("Next: ", next_node.val)
            #     next_node = next_node.next

            # traverse reverse
            return list_val[::-1]

    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: # input as Listnode; only works on leetcode
        prev = None
        curr = head

        while curr:
            nxt = curr.next
            curr.next = prev
            prev = curr
            curr = nxt
        return prev

solved = Solution()
# print(solved.reverseList_(head = [1,2,3,4,5]))
# print(solved.reverseList_(head = [1,2]))
# print(solved.reverseList_(head = [-1]))
# print(solved.reverseList_(head = []))


'''
Write a Python program to calculate the area of regular polygon.

Input number of sides: 4
Input the length of a side: 25
The area of the polygon is: 625

'''
from math import *
n=int(input())
lenght=int(input())
perimetr=n*lenght
apothem=lenght/(2*(tan(pi/n)))
area=(apothem*perimetr)/2

print(round(area))


import re
import emoji

def preprocess_string(text):
    """
    입력받은 text 를 전처리 하는 함수.

    :param text: str
    :return : str

    """

    # 이모티콘부터 제거
    no_emoticon = ''
    for char in text:
        if char not in emoji.UNICODE_EMOJI:
            no_emoticon += char

    # 특수문자 기준 split
    no_punctuation = re.split(r'([!,?]+)|([.]+)|([,]+)|(["])|([\'])|([&]+)|([(]+)|([)]+)|([~]+)|([♡]+)|([☆,★]+)',
                              no_emoticon.strip())
    no_punctuation_text = []

    for string in no_punctuation:
        if (string == '') or (string is None): continue
        no_punctuation_text.append(string)

    no_punctuation_text = ' '.join(no_punctuation_text)

    # 단독으로 쓰인 자모음 분리
    split_char = re.split(r'([ㄱ-ㅣ0-9]+)', no_punctuation_text.strip())
    split_char = ' '.join(split_char)

    # 한국어에서 단독으로 자주 쓰이는 자모음 뭉치 분리
    split_char = re.split(r'([ㅎ]{2,})|([ㅜ,ㅠ]{2,})|([ㅗ]+)|([ㅋ,ㄱ,ㄲ]{2,})|\s+', split_char.strip())
    final_text = []
    for string in split_char:
        if (string == '') or (string is None): continue
        final_text.append(string)

    return ' '.join(final_text)


from sqlwrapper import gensql, dbget, dbput
import json
import datetime
def HOTEL_FD_POST_UPDATE_CheckinGuestArrivals(request):

   d = request.json
   res_id = d.get("Res_id")
   unique_id = d.get("Res_unique_id")
   pf_id = d.get("pf_id")
   a = {}
   RES_Log_Time = datetime.datetime.utcnow()+datetime.timedelta(hours=5, minutes=30)
   RES_Log_Time = RES_Log_Time.time().strftime("%H:%M:%S")
   print(RES_Log_Time)
   RES_Log_Date = datetime.datetime.utcnow().date()
   print(RES_Log_Date)
   RES_Log_Date = str(RES_Log_Date)
   arrival = dbget("select res_arrival, res_adults,res_room from reservation.res_reservation where res_id = '"+res_id+"' and pf_id = '"+pf_id+"' and res_unique_id = '"+unique_id+"'")

   arrival = json.loads(arrival)
   print(arrival)
   print(arrival[0]['res_arrival'],type(arrival[0]['res_arrival']))
   today_arrival = (arrival[0]['res_arrival'])
   adult = arrival[0]['res_adults']
   room = arrival[0]['res_room']
   print(room,type(room))

   print(today_arrival)
   if RES_Log_Date == today_arrival:
       p = {}
       p['res_id'] = res_id
       p['res_unique_id'] = unique_id
       sql_value = gensql('select','room_management.rm_queue_room','rm_queue',p)
       sql_value = json.loads(sql_value)

       if len(sql_value) != 0:
          psql = dbput("delete from room_management.rm_queue_room where res_id = '"+res_id+"' and res_unique_id = '"+unique_id+"'")
          print(psql)
       else:
          pass
       e = {}
       e['Res_id'] = res_id
       e['pf_id'] = pf_id
       e['res_unique_id'] = unique_id
       a['Res_guest_status'] = "checkin"

       sql_value = gensql('update','reservation.res_reservation',a,e)
       print(sql_value)
       res_id = e.get("Res_id")
       Emp_Id = '121'
       Emp_Firstname = "daisy"
       s = {}
       s['Emp_Id'] = Emp_Id
       s['Emp_Firstname'] = Emp_Firstname

       s['RES_Log_Date'] = RES_Log_Date
       s['RES_Log_Time'] = RES_Log_Time
       s['RES_Action_Type'] = "Checkin a guest"
       s['RES_Description'] = "Checked in a guest"
       s['Res_id'] = res_id
       sql_value = gensql('insert','reservation.res_activity_log',s)
       fo_status = "occupied"
       res_status = "checkin"
       sql_value = dbput("update room_management.rm_room_list set rm_fo_status = '"+fo_status+"',rm_reservation_status = '"+res_status+"',rm_fo_person = "+str(adult)+" where rm_room in ("+str(room)+")")

       print(sql_value)
       alertcount = json.loads(dbget("select count(*) from reservation.res_alert where res_id = '"+str(res_id)+"' \
                                      and res_unique_id = '"+str(unique_id)+"'"))
       print(alertcount)
       if alertcount[0]['count'] !=0:
          alertvalue = json.loads(dbget("select * from reservation.res_alert where res_id = '"+str(res_id)+"' \
                                      and res_unique_id = '"+str(unique_id)+"'"))
          return(json.dumps({'Status': 'Success', 'StatusCode': '200', 'alertvalue':alertvalue,'Return': 'Alert Got Successfully','ReturnCode':'AGS'}, sort_keys=True, indent=4))
       else:

         return(json.dumps({'Status': 'Success', 'StatusCode': '200','Return': 'Record Updated Successfully','ReturnCode':'RUS'}, sort_keys=True, indent=4))

   else:
      return(json.dumps({'Status': 'Success', 'StatusCode': '200','Return': 'Checkin a Today Guest arrivals only','ReturnCode':'CTG'}, sort_keys=True, indent=4))


from os import listdir
from PIL import Image

#list src pic
DIR = 'pic'

#print listing of images
img_list = listdir("pic")

#enter and calculate ratio
sh_ent = int(input("Shakal ratio (compress ratio):"))
sh = 100 - sh_ent

#work with image
for filename in img_list:
        outname = "out/" + filename
        filename = "pic/" + filename
        print(filename)
        img = Image.open(filename)
        #save with compress
        img.save(outname, "JPEG", quality=sh)


#Give the Big-O performance of the following code fragment:
def findRepeated(L):
  """
    determines whether all elements in a given list L are distinct
  """
  n=len(L)
  for i in range(n):
    for j in range(i+1, n):
      if L[i]==L[j]:
        return True
  return False


#! /bin/python

import os
import sys
import json

import luigi
import numpy as np
import nifty.tools as nt

import nifty
import nifty.graph.rag as nrag
from vigra.analysis import relabelConsecutive
from elf.segmentation.clustering import mala_clustering, agglomerative_clustering

import cluster_tools.utils.volume_utils as vu
import cluster_tools.utils.function_utils as fu
from cluster_tools.cluster_tasks import SlurmTask, LocalTask, LSFTask

#
# Agglomerate Tasks
#

# TODO it would be nice to be able to change the block shape compared to ws task
# so that we can agglomerate block boundaries.
# However, I am not sure how this interacts with the id-offsets, so haven't
# implemented this yet.
class AgglomerateBase(luigi.Task):
    """ Agglomerate base class
    """

    task_name = 'agglomerate'
    src_file = os.path.abspath(__file__)

    # input and output volumes
    input_path = luigi.Parameter()
    input_key = luigi.Parameter()
    output_path = luigi.Parameter()
    output_key = luigi.Parameter()
    have_ignore_label = luigi.BoolParameter()
    dependency = luigi.TaskParameter()

    def requires(self):
        return self.dependency

    @staticmethod
    def default_task_config():
        # parameter:
        # use_mala_agglomeration: whether to use thresholding based mala agglomeration
        #                         or element number based agglomerative clustering
        # threshold: threshold up to which to agglomerate (mala) or fraction of nodes
        #            after agglomeration (agglomerative clustering)
        # size_regularizer: size regularizer in agglomerative clustering (wardness)
        # invert_inputs: do we need to invert the inputs?
        # offsets: offsets for affinities, set to None for boundaries
        config = LocalTask.default_task_config()
        config.update({'use_mala_agglomeration': True, 'threshold': .9,
                       'size_regularizer': .5, 'invert_inputs': False,
                       'offsets': None})
        return config

    def clean_up_for_retry(self, block_list):
        super().clean_up_for_retry(block_list)
        # TODO remove any output of failed blocks because it might be corrupted

    def run_impl(self):
        # get the global config and init configs
        shebang, block_shape, roi_begin, roi_end = self.global_config_values()
        self.init(shebang)

        # get shape and make block config
        shape = vu.get_shape(self.input_path, self.input_key)
        if len(shape) == 4:
            shape = shape[1:]

        # load the agglomerate config
        config = self.get_task_config()

        # update the config with input and output paths and keys
        # as well as block shape
        config.update({'input_path': self.input_path, 'input_key': self.input_key,
                       'output_path': self.output_path, 'output_key': self.output_key,
                       'block_shape': block_shape, 'have_ignore_label': self.have_ignore_label})

        if self.n_retries == 0:
            block_list = vu.blocks_in_volume(shape, block_shape, roi_begin, roi_end)
        else:
            block_list = self.block_list
            self.clean_up_for_retry(block_list)

        self._write_log('scheduling %i blocks to be processed' % len(block_list))
        n_jobs = min(len(block_list), self.max_jobs)

        # prime and run the jobs
        self.prepare_jobs(n_jobs, block_list, config)
        self.submit_jobs(n_jobs)

        # wait till jobs finish and check for job success
        self.wait_for_jobs()
        self.check_jobs(n_jobs)

class AgglomerateLocal(AgglomerateBase, LocalTask):
    """
    Agglomerate on local machine
    """
    pass

class AgglomerateSlurm(AgglomerateBase, SlurmTask):
    """
    Agglomerate on slurm cluster
    """
    pass

class AgglomerateLSF(AgglomerateBase, LSFTask):
    """
    Agglomerate on lsf cluster
    """
    pass

#
# Implementation
#

def _agglomerate_block(blocking, block_id, ds_in, ds_out, config):
    fu.log("start processing block %i" % block_id)
    have_ignore_label = config['have_ignore_label']
    use_mala_agglomeration = config.get('use_mala_agglomeration', True)
    threshold = config.get('threshold', 0.9)
    size_regularizer = config.get('size_regularizer', .5)
    invert_inputs = config.get('invert_inputs', False)
    offsets = config.get('offsets', None)

    bb = vu.block_to_bb(blocking.getBlock(block_id))
    # load the segmentation / output
    seg = ds_out[bb]

    # check if this block is empty
    if np.sum(seg) == 0:
        fu.log_block_success(block_id)
        return

    # load the input data
    ndim_in = ds_in.ndim
    if ndim_in == 4:
        assert offsets is not None
        assert len(offsets) <= ds_in.shape[0]
        bb_in = (slice(0, len(offsets)),) + bb
        input_ = vu.normalize(ds_in[bb_in])
    else:
        assert offsets is None
        input_ = vu.normalize(ds_in[bb])

    if invert_inputs:
        input_ = 1. - input_

    id_offset = int(seg[seg != 0].min())

    # relabel the segmentation
    _, max_id, _ = relabelConsecutive(seg, out=seg, keep_zeros=True, start_label=1)
    seg = seg.astype('uint32')

    # construct rag
    rag = nrag.gridRag(seg, numberOfLabels=max_id + 1,
                       numberOfThreads=1)

    # extract edge features
    if offsets is None:
        edge_features = nrag.accumulateEdgeMeanAndLength(rag, input_, numberOfThreads=1)
    else:
        edge_features = nrag.accumulateAffinityStandartFeatures(rag, input_, offsets,
                                                                numberOfThreads=1)
    edge_features, edge_sizes = edge_features[:, 0], edge_features[:, -1]
    uv_ids = rag.uvIds()
    # set edges to ignore label to be maximally repulsive
    if have_ignore_label:
        ignore_mask = (uv_ids == 0).any(axis=1)
        edge_features[ignore_mask] = 1

    # build undirected graph
    n_nodes = rag.numberOfNodes
    graph = nifty.graph.undirectedGraph(n_nodes)
    graph.insertEdges(uv_ids)

    if use_mala_agglomeration:
        node_labels = mala_clustering(graph, edge_features,
                                      edge_sizes, threshold)
    else:
        node_ids, node_sizes = np.unique(seg, return_counts=True)
        if node_ids[0] != 0:
            node_sizes = np.concatenate([np.array([0]), node_sizes])
        n_stop = int(threshold * n_nodes)
        node_labels = agglomerative_clustering(graph, edge_features,
                                               node_sizes, edge_sizes,
                                               n_stop, size_regularizer)

    # run clusteting
    node_labels, max_id, _ = relabelConsecutive(node_labels, start_label=1, keep_zeros=True)

    fu.log("reduced number of labels from %i to %i" % (n_nodes, max_id + 1))

    # project node labels back to segmentation
    seg = nrag.projectScalarNodeDataToPixels(rag, node_labels, numberOfThreads=1)
    seg = seg.astype('uint64')
    # add offset back to segmentation
    seg[seg != 0] += id_offset

    ds_out[bb] = seg
    # log block success
    fu.log_block_success(block_id)

def agglomerate(job_id, config_path):
    fu.log("start processing job %i" % job_id)
    fu.log("reading config from %s" % config_path)
    with open(config_path, 'r') as f:
        config = json.load(f)

    # read the input cofig
    input_path = config['input_path']
    input_key = config['input_key']
    shape = list(vu.get_shape(input_path, input_key))
    if len(shape) == 4:
        shape = shape[1:]

    block_shape = list(config['block_shape'])
    block_list = config['block_list']

    # read the output config
    output_path = config['output_path']
    output_key = config['output_key']

    # get the blocking
    blocking = nt.blocking([0, 0, 0], shape, block_shape)

    # submit blocks
    with vu.file_reader(input_path, 'r') as f_in, vu.file_reader(output_path) as f_out:
        ds_in = f_in[input_key]
        assert ds_in.ndim in (3, 4)
        ds_out = f_out[output_key]
        assert ds_out.ndim == 3
        for block_id in block_list:
            _agglomerate_block(blocking, block_id, ds_in, ds_out, config)

    # log success
    fu.log_job_success(job_id)

if __name__ == '__main__':
    path = sys.argv[1]
    assert os.path.exists(path), path
    job_id = int(os.path.split(path)[1].split('.')[0].split('_')[-1])
    agglomerate(job_id, path)


from asyncio import sleep

from discord import Forbidden
from discord.ext import commands

from Utils.domain_tester import get_domain_embed
from Utils.file_tester import get_file_embed

class DmCommands(commands.Cog, name="Dm Commands"):
    """
    Cog including all Commands that are dm only
    """

    def __init__(self, b):
        self.b = b
        print("Dm Commands succesfully added to the bot!")

    @commands.command(name="check",
                      help="Takes given Input and runs a test over it. Only Dm Channels. Accepts URLs",
                      brief="Checks Input", aliases=["test"])
    async def check(self, ctx, *arg):
        if ctx.guild is not None:
            try:
                await ctx.message.delete()
                await ctx.author.send("Only DM Available")
            except Forbidden:
                await ctx.reply("Only DM Available! Warning! The Above message might be milicious. "
                                "Dont click the file/url until you trust it! (for some reason i cant delete it)")
            return
        if arg is None and not ctx.message.attachments:
            await ctx.send("Missing an url")
            return
        if ctx.message.attachments:
            await ctx.reply("Starting testing of files. This takes some time")
            for i in ctx.message.attachments:
                msgn = await ctx.reply("Stand by...")
                await msgn.edit(content=None, embed=await get_file_embed(i, ctx))
                await sleep(30)
        if len(arg) > 0:
            domain = arg[0]
            await ctx.reply(embed=get_domain_embed(domain, ctx))


from ...robot import Robot

from stt_watson.SttWatsonLogListener import SttWatsonLogListener
from recording.Record import Record
from watson_client.Client import Client
from utils.SignalHandler import SignalHandler

import threading
import signal
import os

class WatsonRobot(Robot):
    def __init__(self, config, speaker, actions):
        super(WatsonRobot, self).__init__(config, speaker, actions)
        config['audio-chunk'] = 8000
        config['audio-rate'] = 44100
        config['channels'] = 1

        self.listeners = []
        sttWatsonLogListener = SttWatsonLogListener()
        self.listeners.append(sttWatsonLogListener)
        self.stopper = threading.Event()
        self.record = Record(config, self.stopper)
        self.workers = [self.record]
        self.watsonClient = Client(config)
        self.handler = SignalHandler(self.stopper, self.workers)
        signal.signal(signal.SIGINT, self.handler)

    def name(self):
        return 'Watson'

    def listen(self):
        audioFd, writer = os.pipe()
        self.record.setWriter(writer)
        self.record.start()
        self.watsonClient.setListeners(self.listeners)
        self.watsonClient.startStt(audioFd)


import torch
from torch import nn
from torch.autograd import Variable
import numpy as np
from util import get_data
from torch.utils.data import DataLoader
from torch.nn import functional as F
from torch.optim import Adam
from variables import*
from matplotlib import pyplot as plt

class MnistRegression(object):
    def __init__(self, train_data, test_data):
        self.train_data = train_data
        self.test_data = test_data
        self.model = self.MnistModel()

    class MnistModel(nn.Module):
        def __init__(self):
            super().__init__()
            self.linear = nn.Linear(
                    in_features=input_shape,
                    out_features=output_shape
                    )

        def forward(self, x):
            x = x.reshape(-1, input_shape)
            x = self.linear(x)
            x = F.log_softmax(x, dim=1)
            return x

    def loss_fnc(self, Ypred, Y):
        return F.cross_entropy(Ypred, Y)

    def optimizer(self, learning_rate=0.1):
        return Adam(self.model.parameters(), lr=learning_rate)

    def evaluate(self, Y, Ypred):
        P = torch.argmax(Ypred, dim=1).numpy()
        Y = Y.numpy()
        return np.sum(Y == P)

    def train(self, num_epochs=100):
        opt = self.optimizer()
        total_train_loss = []
        total_test_loss = []
        for i in range(1,num_epochs+1):
            n_correct = 0
            n_total = 0
            for X, Y in self.train_data:
                Y = Y.to(dtype=torch.int64)
                Ypred = self.model(X)
                loss = self.loss_fnc(Ypred, Y)
                loss.backward() # calculate gradients
                total_train_loss.append(loss.item())

                n_correct += self.evaluate(Y, Ypred)
                n_total += batch_size

                opt.step() # update parameters using claculated gradients
                opt.zero_grad() # use to avoid accumilating the gradients

            train_acc = round(n_correct/n_total, 3)

            with torch.no_grad():
                n_correct = 0
                n_total = 0
                for X, Y in self.test_data:
                    Y = Y.to(dtype=torch.int64)
                    Ypred = self.model(X)
                    loss = self.loss_fnc(Ypred, Y)
                    total_test_loss.append(loss.item())

                    n_correct += self.evaluate(Y, Ypred)
                    n_total += batch_size

                test_acc = round(n_correct/n_total, 3)

            print("Train Acc : {} Test Acc : {}".format(train_acc, test_acc))

        plt.plot(total_train_loss, label='Train loss')
        plt.plot(total_test_loss , label='Test loss')
        plt.legend()
        plt.show()

if __name__ == "__main__":
    train_data, test_data = get_data()
    model = MnistRegression(train_data, test_data)
    model.train()


from python_celery_worker.services.db import db_engine

def update_task(id):
    """
    Update the task status in the database

    :param id:
    :return:
    """
    conn = db_engine.connect()
    conn.execute(
        'update tasks set status = %s, message = %s, updated_at = NOW() where id = %s',
        'completed',
        'Updated by celery worker!',
        id
    )


from collections import Counter

class Solution(object):
    def findAnagrams(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: List[int]
        """
        # anagram: str with same histgram

        res = []

        lp = len(p) -1
        ls = len(s)
        pCount = Counter(p)
        mCount = Counter(s[:lp]) # from 0 to lp - 2

        for i in range(lp, ls):
            mCount[s[i]]+=1
            if mCount == pCount:
                res.append(i-lp)
            mCount[s[i-lp]]-=1
            if mCount[s[i-lp]] == 0:
                del mCount[s[i-lp]]

        return res


#!/usr/bin/env python3
""" Problem 8.7 in CtCI book
"""
def permute(my_str):
  _permute("", my_str)

def _permute(so_far, remaining):
  if len(remaining) == 0:
    print(so_far)
  else:
    for i in range(len(remaining)):
      following = so_far + remaining[i]
      rest = remaining[:i] + remaining[i+1:]
      _permute(following, rest)


""" VirtualMachineHandler provides remote access to VirtualMachineDB

    The following methods are available in the Service interface:

    - insertInstance
    - declareInstanceSubmitted
    - declareInstanceRunning
    - instanceIDHeartBeat
    - declareInstanceHalting
    - getInstancesByStatus
    - declareInstancesStopping
    - getUniqueID( instanceID ) return cloud manager uniqueID form VMDIRAC instanceID

"""

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

import os
from subprocess import Popen, PIPE
import six

# DIRAC
from DIRAC import gLogger, S_ERROR, S_OK
from DIRAC.Core.DISET.RequestHandler import RequestHandler
from DIRAC.Core.Utilities.ThreadScheduler import gThreadScheduler
from DIRAC.ConfigurationSystem.Client.Helpers.Operations import Operations

# VMDIRAC
from VMDIRAC.WorkloadManagementSystem.DB.VirtualMachineDB import VirtualMachineDB
from VMDIRAC.Security import VmProperties
from VMDIRAC.Resources.Cloud.Utilities import STATE_MAP
from VMDIRAC.Resources.Cloud.ConfigHelper import getVMTypeConfig, getVMTypes
from VMDIRAC.Resources.Cloud.EndpointFactory import EndpointFactory
from VMDIRAC.WorkloadManagementSystem.Utilities.Utils import getProxyFileForCE

__RCSID__ = '$Id$'

# This is a global instance of the VirtualMachineDB class
gVirtualMachineDB = False

def initializeVirtualMachineManagerHandler(_serviceInfo):

  global gVirtualMachineDB

  gVirtualMachineDB = VirtualMachineDB()
  haltStalledInstances()
  checkStalledInstances()

  if gVirtualMachineDB._connected:
    gThreadScheduler.addPeriodicTask(60 * 15, checkStalledInstances)
    return S_OK()

  return S_ERROR()

def haltStalledInstances():

  result = gVirtualMachineDB.getInstancesByStatus('Stalled')
  if not result['OK']:
    return result

  uList = []
  for image in result['Value']:
    uList += result['Value'][image]

  stallingList = []
  for uID in uList:
    result = gVirtualMachineDB.getInstanceID(uID)
    if not result['OK']:
      continue
    stallingList.append(result['Value'])
  return haltInstances(stallingList)

def getCEInstances(siteList=None, ceList=None, vo=None):

  result = getVMTypes(siteList=siteList, ceList=ceList, vo=vo)
  if not result['OK']:
    return S_ERROR('Failed to get images from the CS')
  imageDict = result['Value']
  ceList = []
  for site in imageDict:
    for ce in imageDict[site]:
      result = EndpointFactory().getCE(site, ce)
      if not result['OK']:
        continue
      ceList.append((site, ce, result['Value']))

  nodeDict = {}
  for site, ceName, ce in ceList:
    result = ce.getVMNodes()
    if not result['OK']:
      continue
    for node in result['Value']:
      if not node.name.startswith('DIRAC'):
        continue
      ip = (node.public_ips[0] if node.public_ips else 'None')
      nodeState = node.state.upper() if not isinstance(node.state, six.integer_types) else STATE_MAP[node.state]
      nodeDict[node.id] = {"Site": site,
                           "CEName": ceName,
                           "NodeName": node.name,
                           "PublicIP": ip,
                           "State": nodeState}
  return S_OK(nodeDict)

def checkStalledInstances():
  """
   To avoid stalling instances consuming resources at cloud endpoint,
   attempts to halt the stalled list in the cloud endpoint
  """

  result = gVirtualMachineDB.declareStalledInstances()
  if not result['OK']:
    return result

  stallingList = result['Value']

  return haltInstances(stallingList)

def stopInstance(site, endpoint, nodeID):

  result = getVMTypeConfig(site, endpoint)
  if not result['OK']:
    return result
  ceParams = result['Value']
  ceFactory = EndpointFactory()
  result = ceFactory.getCEObject(parameters=ceParams)
  if not result['OK']:
    return result

  ce = result['Value']
  result = ce.stopVM(nodeID)
  return result

def createEndpoint(uniqueID):

  result = gVirtualMachineDB.getEndpointFromInstance(uniqueID)
  if not result['OK']:
    return result
  site, endpoint = result['Value'].split('::')

  result = getVMTypeConfig(site, endpoint)
  if not result['OK']:
    return result
  ceParams = result['Value']
  ceFactory = EndpointFactory()
  result = ceFactory.getCEObject(parameters=ceParams)
  return result

def haltInstances(vmList):
  """
   Common haltInstances for Running(from class VirtualMachineManagerHandler) and
   Stalled(from checkStalledInstances periodic task) to Halt
  """

  failed = {}
  successful = {}

  for instanceID in vmList:
    instanceID = int(instanceID)
    result = gVirtualMachineDB.getUniqueID(instanceID)
    if not result['OK']:
      gLogger.error('haltInstances: on getUniqueID call: %s' % result['Message'])
      continue
    uniqueID = result['Value']

    result = createEndpoint(uniqueID)
    if not result['OK']:
      gLogger.error('haltInstances: on createEndpoint call: %s' % result['Message'])
      continue

    endpoint = result['Value']

    # Get proxy to be used to connect to the cloud endpoint
    authType = endpoint.parameters.get('Auth')
    if authType and authType.lower() in ['x509', 'voms']:
      siteName = endpoint.parameters['Site']
      ceName = endpoint.parameters['CEName']
      gLogger.verbose("Getting cloud proxy for %s/%s" % (siteName, ceName))
      result = getProxyFileForCE(endpoint)
      if not result['OK']:
        continue
      endpoint.setProxy(result['Value'])

    result = endpoint.stopVM(uniqueID)
    if result['OK']:
      gVirtualMachineDB.recordDBHalt(instanceID, 0)
      successful[instanceID] = True
    else:
      failed[instanceID] = result['Message']

  return S_OK({"Successful": successful, "Failed": failed})

def getPilotOutput(pilotRef):

  if not pilotRef.startswith('vm://'):
    return S_ERROR('Invalid pilot reference %s' % pilotRef)

  # Get the VM public IP
  diracID, nPilot = os.path.basename(pilotRef).split(':')
  result = gVirtualMachineDB.getUniqueIDByName(diracID)
  if not result['OK']:
    return result
  uniqueID = result['Value']
  result = gVirtualMachineDB.getInstanceID(uniqueID)
  if not result['OK']:
    return result
  instanceID = result['Value']
  result = gVirtualMachineDB.getInstanceParameter("PublicIP", instanceID)
  if not result['OK']:
    return result
  publicIP = result['Value']

  op = Operations()
  privateKeyFile = op.getValue('/Cloud/PrivateKey', '')
  diracUser = op.getValue('/Cloud/VMUser', '')

  ssh_str = '%s@%s' % (diracUser, publicIP)
  cmd = ['ssh', '-i', privateKeyFile, ssh_str,
         "cat /etc/joboutputs/vm-pilot.%s.log" % nPilot]
  inst = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  output, stderr = inst.communicate()
  if inst.returncode:
    return S_ERROR('Failed to get pilot output: %s' % stderr)
  else:
    return S_OK(output)

class VirtualMachineManagerHandler(RequestHandler):

  def initialize(self):

    credDict = self.getRemoteCredentials()
    self.rpcProperties = credDict['properties']

  @staticmethod
  def __logResult(methodName, result):
    '''
    Method that writes to log error messages
    '''
    if not result['OK']:
      gLogger.error('%s: %s' % (methodName, result['Message']))

  types_getCEInstances = [(list, type(None)), (list, type(None)), six.string_types]

  def export_getCEInstances(self, siteList, ceList, vo):

    if not siteList:
      siteList = None
    return getCEInstances(siteList=siteList, ceList=ceList, vo=vo)

  types_stopInstance = [six.string_types, six.string_types, six.string_types]

  def export_stopInstance(self, site, endpoint, nodeID):

    return stopInstance(site, endpoint, nodeID)

  types_getPilotOutput = [six.string_types]

  def export_getPilotOutput(self, pilotReference):

    return getPilotOutput(pilotReference)

  types_checkVmWebOperation = [six.string_types]

  def export_checkVmWebOperation(self, operation):
    """
    return true if rpc has VM_WEB_OPERATION
    """
    if VmProperties.VM_WEB_OPERATION in self.rpcProperties:
      return S_OK('Auth')
    return S_OK('Unauth')

  types_insertInstance = [six.string_types, six.string_types, six.string_types, six.string_types, six.string_types]

  def export_insertInstance(self, uniqueID, imageName, instanceName, endpoint, runningPodName):
    """
    Check Status of a given image
    Will insert a new Instance in the DB
    """
    res = gVirtualMachineDB.insertInstance(uniqueID, imageName, instanceName, endpoint, runningPodName)
    self.__logResult('insertInstance', res)

    return res

  types_getUniqueID = [six.string_types]

  def export_getUniqueID(self, instanceID):
    """
    return cloud manager uniqueID from VMDIRAC instanceID
    """
    res = gVirtualMachineDB.getUniqueID(instanceID)
    self.__logResult('getUniqueID', res)

    return res

  types_getUniqueIDByName = [six.string_types]

  def export_getUniqueIDByName(self, instanceName):
    """
    return cloud manager uniqueID from VMDIRAC name
    """
    result = gVirtualMachineDB.getUniqueIDByName(instanceName)
    self.__logResult('getUniqueIDByName', result)

    return result

  types_setInstanceUniqueID = [six.integer_types, six.string_types]

  def export_setInstanceUniqueID(self, instanceID, uniqueID):
    """
    Check Status of a given image
    Will insert a new Instance in the DB
    """
    res = gVirtualMachineDB.setInstanceUniqueID(instanceID, uniqueID)
    self.__logResult('setInstanceUniqueID', res)

    return res

  types_declareInstanceSubmitted = [six.string_types]

  def export_declareInstanceSubmitted(self, uniqueID):
    """
    After submission of the instance the Director should declare the new Status
    """
    res = gVirtualMachineDB.declareInstanceSubmitted(uniqueID)
    self.__logResult('declareInstanceSubmitted', res)

    return res

  types_declareInstanceRunning = [six.string_types, six.string_types]

  def export_declareInstanceRunning(self, uniqueID, privateIP):
    """
    Declares an instance Running and sets its associated info (uniqueID, publicIP, privateIP)
    Returns S_ERROR if:
      - instanceName does not have a "Submitted" entry
      - uniqueID is not unique
    """
    gLogger.info('Declare instance Running uniqueID: %s' % (uniqueID))
    if VmProperties.VM_RPC_OPERATION not in self.rpcProperties:
      return S_ERROR("Unauthorized declareInstanceRunning RPC")

    publicIP = self.getRemoteAddress()[0]
    gLogger.info('Declare instance Running publicIP: %s' % (publicIP))

    res = gVirtualMachineDB.declareInstanceRunning(uniqueID, publicIP, privateIP)
    self.__logResult('declareInstanceRunning', res)

    return res

  types_instanceIDHeartBeat = [six.string_types, float, six.integer_types,
                               six.integer_types, six.integer_types]

  def export_instanceIDHeartBeat(self, uniqueID, load, jobs,
                                 transferredFiles, transferredBytes, uptime=0):
    """
    Insert the heart beat info from a running instance
    It checks the status of the instance and the corresponding image
    Declares "Running" the instance and the image
    It returns S_ERROR if the status is not OK
    """
    if VmProperties.VM_RPC_OPERATION not in self.rpcProperties:
      return S_ERROR("Unauthorized declareInstanceIDHeartBeat RPC")

    try:
      uptime = int(uptime)
    except ValueError:
      uptime = 0

    res = gVirtualMachineDB.instanceIDHeartBeat(uniqueID, load, jobs,
                                                transferredFiles, transferredBytes, uptime)
    self.__logResult('instanceIDHeartBeat', res)

    return res

  types_declareInstancesStopping = [list]

  def export_declareInstancesStopping(self, instanceIdList):
    """
    Declares "Stopping" the instance because the Delete button of Browse Instances
    The instanceID is the VMDIRAC VM id
    When next instanceID heat beat with stopping status on the DB the VM will stop the job agent and terminates properly
    It returns S_ERROR if the status is not OK
    """
    if VmProperties.VM_WEB_OPERATION not in self.rpcProperties:
      return S_ERROR("Unauthorized VM Stopping")

    for instanceID in instanceIdList:
      gLogger.info('Stopping DIRAC instanceID: %s' % (instanceID))
      result = gVirtualMachineDB.getInstanceStatus(instanceID)
      if not result['OK']:
        self.__logResult('declareInstancesStopping on getInstanceStatus call: ', result)
        return result
      state = result['Value']
      gLogger.info('Stopping DIRAC instanceID: %s, current state %s' % (instanceID, state))

      if state == 'Stalled':
        result = gVirtualMachineDB.getUniqueID(instanceID)
        if not result['OK']:
          self.__logResult('declareInstancesStopping on getUniqueID call: ', result)
          return result
        uniqueID = result['Value']
        result = gVirtualMachineDB.getEndpointFromInstance(uniqueID)
        if not result['OK']:
          self.__logResult('declareInstancesStopping on getEndpointFromInstance call: ', result)
          return result
        endpoint = result['Value']

        result = self.export_declareInstanceHalting(uniqueID, 0)
      elif state == 'New':
        result = gVirtualMachineDB.recordDBHalt(instanceID, 0)
        self.__logResult('declareInstanceHalted', result)
      else:
        # this is only aplied to allowed trasitions
        result = gVirtualMachineDB.declareInstanceStopping(instanceID)
        self.__logResult('declareInstancesStopping: on declareInstanceStopping call: ', result)

    return result

  types_declareInstanceHalting = [six.string_types, float]

  def export_declareInstanceHalting(self, uniqueID, load):
    """
    Insert the heart beat info from a halting instance
    The VM has the uniqueID, which is the Cloud manager VM id
    Declares "Halted" the instance and the image
    It returns S_ERROR if the status is not OK
    """
    if VmProperties.VM_RPC_OPERATION not in self.rpcProperties:
      return S_ERROR("Unauthorized declareInstanceHalting RPC")

    endpoint = gVirtualMachineDB.getEndpointFromInstance(uniqueID)
    if not endpoint['OK']:
      self.__logResult('declareInstanceHalting', endpoint)
      return endpoint
    endpoint = endpoint['Value']

    result = gVirtualMachineDB.declareInstanceHalting(uniqueID, load)
    if not result['OK']:
      if "Halted ->" not in result["Message"]:
        self.__logResult('declareInstanceHalting on change status: ', result)
        return result
      else:
        gLogger.info("Bad transition from Halted to something, will assume Halted")

    haltingList = []
    instanceID = gVirtualMachineDB.getInstanceID(uniqueID)
    if not instanceID['OK']:
      self.__logResult('declareInstanceHalting', instanceID)
      return instanceID
    instanceID = instanceID['Value']
    haltingList.append(instanceID)

    return haltInstances(haltingList)

  types_getInstancesByStatus = [six.string_types]

  def export_getInstancesByStatus(self, status):
    """
    Get dictionary of Image Names with InstanceIDs in given status
    """

    res = gVirtualMachineDB.getInstancesByStatus(status)
    self.__logResult('getInstancesByStatus', res)
    return res

  types_getAllInfoForUniqueID = [six.string_types]

  def export_getAllInfoForUniqueID(self, uniqueID):
    """
    Get all the info for a UniqueID
    """
    res = gVirtualMachineDB.getAllInfoForUniqueID(uniqueID)
    self.__logResult('getAllInfoForUniqueID', res)

    return res

  types_getInstancesContent = [dict, (list, tuple),
                               six.integer_types, six.integer_types]

  def export_getInstancesContent(self, selDict, sortDict, start, limit):
    """
    Retrieve the contents of the DB
    """
    res = gVirtualMachineDB.getInstancesContent(selDict, sortDict, start, limit)
    self.__logResult('getInstancesContent', res)

    return res

  types_getHistoryForInstanceID = [six.integer_types]

  def export_getHistoryForInstanceID(self, instanceId):
    """
    Retrieve the contents of the DB
    """
    res = gVirtualMachineDB.getHistoryForInstanceID(instanceId)
    self.__logResult('getHistoryForInstanceID', res)

    return res

  types_getInstanceCounters = [six.string_types, dict]

  def export_getInstanceCounters(self, groupField, selDict):
    """
    Retrieve the contents of the DB
    """
    res = gVirtualMachineDB.getInstanceCounters(groupField, selDict)
    self.__logResult('getInstanceCounters', res)

    return res

  types_getHistoryValues = [int, dict]

  def export_getHistoryValues(self, averageBucket, selDict, fields2Get=None, timespan=0):
    """
    Retrieve the contents of the DB
    """
    if not fields2Get:
      fields2Get = []
    res = gVirtualMachineDB.getHistoryValues(averageBucket, selDict, fields2Get, timespan)
    self.__logResult('getHistoryValues', res)

    return res

  types_getRunningInstancesHistory = [int, int]

  def export_getRunningInstancesHistory(self, timespan, bucketSize):
    """
    Retrieve number of running instances in each bucket
    """
    res = gVirtualMachineDB.getRunningInstancesHistory(timespan, bucketSize)
    self.__logResult('getRunningInstancesHistory', res)

    return res

  types_getRunningInstancesBEPHistory = [int, int]

  def export_getRunningInstancesBEPHistory(self, timespan, bucketSize):
    """
    Retrieve number of running instances in each bucket by End-Point History
    """
    res = gVirtualMachineDB.getRunningInstancesBEPHistory(timespan, bucketSize)
    self.__logResult('getRunningInstancesBEPHistory', res)

    return res

  types_getRunningInstancesByRunningPodHistory = [int, int]

  def export_getRunningInstancesByRunningPodHistory(self, timespan, bucketSize):
    """
    Retrieve number of running instances in each bucket by Running Pod History
    """
    res = gVirtualMachineDB.getRunningInstancesByRunningPodHistory(timespan, bucketSize)
    self.__logResult('getRunningInstancesByRunningPodHistory', res)

    return res

  types_getRunningInstancesByImageHistory = [int, int]

  def export_getRunningInstancesByImageHistory(self, timespan, bucketSize):
    """
    Retrieve number of running instances in each bucket by Running Pod History
    """
    res = gVirtualMachineDB.getRunningInstancesByImageHistory(timespan, bucketSize)
    self.__logResult('getRunningInstancesByImageHistory', res)

    return res


def nextInt(): return int(input())
def nextInts(): return map(int, input().split())
def nextIntList(): return list(nextInts())

MOD = 10**5
def calc(x):
    y = 0
    x_c = x
    while x_c > 0:
        x_c, y_c = divmod(x_c, 10)
        y += y_c
    return (x + y) % MOD

def solve():
    N, K = nextInts()
    start_list = [N]
    routine_list = list()
    count = 0
    routine_flag = False

    # find until routine
    while True:
        N = calc(N)
        count += 1
        if count == K:
            break
        if routine_flag:
            if N == routine_list[0]:
                break
            routine_list.append(N)
        else:
            if N in start_list:
                routine_flag = True
                routine_list.append(N)
            start_list.append(N)
    if count == K:
        print(N)
        return

    # calc routine
    rem = K - count
    pos = rem % len(routine_list)
    print(routine_list[pos])

solve()


from setuptools import setup

import dorm

with open("README.md", "r") as readme:
    long_description = readme.read()

setup(
    name="dorm",
    version=dorm.version,
    description="A tiny SQLite ORM for Python.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    author="Dan Watson",
    author_email="dcwatson@gmail.com",
    url="https://github.com/dcwatson/dorm",
    license="MIT",
    py_modules=["dorm"],
    entry_points={"console_scripts": ["dorm=dorm:main"]},
    classifiers=[
        "Development Status :: 2 - Pre-Alpha",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Topic :: Database",
    ],
)


"""empty message

Revision ID: 41124ac6e47e

Revises: 57296b50c499

Create Date: 2014-11-30 17:08:44.396000

"""

# revision identifiers, used by Alembic.

revision = '41124ac6e47e'

down_revision = '57296b50c499'

from alembic import op

import sqlalchemy as sa

def upgrade():

    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('provider', sa.Column('address', sa.String(length=250), nullable=True))
    op.add_column('provider', sa.Column('emails', sa.String(length=250), nullable=True))
    ### end Alembic commands ###

def downgrade():

    ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('provider', 'emails')
    op.drop_column('provider', 'address')
    ### end Alembic commands ###


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

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('student', '0002_userinfo_grade'),
    ]

    operations = [
        migrations.CreateModel(
            name='Events',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=80)),
                ('date', models.DateTimeField()),
                ('cost', models.DecimalField(max_digits=6, decimal_places=2)),
            ],
        ),
        migrations.AddField(
            model_name='userinfo',
            name='balance',
            field=models.DecimalField(default=0.0, max_digits=6, decimal_places=2),
            preserve_default=False,
        ),
    ]


from django import template
from django.urls import NoReverseMatch, reverse

from utilities.utils import get_viewname, prepare_cloned_fields

register = template.Library()

#
# Instance buttons
#

@register.inclusion_tag('buttons/clone.html')
def clone_button(instance):
    url = reverse(get_viewname(instance, 'add'))

    # Populate cloned field values
    param_string = prepare_cloned_fields(instance).urlencode()
    if param_string:
        url = f'{url}?{param_string}'

    return {
        'url': url,
    }

@register.inclusion_tag('buttons/edit.html')
def edit_button(instance):
    viewname = get_viewname(instance, 'edit')
    url = reverse(viewname, kwargs={'pk': instance.pk})

    return {
        'url': url,
    }

@register.inclusion_tag('buttons/delete.html')
def delete_button(instance):
    viewname = get_viewname(instance, 'delete')
    url = reverse(viewname, kwargs={'pk': instance.pk})

    return {
        'url': url,
    }

#
# List buttons
#

@register.inclusion_tag('buttons/add.html')
def add_button(model, action='add'):
    try:
        url = reverse(get_viewname(model, action))
    except NoReverseMatch:
        url = None

    return {
        'url': url,
    }

@register.inclusion_tag('buttons/import.html')
def import_button(model, action='import'):
    try:
        url = reverse(get_viewname(model, action))
    except NoReverseMatch:
        url = None

    return {
        'url': url,
    }

@register.inclusion_tag('buttons/bulk_edit.html')
def bulk_edit_button(model, action='bulk_edit', query_params=None):
    try:
        url = reverse(get_viewname(model, action))
        if query_params:
            url = f'{url}?{query_params.urlencode()}'
    except NoReverseMatch:
        url = None

    return {
        'url': url,
    }

@register.inclusion_tag('buttons/bulk_delete.html')
def bulk_delete_button(model, action='bulk_delete', query_params=None):
    try:
        url = reverse(get_viewname(model, action))
        if query_params:
            url = f'{url}?{query_params.urlencode()}'
    except NoReverseMatch:
        url = None

    return {
        'url': url,
    }


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from rp_ui_harness import RequestPolicyTestCase

TEST_URL = "http://www.maindomain.test/link_1.html"
PREF_DEFAULT_ALLOW = "extensions.requestpolicy.defaultPolicy.allow"

class TestOpenInCurrentTab(RequestPolicyTestCase):

    def setUp(self):
        RequestPolicyTestCase.setUp(self)
        self.prefs.set_pref(PREF_DEFAULT_ALLOW, False)

    def test_open_in_current_tab(self):
        with self.marionette.using_context("content"):
            # load the test url
            self.marionette.navigate(TEST_URL)

            # find the URL string and its wrapping element
            url_wrapper = self.marionette.find_element("id", "text_url_1")
            url = url_wrapper.text

            # select the URL
            self.web_utils.select_element_text(url_wrapper)

        with self.marionette.using_context("chrome"):
            # perform right-click and entry selection
            self.ctx_menu.select_entry(
                "context-openlinkincurrent", url_wrapper)

            tab = self.browser.tabbar.selected_tab
            self.tabs.wait_until_loaded(tab)

            self.assertFalse(self.redir.is_shown(),
                             "Following the URL didn't cause a redirect.")
            self.assertEqual(tab.location, url,
                             "The location is correct.")


#!/usr/bin/python

import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate

a=0.7
b=0.6

X = np.arange(0,2.4,0.2)
Y = np.arange(0,2.4,0.2)
m,p =  np.meshgrid(X,Y)
mdot = np.divide(1,1+np.square(p))- np.multiply(b,m)
pdot = np.subtract(m,np.multiply(a,p))

fig, ax = plt.subplots()
q=ax.quiver(p,m,pdot,mdot)
ax.quiverkey(q,X=0.3,Y=2.4, U=5,
             label='Quiver key, length = 5', labelpos='E')
ax.plot(p,np.multiply(a,p))
ax.plot(p, np.divide( 1, np.multiply(b,(1+np.square(p)))))
ax.set_xlabel('p')
ax.set_ylabel('m')

def dydt_autoinhib(t,y,a,b):
    y1,y2=y
    dy1 = 1/(1+y2**2)-b*y1
    dy2 = y1-a*y2
    return (dy1,dy2)

# lambda trick so we can pass the right function into the solver
dydt_params = lambda t,y: dydt_autoinhib(t,y,a,b)

solution1 = scipy.integrate.solve_ivp(dydt_params, t_span=(0,20),y0=(2,2), method='RK45')
t1_ode45 = solution1.t
m1_ode45 = solution1.y[0]
p1_ode45 = solution1.y[1]
ax.plot(p1_ode45,m1_ode45)
plt.show()


import numpy as np
import matplotlib.pyplot as plt
from scipy import fftpack, signal

# 고주파 성분만 날리는 fft
# def get_filtered_data(in_data, filter_value=0.004):
def del_high_freq(in_data, filter_value=0.004):
    """
    :param in_data: 대상 시계열 신호
    :param filter_value: filter_value이상의 주파수를 가지는 신호를 날림
    :return: fft 결과
    """
    sig_fft = fftpack.fft(in_data)
    sample_freq = fftpack.fftfreq(in_data.size)
    high_freq_fft = sig_fft.copy()

    high_freq_fft[np.abs(sample_freq) > filter_value] = 0
    filtered_data = fftpack.ifft(high_freq_fft)

    return filtered_data

# 고주파, 저주파 성분을 날리는 fft
def del_high_and_low_freq(in_data, high_filter_value, low_filter_value):
    """
    :param in_data: 대상 시계열 신호
    :param high_filter_value: fft를 수행할 최대값, low_filter_value ~ high_filter_value값 사이의 신호를 fft
    :param low_filter_value: fft를 수행할 최소값
    :return: fft 결과
    """
    sig_fft = fftpack.fft(in_data)
    sample_freq = fftpack.fftfreq(in_data.size)
    high_freq_fft = sig_fft.copy()

    low_value1 = np.max(high_freq_fft)
    high_freq_fft[np.abs(sample_freq) > high_filter_value] = 0
    high_freq_fft[np.abs(sample_freq) < low_filter_value] = 0

    low_value2 = np.max(high_freq_fft)
    filtered_data = fftpack.ifft(high_freq_fft)

    return filtered_data, low_value1, low_value2

def fft(pupil_list, minu=None, quar=None):
    global section_frames, time

    # 데이터에서 0, -1인 부분 제거
    while 0 in pupil_list:
        pupil_list.remove(0)
    while -1 in pupil_list:
        pupil_list.remove(-1)

    if minu is not None:
        time = minu * 1800
        section_frames = len(pupil_list) // time

    if quar is not None:
        time = len(pupil_list) // quar
        section_frames = quar

    y = np.array(pupil_list)

    # fft
    # filtered_sig = del_high_freq(y, filter_value=0.005)  # 고주파 필터링
    filtered_sig, _, _ = del_high_and_low_freq(y, 0.0048, 0.0035)  # 저주파, 고주파 필터링
    filtered_sig = filtered_sig.astype(np.float)

    # zero-crossing point
    zero_crossings = np.where(np.diff(np.sign(np.diff(filtered_sig))))[0]
    zero_crossings = np.insert(zero_crossings, 0, 0)
    zero_crossings = np.append(zero_crossings, len(filtered_sig) - 1)

    # 변화 속도 계산
    change_rates_list = [[] for _ in range(section_frames)]
    for section in range(section_frames):
        # zero-crossing points 기준으로 원하는 위치(섹션) 가져오기
        section_zero_crossing = zero_crossings[np.where(zero_crossings <= (section + 1) * time)]
        section_zero_crossing = section_zero_crossing[np.where(section * time < section_zero_crossing)]
        # 변화 속도 계산
        for j in range(len(section_zero_crossing) - 1):
            change_rate = abs((filtered_sig[section_zero_crossing[j + 1]] - filtered_sig[section_zero_crossing[j]]) / (
                        section_zero_crossing[j + 1] - section_zero_crossing[j]))
            change_rates_list[section].append(change_rate)

    return filtered_sig, zero_crossings, section_frames, change_rates_list

# fft를 수행한 결과 그래프 그리기
def draw_fft_graph(y, filtered_sig, zero_crossings, section_frames, savepath, minu=None, quar=None):
    global time
    x = np.arange(0, len(y))
    if minu is not None:
        time = minu * 1800
        section_frames = len(y) // time

    if quar is not None:
        time = len(y) // quar
        section_frames = quar

    fig = plt.figure(dpi=150)

    # plt.figure(figsize=(6, 5))
    plt.rcParams["font.family"] = 'Malgun Gothic'
    plt.figure(figsize=(14, 6))
    plt.plot(x, y, label='Original signal')
    plt.plot(x, filtered_sig, linewidth=2, label='Filtered signal')
    # plt.plot(zero_crossings, filtered_sig[zero_crossings], marker='o', color='red', linestyle='--')
    plt.legend(loc='upper right')

    # 섹션 나눠진거 표시
    for section in range(section_frames):
        plt.axvline(x=section * time, ymin=0, ymax=1.0, color='r')
        plt.axvline(x=(section + 1) * time, ymin=0, ymax=1.0, color='r')

    # plt.xlim(0, 1800)
    plt.title('동공크기 변화율')
    plt.xlabel('Frame')
    plt.ylabel('Pupil size')
    plt.savefig(f'{savepath}')
    plt.show()

# 2차식 추세선 그리기, 히스토그램 그래프 저장
def draw_trendline_fft(data, title, y_lim, y_label, savepath, quar = None, avg = False):
    results = {}

    # 추세선
    x = np.arange(0, len(data))
    y = []
    for idx, value in enumerate(data):
        y.append(value)
    y = np.array(y)  # 10개 구간에 해당하는 특징(깜빡임 횟수)

    fit = np.polyfit(x, y, 2)
    a = fit[0]
    b = fit[1]
    c = fit[2]
    fit_equation = a * np.square(x) + b * x + c
    results['coeffs'] = fit.tolist()

    # r-squared
    p = np.poly1d(fit)

    # fit values, and mean
    yhat = p(x)
    ybar = np.sum(y) / len(y)
    ssreg = np.sum((yhat - ybar) ** 2)
    sstot = np.sum((y - ybar) ** 2)
    results['r-squared'] = ssreg / sstot
    r_squared = str(round(results['r-squared'], 3))  # 출력하기 위해 문자열로 변환
    a = str(round(results['coeffs'][0], 3))
    b = str(round(results['coeffs'][1], 3))
    c = str(round(results['coeffs'][2], 3))
    # print("R 제곱값: ", round(results['r-squared'], 3))
    # print("추세선: "+"Y="+a+"xX^2 + "+b+"xX + "+c)

    period = ['0~3분', '3~6분', '6~9분', '9~12분', '12~15분', '15~18분', '18~21분', '21~24분', '24~27분', '27~30분', '30~33분']
    plt.rcParams["font.family"] = 'Malgun Gothic'

    fig = plt.figure(dpi=150)
    ax = fig.add_subplot(1, 1, 1)

    for idx2, value2 in enumerate(data):
        ax.bar(period[idx2], value2, color='b', alpha=0.5)
    ax.plot(x, fit_equation, color='r', alpha=0.5, label='Polynomial fit', linewidth=3.0)
    # ax.scatter(x, y, s = 5, color = 'b', label = 'Data points')  # 추세선 예측에 사용한 좌표 그리기

    # Plotting
    plt.xticks(rotation=20)
    plt.title(f'{title}')
    plt.ylim(0, y_lim)
    plt.xlabel('구간')
    plt.ylabel(f'{y_label}')

    # 동공 크기 변화율 출력할 때 위치 조정
    if not avg:
        plt.text(3.2, 0.055, "추세선: " + r'$y = $' + a + r'$x^2 + ($' + b + r'$)x + $' + c, fontdict={'size': 12})
        plt.text(7.5, 0.05, r'$R^2 =$' + r_squared, fontdict={'size': 12})

    # 평균 동공크기 변화율 출력할 때 위치 조정
    else:
        plt.text(3.2, 0.027, "추세선: " + r'$y = $' + a + r'$x^2 + ($' + b + r'$)x + $' + c, fontdict={'size': 12})
        plt.text(7.5, 0.025, r'$R^2 =$' + r_squared, fontdict={'size': 12})

    plt.tight_layout()
    fig.canvas.draw()
    img = np.array(fig.canvas.renderer._renderer)
    spl = title.split('.')[0]
    plt.savefig(f'{savepath}')
    plt.imshow(img)
    plt.show()  # 그래프 잘 나오는지 띄우기


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

def normalize_l2(x):
    """
    Expects x.shape == [N, C, H, W]
    """
    norm = torch.norm(x.view(x.size(0), -1), p=2, dim=1)
    norm = norm.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1)
    return x / norm

def pair_cos_dist(x, y):
    cos = nn.CosineSimilarity(dim=-1, eps=1e-6)
    c = torch.clamp(1 - cos(x, y), min=0)
    return c

class Feature_Targets(nn.Module):
    def __init__(self, epsilon, num_steps, step_size, data_min = -1.0, data_max = 1.0, grad_sign=True, random_start = True):
        super().__init__()
        self.epsilon = epsilon
        self.num_steps = num_steps
        self.step_size = step_size
        self.grad_sign = grad_sign
        self.data_min = data_min
        self.data_max = data_max
        self.random_start = random_start

    def forward(self, model, bx, by, target_bx):
        """
        :param model: the classifier's forward method
        :param bx: batch of images
        :param by: true labels
        :return: perturbed batch of images
        """
        adv_bx = bx.detach().clone()
        target = target_bx.detach().clone()
        if self.random_start:
            adv_bx += torch.zeros_like(adv_bx).uniform_(-self.epsilon, self.epsilon)
            adv_bx = adv_bx.clamp(self.data_min, self.data_max)

        target_feature, target_logits = model(target)

        for i in range(self.num_steps):
            adv_bx.requires_grad_()
            with torch.enable_grad():
                feature, logits = model(adv_bx)
                loss = pair_cos_dist(feature, target_feature).mean()
            grad = torch.autograd.grad(loss, adv_bx, only_inputs=True)[0]

            if self.grad_sign:
                adv_bx = adv_bx.detach() + self.step_size * torch.sign(grad.detach())
            else:
                grad = normalize_l2(grad.detach())
                adv_bx = adv_bx.detach() + self.step_size * grad

            adv_bx = torch.min(torch.max(adv_bx, bx - self.epsilon), bx + self.epsilon).clamp(self.data_min, self.data_max)

        return adv_bx


import sys
#sys.path.append('/usr/local/Cellar/opencv3/3.2.0/lib/python2.7/site-packages')
sys.path.append("/usr/local/Cellar/opencv3/3.2.0/lib/python3.5/site-packages")
import cv2
import numpy as np
import os
import random

def show_image(im):
    height, width = im.shape[:2]
    res = cv2.resize(im,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)
    cv2.imshow("Image", res)

def show_imageOrig(im):
    height, width = im.shape[:2]
    res = cv2.resize(im,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)
    cv2.imshow("ImageOrig", res)

#kernel = np.ones((3,3),np.uint8)

#cap = cv2.VideoCapture("dogs.mp4")
#ret, frame1 = cap.read()
#prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
#hsv = np.zeros_like(frame1)
#hsv[...,1] = 255

#fgbg = cv2.createBackgroundSubtractorMOG2(50, 16, False)

#fgbg.setBackgroundRatio(0.8) # frames before object becomes foreground
#fgbg.setVarInit(500) # speed of adaption of new components

#i = 0
#while(1):
#    ret, frame2 = cap.read()
#    ret, frame2 = cap.read()
#    frame2 = cv2.GaussianBlur(frame2,(9,9),0)

#    fgmask = fgbg.apply(frame2)
#    fgmask = fgbg.apply(frame2,fgmask, 0)
    #fgmask = cv2.dilate(fgmask,kernel,iterations = 5)
    #fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel)
    '''
    ^^ Line above may or may not be good
    '''
    #if (i > 10 and i % 2 == 0):
        #cv2.imwrite(str(i) + ".png",fgmask)
#    show_image(fgmask)

#    k = cv2.waitKey(30) & 0xff
#    if k == 27:
#        break

    #i += 1
#cap.release()
#cv2.destroyAllWindows()

#errorCount = 0
np.random.seed(133)
numLabels = 101
image_size_x = 240
image_size_y = 320
dataRoot = "./UCF-101/"

def processFolder(folder):
  #tick = 0
  #global errorCount
  print(dataRoot + folder)
  try:
    videoFileNames = os.listdir(dataRoot + folder)
  except:
    print("Not a directory, moving along.")
    return None, None
  #i = 0
  #data = np.zeros(shape=(len(videoFileNames)*1, image_size_x, image_size_y), dtype=np.float32)
  #labels = np.zeros(shape=(len(videoFileNames)*1, 101), dtype=np.float32)
  for videoName in videoFileNames:
    #if tick < 2:
    #  tick = tick + 1
    #  continue
    #tick = 0

    if random.random() < 0.98:
      continue

    try:
      print(videoName)
      cap = cv2.VideoCapture(dataRoot + folder + "/" + videoName)
#ret, frame1 = cap.read()
#prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
#hsv = np.zeros_like(frame1)
#hsv[...,1] = 255

      fgbg = cv2.createBackgroundSubtractorMOG2(50, 16, False)

      fgbg.setBackgroundRatio(0.8) # frames before object becomes foreground
      fgbg.setVarInit(500) # speed of adaption of new components

      i = 0
      frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
      while(cap.get(cv2.CAP_PROP_POS_FRAMES) < frames - 3):
        #ret, frame2 = cap.read()
        ret, frame2 = cap.read()
        if ret == False:
          continue
        show_imageOrig(frame2)
        frame2 = cv2.GaussianBlur(frame2,(9,9),0)

        fgmask = fgbg.apply(frame2)
        fgmask = fgbg.apply(frame2,fgmask, 0)
        show_image(fgmask)

        k = cv2.waitKey(30) & 0xff
        if k == 27:
          break
    except IOError as e:
      print('Could not read:', image_file, ':', e, '- it\'s ok, skipping.')
  #return data, labels

def iterData(folder):
  labelNames = os.listdir(folder)
  for i in range(len(labelNames)):#len(labelNames)
    processFolder(labelNames[i])

iterData(dataRoot)


import csv
import getopt, sys
from moviepy.editor import VideoFileClip, concatenate_videoclips

folder = '/Videos/'
# file name of the video and config file
event = '20221002 PREECNLBVA'

output_file = None # Create a file for each segment
#output_file = 'check' # Compile the clips with a check flag
output_file = 'highlight' # Compile the clips with a highligh flag
#output_file = '20221002 EYF Segments.mp4' # compile all segments in the config file

# --input
mp4_file = folder + '/' + event + '.mp4'
    # --config
config_file = folder + '/' + event + '.csv'
# --output

def return_filename(desc, prefix, suffix):
    return str(prefix or '') + str(desc or '') + str(suffix or '') + '.mp4'

def main():
    global folder
    global event
    global output_file
    global mp4_file
    global config_file

    argumentList = sys.argv[1:]

    options = "i:c:o:"

    long_options = ["input=","config=","output="]

    try:
        arguments, values = getopt.getopt(argumentList, options, long_options)
        for currentArgument, currentValue in arguments:
            if currentArgument in ("-i", "--input"):
                mp4_file = currentValue
                # print ("File: ", currentValue)
            if currentArgument in ("-o", "--output"):
                output_file = currentValue
            if currentArgument in ("-c", "--config"):
                config_file = currentValue
                # print ("Config: ", currentValue)
    except getopt.error as err:
        print (str(err))

    if mp4_file is None:
        # If mp4 file is not provided, use config file name
        mp4_file = config_file.replace(".csv", ".mp4")

    # Read the config file
    rows = csv.DictReader(open(config_file))

    first = True
    for row in rows:
        if row['source'] == 'video':
            min = int(row['min'])
            sec = int(row['sec'])

            if min > 0:
                start_seconds = min * 60 + sec
            else:
                start_seconds = sec
            length_in_sec = int(row['length_in_sec'])
            end_seconds = start_seconds + length_in_sec
            if start_seconds and end_seconds:
                if output_file is None:
                    # MODE = Split the segments into separate files
                    clip = VideoFileClip(mp4_file).subclip(start_seconds, end_seconds)
                    file_name = return_filename(row['desc'], row['filename_prefix'], row['filename_suffix'])
                    clip.write_videofile(file_name)
                else:
                    # MODE = Concatenate the segments into a single file
                    if (output_file == 'check' and row['filename_suffix'] == 'check') or \
                        (output_file == 'highlight' and row['filename_suffix'] == 'highlight') or \
                            (output_file != 'check' and output_file != 'highlight'):
                        # Save only if check or highlight or if all clips
                        if first:
                            final_clip = VideoFileClip(mp4_file).subclip(start_seconds, end_seconds)
                            first = False
                        else:
                            clip = VideoFileClip(mp4_file).subclip(start_seconds, end_seconds)
                            final_clip = concatenate_videoclips([final_clip,clip])
            else:
                print(f'Error with config settings for: {row}')

    if output_file:
        # Save the final clip
        if output_file == 'check':
            output_file = event + ' check.mp4'
        elif output_file == 'highlight':
            output_file = event + ' highlight.mp4'
        final_clip.write_videofile(output_file)

if __name__ == "__main__":
    main()


import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from scipy.optimize import minimize
from sklearn.metrics import mean_squared_error as mse

def SIR():

    def prediction(beta, gamma, population, i0, r0, d0, time_predict):
        def SIR_model(y, t, beta, gamma, population):
            s, i, r = y
            dSdt = -beta * s * i / population
            dIdt = beta * s * i / population - gamma * i
            dRdt = gamma * i
            return [dSdt, dIdt, dRdt]

        s0 = population - i0 - r0 - d0
        y_0 = [s0, i0, r0]
        sol = odeint(SIR_model, y_0, time_predict, args=(beta, gamma, population))
        sol = np.transpose(sol)
        return sol

    def error_model(point, cases, population, infected_0, recovered_0, dead_0):
        beta, gamma = point

        def SIR_model(y, t, beta, gamma, population):
            s, i, r = y
            dSdt = -beta * s * i / population
            dIdt = beta * s * i / population - gamma * i
            dRdt = gamma * i
            return [dSdt, dIdt, dRdt]

        suscepted_0 = population - infected_0 - recovered_0 - dead_0
        y0 = [suscepted_0, infected_0, recovered_0]
        sol = odeint(SIR_model, y0, np.arange(1, len(cases) + 1), args=(beta, gamma, population))
        sol = np.transpose(sol)
        error = mse(cases, sol[1])
        return error

    def trainer(cases, population, infected_0, recovered_0, dead_0):
        optimal = minimize(error_model, np.array([0.001, 0.001]), args=(cases, population, infected_0, recovered_0, dead_0),
                           method='L-BFGS-B', bounds=[(0.000001, 1.0), (0.000001, 1.0)])
        beta, gamma = optimal.x
        return beta, gamma

    def plot(s, i, r, initials_state, city_name, period_predict, time_predict, population):
        plt.figure()
        plt.title('Projeção do total de habitantes sucetíveis, infectados e recuperados em ' + city_name + '/' +
                  initials_state[0], fontsize=20)
        plt.xlabel('Meses', fontsize=15)
        plt.xticks(np.linspace(15, period_predict + 15, 7)[:-1], ('Abril', 'Maio', 'Junho', 'Julo', 'Agosto', 'Setembro'))
        plt.ylabel('Número de habitantes', fontsize=15)
        plt.yticks(np.arange(0, population, step=population * 0.03))
        plt.plot(time_predict, s, label='Sucetíveis')
        plt.plot(time_predict, i, label='Infectados')
        plt.plot(time_predict, r, label='Recuperados')
        plt.legend(loc='center left', bbox_to_anchor=(1.002, 0.7), fontsize=14)
        plt.rcParams["figure.figsize"] = (20, 10)
        plt.show()


import pandas as pd
import networkx as nx
import json

hierarchy_df = pd.read_csv('hierarchy_table.csv', index_col=0, dtype=str)

graph_network = nx.from_pandas_edgelist(
    hierarchy_df,
    source='Parent',
    target='Child',
)

json_graph = json.dumps(graph_network, default=nx.node_link_data)

# Using a JSON string
with open('json_graph.json', 'w') as outfile:
    outfile.write(json_graph)


#通过node2vec算法获取的节点向量表示计算相似度
import math
import os
import time
import pandas as pd
import numpy as np

#获取所有节点的向量表示形式
def getNodeVector(fileEMB, raw_dataset_path):
    nodeVecDict = {}
    raw_dataset = np.loadtxt(raw_dataset_path, delimiter=',')
    m, n = raw_dataset.shape
    pro_file = pd.read_csv(fileEMB)
    pro = np.array(pro_file["name"])
    vec = np.array(pro_file["node2vec"])

    for i in range(len(pro)):
        every_nodeVecList = []
        node_vector_list = raw_dataset[i, 0:n - 2]
        for j in range(len(node_vector_list)):
            every_nodeVecList.append(float(node_vector_list[j]))
        nodeVecDict[pro[i]] = every_nodeVecList
    return nodeVecDict

def caculateNodeVecSim(nodeVecDict,path):
    NodeVecSimDict = {}
    filePathogenic = path + "/dataset/uploads/train.txt"  # 已知宿主蛋白
    fileCandidate = path + "/dataset/uploads/candidateNode_degree.txt"  # 候选宿主蛋白
    with open(filePathogenic, 'r') as f1:
        pathogenic = f1.readlines()

    with open(fileCandidate, 'r') as f2:
        candidate = f2.readlines()

    for i in candidate:
        simCalcute = []
        every_candidate = i.split("\t")[0]
        for every_pathogenic in pathogenic:
            every_pathogenic = every_pathogenic.strip("\n")
            sim = math.sqrt(sum([(a - b) ** 2 for (a, b) in zip(nodeVecDict[every_candidate], nodeVecDict[every_pathogenic])]))
            simCalcute.append(sim)
        maxSim = max(simCalcute)
        print(maxSim)
        NodeVecSimDict[every_candidate] = maxSim
    return NodeVecSimDict

def save(NodeVecSimDict):
    fileSim = "../dataset/result/Candidate_NodeVecSim.txt"
    with open(fileSim, "w") as fw:
        for key, value in NodeVecSimDict.items():
            fw.write(key + "\t" + str(value) + "\n")
    fw.close()

if __name__ == '__main__':
    path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
    NodeVecTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print("开始获取节点的向量表示：" + NodeVecTime)
    fileEMB = path + "/dataset/temp/COVID_Node2Vec.csv"
    raw_dataset_path = path + '/dataset/minmax_out/raw_minmax.out'
    nodeVecDict = getNodeVector(fileEMB, raw_dataset_path)

    simNodeVecTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print("基于向量表示开始计算相似度：" + simNodeVecTime)
    NodeVecSimDict = caculateNodeVecSim(nodeVecDict,path)
    save(NodeVecSimDict)