import streamlit as st
import pandas as pd
import punto_corte as pc

st.set_page_config(page_title=u"Resumen de datos",
                   page_icon=":ledger:",
                   layout="centered",
                   initial_sidebar_state="auto",
                   menu_items=None)

st.markdown(u"# Resumen de datos")

try:
    shared = st.session_state["shared"]
except:
    st.error(u"No hay archivos cargados.")
    st.stop()

try:
    puntaje = st.session_state["puntaje"]
except:
    st.error(u"No hay archivo de puntaje cargado.")
else:
    st.markdown(u"### Resumen de los puntajes")
    st.table(puntaje.describe())
    st.markdown(u"### Distribución acumulada de los puntajes")
    st.plotly_chart(pc.crear_cumdist(puntaje, "puntaje"))

try:
    items = st.session_state["items"]
except:
    st.error(u"No hay archivo de items cargado.")
else:
    st.markdown(u"### Resumen de los items")
    st.table(items.describe())
    st.markdown(u"### Distribución de dificultad")
    st.plotly_chart(pc.crear_cumdist(items, "items"))


# -*- coding: utf-8 -*-
"""Utility functions used for the Flask Processing Controller Interface"""
from http import HTTPStatus
from functools import wraps

import jsonschema
from flask import request

from config_db.sbi_client import SchedulingBlockDbClient

DB = SchedulingBlockDbClient()

def get_root_url():
    """Return the root URL for this resource."""
    # path = request.path
    # path = path[1:].split('/')
    # path = '/'.join(path[:2])
    # return request.url_root + path
    return request.url_root

def add_scheduling_block(config):
    """Adds a scheduling block to the database, returning a response object"""
    try:
        DB.add_sbi(config)
    except jsonschema.ValidationError as error:
        error_dict = error.__dict__
        for key in error_dict:
            error_dict[key] = error_dict[key].__str__()
        error_response = dict(message="Failed to add scheduling block",
                              reason="JSON validation error",
                              details=error_dict)
        return error_response, HTTPStatus.BAD_REQUEST
    response = dict(config=config,
                    message='Successfully registered scheduling block '
                            'instance with ID: {}'.format(config['id']))
    response['links'] = {
        'self': '{}scheduling-block/{}'.format(request.url_root,
                                               config['id']),
        'list': '{}'.format(request.url),
        'home': '{}'.format(request.url_root)
    }
    return response, HTTPStatus.ACCEPTED

def missing_db_response(func):
    """Decorator to check connection exceptions"""
    @wraps(func)
    def with_exception_handling(*args, **kwargs):
        """Wrapper to check for connection failures"""
        try:
            return func(*args, **kwargs)
        except ConnectionError as error:
            return (dict(error='Unable to connect to Configuration Db.',
                         error_message=str(error),
                         links=dict(root='{}'.format(get_root_url()))),
                    HTTPStatus.NOT_FOUND)
    return with_exception_handling


import csv
import numpy as np
import matplotlib.pyplot as plt
import scipy.fft
import scipy.signal as signal

def file_to_signal_array(file_path):
    with open(file_path, mode='r') as file:
        csv_reader = csv.reader(file)
        next(csv_reader)
        signal = []
        for row in csv_reader:
            signal.append(float(row[1]))
    return np.array(signal)

csv_file = r'data/project1-1e-data.csv'
signal = file_to_signal_array(csv_file)

# Number of samples
N_samples = len(signal)

# Define the Dirichlet kernel in the frequency domain
n = 92 # the n in the Dirichlet kernel

# Define the h kernel
h_kernel = np.zeros(N_samples)
h_kernel[0] = -1
h_kernel[1] = 2
h_kernel[2] = -1

# Compute the Fourier transforms of the signal, Dirichlet kernel, and h kernel
signal_fft = scipy.fft.fft(signal)
dirichlet_kernel_fft = np.zeros(N_samples)
dirichlet_kernel_fft[int(N_samples/2-n-1):int(N_samples/2+n)] = 1
h_kernel_fft = scipy.fft.fft(h_kernel, N_samples)
dirichlet_kernel_fft = scipy.fft.fftshift(dirichlet_kernel_fft)

# Perform convolution in the frequency domain
convolution_dirichlet_fft = signal_fft * dirichlet_kernel_fft
convolution_h_fft = signal_fft * h_kernel_fft

# Compute the inverse Fourier transforms to get the convolution results
convolution_dirichlet = np.real(scipy.fft.ifft(convolution_dirichlet_fft))
convolution_h = np.real(scipy.fft.ifft(convolution_h_fft))

# Plotting
x_vals = np.linspace(0, 1, N_samples)

plt.figure(figsize=(12, 6))
plt.subplot(2, 2, 1)
plt.plot(x_vals, signal, color="blue", label="signal")
plt.title("Original signal")
plt.legend()

plt.subplot(2, 2, 2)
plt.plot(x_vals, signal, color="blue", label="signal")
plt.title("Original signal")
plt.legend()

plt.subplot(2, 2, 3)
plt.plot(x_vals, convolution_dirichlet, color="red", label="convolution")
plt.title("Convolution with Dirichlet kernel")
plt.legend()

plt.subplot(2, 2, 4)
plt.plot(x_vals, convolution_h, color="red", label="convolution")
plt.title("Convolution with h kernel")
plt.legend()

plt.tight_layout()
plt.show()


#!/usr/bin/env python

##################################################
## DEPENDENCIES
import sys
import os
import os.path
try:
    import builtins as builtin
except ImportError:
    import __builtin__ as builtin
from os.path import getmtime, exists
import time
import types
from Cheetah.Version import MinCompatibleVersion as RequiredCheetahVersion
from Cheetah.Version import MinCompatibleVersionTuple as RequiredCheetahVersionTuple
from Cheetah.Template import Template
from Cheetah.DummyTransaction import *
from Cheetah.NameMapper import NotFound, valueForName, valueFromSearchList, valueFromFrameOrSearchList
from Cheetah.CacheRegion import CacheRegion
import Cheetah.Filters as Filters
import Cheetah.ErrorCatchers as ErrorCatchers

##################################################
## MODULE CONSTANTS
VFFSL=valueFromFrameOrSearchList
VFSL=valueFromSearchList
VFN=valueForName
currentTime=time.time
__CHEETAH_version__ = '2.4.4'
__CHEETAH_versionTuple__ = (2, 4, 4, 'development', 0)
__CHEETAH_genTime__ = 1458422848.814569
__CHEETAH_genTimestamp__ = 'Sat Mar 19 16:27:28 2016'
__CHEETAH_src__ = '/home/jeb/Repositories/rosmod/gui/templates/component_hpp.tmpl'
__CHEETAH_srcLastModified__ = 'Tue Mar  8 10:57:08 2016'
__CHEETAH_docstring__ = 'Autogenerated by Cheetah: The Python-Powered Template Engine'

if __CHEETAH_versionTuple__ < RequiredCheetahVersionTuple:
    raise AssertionError(
      'This template was compiled with Cheetah version'
      ' %s. Templates compiled before version %s must be recompiled.'%(
         __CHEETAH_version__, RequiredCheetahVersion))

##################################################
## CLASSES

class component_hpp(Template):

    ##################################################
    ## CHEETAH GENERATED METHODS

    def __init__(self, *args, **KWs):

        super(component_hpp, self).__init__(*args, **KWs)
        if not self._CHEETAH__instanceInitialized:
            cheetahKWArgs = {}
            allowedKWs = 'searchList namespaces filter filtersLib errorCatcher'.split()
            for k,v in KWs.items():
                if k in allowedKWs: cheetahKWArgs[k] = v
            self._initCheetahInstance(**cheetahKWArgs)

    def respond(self, trans=None):

        ## CHEETAH: main method generated for this template
        if (not trans and not self._CHEETAH__isBuffering and not callable(self.transaction)):
            trans = self.transaction # is None unless self.awake() was called
        if not trans:
            trans = DummyTransaction()
            _dummyTrans = True
        else: _dummyTrans = False
        write = trans.response().write
        SL = self._CHEETAH__searchList
        _filter = self._CHEETAH__currentFilter

        ########################################
        ## START - generated method body

        write(u'''#ifndef ''')
        _v = VFFSL(SL,"define_guard",True) # u'${define_guard}' on line 1, col 9
        if _v is not None: write(_filter(_v, rawExpr=u'${define_guard}')) # from line 1, col 9.
        write(u'''_HPP
#define ''')
        _v = VFFSL(SL,"define_guard",True) # u'${define_guard}' on line 2, col 9
        if _v is not None: write(_filter(_v, rawExpr=u'${define_guard}')) # from line 2, col 9.
        write(u'''_HPP
''')
        _v = VFFSL(SL,"hash_include",True) # u'$hash_include' on line 3, col 1
        if _v is not None: write(_filter(_v, rawExpr=u'$hash_include')) # from line 3, col 1.
        write(u''' "node/Component.hpp"
''')
        for topic in VFFSL(SL,"topics",True): # generated from line 4, col 1
            _v = VFFSL(SL,"hash_include",True) # u'$hash_include' on line 5, col 1
            if _v is not None: write(_filter(_v, rawExpr=u'$hash_include')) # from line 5, col 1.
            write(u''' "''')
            _v = VFFSL(SL,"topic",True)[0] # u'$topic[0]' on line 5, col 16
            if _v is not None: write(_filter(_v, rawExpr=u'$topic[0]')) # from line 5, col 16.
            write(u'''/''')
            _v = VFFSL(SL,"topic",True)[1] # u'${topic[1]}' on line 5, col 26
            if _v is not None: write(_filter(_v, rawExpr=u'${topic[1]}')) # from line 5, col 26.
            write(u'''.h"
''')
        for service in VFFSL(SL,"services",True): # generated from line 7, col 1
            _v = VFFSL(SL,"hash_include",True) # u'$hash_include' on line 8, col 1
            if _v is not None: write(_filter(_v, rawExpr=u'$hash_include')) # from line 8, col 1.
            write(u''' "''')
            _v = VFFSL(SL,"service",True)[0] # u'$service[0]' on line 8, col 16
            if _v is not None: write(_filter(_v, rawExpr=u'$service[0]')) # from line 8, col 16.
            write(u'''/''')
            _v = VFFSL(SL,"service",True)[1] # u'${service[1]}' on line 8, col 28
            if _v is not None: write(_filter(_v, rawExpr=u'${service[1]}')) # from line 8, col 28.
            write(u'''.h"
''')
        write(u'''
#ifdef USE_ROSMOD
  #include "rosmod/rosmod_ros.h"
#else
  #ifdef USE_ROSCPP
    #include "ros/ros.h"
  #endif
#endif

''')
        if VFFSL(SL,"component_type",True) == 'KSP': # generated from line 19, col 1
            _v = VFFSL(SL,"hash_include",True) # u'$hash_include' on line 20, col 1
            if _v is not None: write(_filter(_v, rawExpr=u'$hash_include')) # from line 20, col 1.
            write(u''' "krpci/krpci.hpp"
''')
        if VFFSL(SL,"component_type",True) == 'SUMO': # generated from line 22, col 1
            _v = VFFSL(SL,"hash_include",True) # u'$hash_include' on line 23, col 1
            if _v is not None: write(_filter(_v, rawExpr=u'$hash_include')) # from line 23, col 1.
            write(u''' "sumo/sumo_client.hpp"
''')
        write(u'''
''')
        if VFFSL(SL,"trafficGen",True): # generated from line 26, col 1
            if VFFSL(SL,"len",False)(VFFSL(SL,"publishers",True)) > 0: # generated from line 27, col 1
                _v = VFFSL(SL,"hash_include",True) # u'$hash_include' on line 28, col 1
                if _v is not None: write(_filter(_v, rawExpr=u'$hash_include')) # from line 28, col 1.
                write(u''' "network/sender.hpp"
''')
            if VFFSL(SL,"len",False)(VFFSL(SL,"subscribers",True)) > 0: # generated from line 30, col 1
                _v = VFFSL(SL,"hash_include",True) # u'$hash_include' on line 31, col 1
                if _v is not None: write(_filter(_v, rawExpr=u'$hash_include')) # from line 31, col 1.
                write(u''' "network/receiver.hpp"
''')
        write(u'''
''')
        if VFFSL(SL,"user_includes",True) == "": # generated from line 35, col 1
            write(u'''//# Start User Includes Marker
//# End User Includes Marker
''')
        else: # generated from line 38, col 1
            write(u'''//# Start User Includes Marker
''')
            _v = VFFSL(SL,"user_includes",True) # u'$user_includes' on line 40, col 1
            if _v is not None: write(_filter(_v, rawExpr=u'$user_includes')) # from line 40, col 1.
            write(u'''//# End User Includes Marker
''')
        write(u'''
''')
        if VFFSL(SL,"hpp_globals",True) == "": # generated from line 43, col 1
            write(u'''//# Start User Globals Marker
//# End User Globals Marker
''')
        else: # generated from line 46, col 1
            write(u'''//# Start User Globals Marker
''')
            _v = VFFSL(SL,"hpp_globals",True) # u'$hpp_globals' on line 48, col 1
            if _v is not None: write(_filter(_v, rawExpr=u'$hpp_globals')) # from line 48, col 1.
            write(u'''//# End User Globals Marker
''')
        write(u'''
class ''')
        _v = VFFSL(SL,"component_name",True) # u'$component_name' on line 51, col 7
        if _v is not None: write(_filter(_v, rawExpr=u'$component_name')) # from line 51, col 7.
        write(u''' : public Component
{
public:
  // Constructor
  ''')
        _v = VFFSL(SL,"component_name",True) # u'${component_name}' on line 55, col 3
        if _v is not None: write(_filter(_v, rawExpr=u'${component_name}')) # from line 55, col 3.
        write(u'''(ComponentConfig& _config, int argc, char **argv)
  : Component(_config, argc, argv) {}

  // Initialization
  void init_timer_operation(const NAMESPACE::TimerEvent& event);

''')
        if VFFSL(SL,"len",False)(VFFSL(SL,"subscribers",True)) > 0: # generated from line 61, col 1
            for sub in VFFSL(SL,"subscribers",True): # generated from line 62, col 1
                write(u'''  // Subscriber Operation - ''')
                _v = VFN(VFFSL(SL,"sub",True),"properties",True)["name"] # u'$sub.properties["name"]' on line 63, col 29
                if _v is not None: write(_filter(_v, rawExpr=u'$sub.properties["name"]')) # from line 63, col 29.
                write(u'''
  void ''')
                _v = VFN(VFFSL(SL,"sub",True),"properties",True)["name"] # u'${sub.properties["name"]}' on line 64, col 8
                if _v is not None: write(_filter(_v, rawExpr=u'${sub.properties["name"]}')) # from line 64, col 8.
                write(u'''_operation(const ''')
                _v = VFN(VFN(VFN(VFFSL(SL,"sub",True),"properties",True)["message_reference"],"parent",True),"properties",True)["name"] # u'$sub.properties["message_reference"].parent.properties["name"]' on line 64, col 50
                if _v is not None: write(_filter(_v, rawExpr=u'$sub.properties["message_reference"].parent.properties["name"]')) # from line 64, col 50.
                write(u'''::''')
                _v = VFN(VFN(VFFSL(SL,"sub",True),"properties",True)["message_reference"],"properties",True)["name"] # u'${sub.properties["message_reference"].properties["name"]}' on line 64, col 114
                if _v is not None: write(_filter(_v, rawExpr=u'${sub.properties["message_reference"].properties["name"]}')) # from line 64, col 114.
                write(u'''::ConstPtr& received_data);

''')
        if VFFSL(SL,"len",False)(VFFSL(SL,"provided_services",True)) > 0: # generated from line 68, col 1
            for server in VFFSL(SL,"servers",True): # generated from line 69, col 1
                write(u'''  // Server Operation - ''')
                _v = VFN(VFFSL(SL,"server",True),"properties",True)["name"] # u'$server.properties["name"]' on line 70, col 25
                if _v is not None: write(_filter(_v, rawExpr=u'$server.properties["name"]')) # from line 70, col 25.
                write(u'''
  bool ''')
                _v = VFN(VFN(VFFSL(SL,"server",True),"properties",True)["service_reference"],"properties",True)["name"] # u'${server.properties["service_reference"].properties["name"]}' on line 71, col 8
                if _v is not None: write(_filter(_v, rawExpr=u'${server.properties["service_reference"].properties["name"]}')) # from line 71, col 8.
                write(u'''_operation(''')
                _v = VFN(VFN(VFN(VFFSL(SL,"server",True),"properties",True)["service_reference"],"parent",True),"properties",True)["name"] # u'$server.properties["service_reference"].parent.properties["name"]' on line 71, col 79
                if _v is not None: write(_filter(_v, rawExpr=u'$server.properties["service_reference"].parent.properties["name"]')) # from line 71, col 79.
                write(u'''::''')
                _v = VFN(VFN(VFFSL(SL,"server",True),"properties",True)["service_reference"],"properties",True)["name"] # u'$server.properties["service_reference"].properties["name"]' on line 71, col 146
                if _v is not None: write(_filter(_v, rawExpr=u'$server.properties["service_reference"].properties["name"]')) # from line 71, col 146.
                write(u'''::Request &req,
    ''')
                _v = VFN(VFN(VFN(VFFSL(SL,"server",True),"properties",True)["service_reference"],"parent",True),"properties",True)["name"] # u'$server.properties["service_reference"].parent.properties["name"]' on line 72, col 5
                if _v is not None: write(_filter(_v, rawExpr=u'$server.properties["service_reference"].parent.properties["name"]')) # from line 72, col 5.
                write(u'''::''')
                _v = VFN(VFN(VFFSL(SL,"server",True),"properties",True)["service_reference"],"properties",True)["name"] # u'$server.properties["service_reference"].properties["name"]' on line 72, col 72
                if _v is not None: write(_filter(_v, rawExpr=u'$server.properties["service_reference"].properties["name"]')) # from line 72, col 72.
                write(u'''::Response &res);

''')
        if VFFSL(SL,"len",False)(VFFSL(SL,"timers",True)) > 0: # generated from line 76, col 1
            for timer in VFFSL(SL,"timers",True): # generated from line 77, col 1
                write(u'''  // Timer Operation - ''')
                _v = VFN(VFFSL(SL,"timer",True),"properties",True)["name"] # u'$timer.properties["name"]' on line 78, col 24
                if _v is not None: write(_filter(_v, rawExpr=u'$timer.properties["name"]')) # from line 78, col 24.
                write(u'''
  void ''')
                _v = VFN(VFFSL(SL,"timer",True),"properties",True)["name"] # u'${timer.properties["name"]}' on line 79, col 8
                if _v is not None: write(_filter(_v, rawExpr=u'${timer.properties["name"]}')) # from line 79, col 8.
                write(u'''_operation(const NAMESPACE::TimerEvent& event);

''')
        write(u'''  // Start up
  void startUp();

  // Destructor
  ~''')
        _v = VFFSL(SL,"component_name",True) # u'${component_name}' on line 87, col 4
        if _v is not None: write(_filter(_v, rawExpr=u'${component_name}')) # from line 87, col 4.
        write(u'''();

private:

''')
        if VFFSL(SL,"len",False)(VFFSL(SL,"timers",True)) > 0: # generated from line 91, col 1
            for timer in VFFSL(SL,"timers",True): # generated from line 92, col 1
                write(u'''  // Timer
  NAMESPACE::Timer ''')
                _v = VFN(VFFSL(SL,"timer",True),"properties",True)["name"] # u'$timer.properties["name"]' on line 94, col 20
                if _v is not None: write(_filter(_v, rawExpr=u'$timer.properties["name"]')) # from line 94, col 20.
                write(u''';

''')
        if VFFSL(SL,"len",False)(VFFSL(SL,"subscribers",True)) > 0: # generated from line 98, col 1
            if VFFSL(SL,"trafficGen",True): # generated from line 99, col 1
                write(u'''  // function for when servers are done receiving data
  void mw_recv_done_operation(Network::receiver* receiver_mw);
''')
            for sub in VFFSL(SL,"subscribers",True): # generated from line 103, col 1
                write(u'''  // Subscriber
  NAMESPACE::Subscriber ''')
                _v = VFN(VFFSL(SL,"sub",True),"properties",True)["name"] # u'$sub.properties["name"]' on line 105, col 25
                if _v is not None: write(_filter(_v, rawExpr=u'$sub.properties["name"]')) # from line 105, col 25.
                write(u''';
''')
                if VFFSL(SL,"trafficGen",True): # generated from line 106, col 1
                    write(u'''  // message id for this data stream
  uint64_t ''')
                    _v = VFN(VFFSL(SL,"sub",True),"properties",True)['name'] # u"${sub.properties['name']}" on line 108, col 12
                    if _v is not None: write(_filter(_v, rawExpr=u"${sub.properties['name']}")) # from line 108, col 12.
                    write(u'''_id;
  // subscriber receiver middleware
  Network::receiver ''')
                    _v = VFN(VFFSL(SL,"sub",True),"properties",True)['name'] # u"${sub.properties['name']}" on line 110, col 21
                    if _v is not None: write(_filter(_v, rawExpr=u"${sub.properties['name']}")) # from line 110, col 21.
                    write(u'''_recv_mw;
''')
                write(u'''
''')
        if VFFSL(SL,"len",False)(VFFSL(SL,"publishers",True)) > 0: # generated from line 115, col 1
            if VFFSL(SL,"trafficGen",True): # generated from line 116, col 1
                write(u'''  // do we abide by the profiles?
  bool tg_misbehave;
  // size of messages generated
  uint64_t max_data_length;
''')
            for pub in VFFSL(SL,"publishers",True): # generated from line 122, col 1
                write(u'''  // Publisher
  NAMESPACE::Publisher ''')
                _v = VFN(VFFSL(SL,"pub",True),"properties",True)["name"] # u'$pub.properties["name"]' on line 124, col 24
                if _v is not None: write(_filter(_v, rawExpr=u'$pub.properties["name"]')) # from line 124, col 24.
                write(u''';
''')
                if VFFSL(SL,"trafficGen",True): # generated from line 125, col 1
                    write(u'''  // Timer for generating traffic
  NAMESPACE::Timer ''')
                    _v = VFN(VFFSL(SL,"pub",True),"properties",True)['name'] # u"${pub.properties['name']}" on line 127, col 20
                    if _v is not None: write(_filter(_v, rawExpr=u"${pub.properties['name']}")) # from line 127, col 20.
                    write(u'''_timer;
  // Timer operation for traffic generation
  void ''')
                    _v = VFN(VFFSL(SL,"pub",True),"properties",True)['name'] # u"${pub.properties['name']}" on line 129, col 8
                    if _v is not None: write(_filter(_v, rawExpr=u"${pub.properties['name']}")) # from line 129, col 8.
                    write(u'''_timer_operation(const NAMESPACE::TimerEvent& event);
  // publisher sender middleware
  Network::sender ''')
                    _v = VFN(VFFSL(SL,"pub",True),"properties",True)['name'] # u"${pub.properties['name']}" on line 131, col 19
                    if _v is not None: write(_filter(_v, rawExpr=u"${pub.properties['name']}")) # from line 131, col 19.
                    write(u'''_send_mw;
''')
                write(u'''
''')
        if VFFSL(SL,"len",False)(VFFSL(SL,"provided_services",True)) > 0: # generated from line 136, col 1
            for server in VFFSL(SL,"servers",True): # generated from line 137, col 1
                write(u'''  // Server
  NAMESPACE::ServiceServer ''')
                _v = VFN(VFFSL(SL,"server",True),"properties",True)["name"] # u'${server.properties["name"]}' on line 139, col 28
                if _v is not None: write(_filter(_v, rawExpr=u'${server.properties["name"]}')) # from line 139, col 28.
                write(u''';

''')
        if VFFSL(SL,"len",False)(VFFSL(SL,"required_services",True)) > 0: # generated from line 143, col 1
            for client in VFFSL(SL,"clients",True): # generated from line 144, col 1
                write(u'''  // Client
  NAMESPACE::ServiceClient ''')
                _v = VFN(VFFSL(SL,"client",True),"properties",True)["name"] # u'$client.properties["name"]' on line 146, col 28
                if _v is not None: write(_filter(_v, rawExpr=u'$client.properties["name"]')) # from line 146, col 28.
                write(u''';

''')
        if VFFSL(SL,"user_private_variables",True) == "": # generated from line 150, col 1
            write(u'''  //# Start User Private Variables Marker
  //# End User Private Variables Marker
''')
        else: # generated from line 153, col 1
            write(u'''  //# Start User Private Variables Marker
''')
            _v = VFFSL(SL,"user_private_variables",True) # u'$user_private_variables' on line 155, col 1
            if _v is not None: write(_filter(_v, rawExpr=u'$user_private_variables')) # from line 155, col 1.
            write(u'''  //# End User Private Variables Marker
''')
        write(u'''};

#endif
''')

        ########################################
        ## END - generated method body

        return _dummyTrans and trans.response().getvalue() or ""

    ##################################################
    ## CHEETAH GENERATED ATTRIBUTES

    _CHEETAH__instanceInitialized = False

    _CHEETAH_version = __CHEETAH_version__

    _CHEETAH_versionTuple = __CHEETAH_versionTuple__

    _CHEETAH_genTime = __CHEETAH_genTime__

    _CHEETAH_genTimestamp = __CHEETAH_genTimestamp__

    _CHEETAH_src = __CHEETAH_src__

    _CHEETAH_srcLastModified = __CHEETAH_srcLastModified__

    _mainCheetahMethod_for_component_hpp= 'respond'

## END CLASS DEFINITION

if not hasattr(component_hpp, '_initCheetahAttributes'):
    templateAPIClass = getattr(component_hpp, '_CHEETAH_templateClass', Template)
    templateAPIClass._addCheetahPlumbingCodeToClass(component_hpp)

# CHEETAH was developed by Tavis Rudd and Mike Orr
# with code, advice and input from many other volunteers.
# For more information visit http://www.CheetahTemplate.org/

##################################################
## if run from command line:
if __name__ == '__main__':
    from Cheetah.TemplateCmdLineIface import CmdLineIface
    CmdLineIface(templateObj=component_hpp()).run()


###############
# moonraker #
###############

def main():
    import boto3
    import argparse
    import sys
    import datetime
    from os import environ
    from pkg_resources import resource_filename
    from json import dumps
    from pathlib import Path

    # from moonraker.amiget import get_amimap
    from moonraker.iam_role_get import get_iam_role
    from moonraker.keypairget import get_key_pairs
    from moonraker.os_get import get_os
    # from moonraker.populate import populate
    from moonraker.profileget import get_profile
    from moonraker.regionget import get_region
    # from size_get import get_sizes
    from moonraker.subnetget import get_subnets
    from moonraker.sg_get import get_sgs
    from moonraker.userdata import add_user_data
    from moonraker.vpcget import get_vpc
    from moonraker.vpc_sanitize import sanitize_vpc

    # ADD ARGUMENTS
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', '--vpc', help="VPC (VPC-id or friendly name)")
    parser.add_argument('-o','--os', help="operating system")
    parser.add_argument('-t','--type', help="instance type (size)")
    parser.add_argument('-z','--zone', help="availability zone")
    parser.add_argument('--hostname', help='name of instance')
    parser.add_argument('-k', '--key', help='name of keypair')
    parser.add_argument('--role', help='instance iam role')
    parser.add_argument('--region', help='region of build')
    parser.add_argument('--profile', help="AWS CLI Profile")
    parser.add_argument('--network', help="Public or Private subnet")
    parser.add_argument('--timezone', help='Timezone of instance')
    parser.add_argument('--user', help='default user on instance')
    parser.add_argument('--populate', help='enter location of file')
    parser.add_argument('--root', help='Use to override default root volume size of 64 GB')
    parser.add_argument('-d', '--disks', nargs='+', help='add data volumes', default=None)
    parser.add_argument('--sgs', nargs='+', help='add security groups', default=None)
    args = parser.parse_args()

    value_dict = {}
    skipped_opts = {}

    # if args.populate:
    #     pop_dict = populate(args.populate)
    # else:
    pop_dict = {}

    vars_list = ""

    # add generator later
    # instance_count = 1

    #Note: if updating allowed_os, also update linux_os (below) and user_dict in userdata.py
    allowed_os = [
        'ubuntu16', 'ubuntu18', 'amazonlinux2', 'rhel7', 'centos7', 'windows2016'
        ]
    linux_os = [
        'ubuntu16', 'ubuntu18', 'amazonlinux2', 'rhel7', 'centos7'
        ]
    allowed_regions = ['us-east-1', 'us-east-2', 'us-west-2', 'eu-central-1']

    # Generate filename values
    build_no = '{:%H%M%S}'.format(datetime.datetime.now())
    # Create tfvars file
    tfvars_file = Path("variables.tfvars")
    if not tfvars_file.is_file():
        Path(tfvars_file).touch()
    provider_file = Path("main.tf")
    if not provider_file.is_file():
        with open(resource_filename('moonraker', 'main.tf')) as f:
            filedata = f.read()
        with open(provider_file, 'w') as file:
            file.write(filedata)
    # Create Main File
    main_file = f"ec2-{build_no}.tf"
    main_source_file = resource_filename('moonraker', 'ec2.tf')
    with open(main_source_file, 'r') as file :
        filedata = file.read()
    with open(main_file, 'w') as file:
        file.write(filedata)

    # Create generic variables file
    var_file = Path("variables.tf")
    if not var_file.is_file():
        with open(var_file, 'w') as file:
            file.write('\nvariable "profile" {}')
            file.write('\nvariable "region" {}')

    # Create EC2 variables file
    ec2_var_file = Path("ec2_variables.tf")
    if not ec2_var_file.is_file():
        var_source_file = resource_filename('moonraker', 'ec2_variables.tf')
        with open(var_source_file, 'r') as file :
            filedata = file.read()
        with open(ec2_var_file, 'w') as file:
            file.write(filedata)

    # Create data file
    data_file = Path("data.tf")
    if not data_file.is_file():
        data_source_file = resource_filename('moonraker', 'data.tf')
        with open(data_source_file, 'r') as file :
            filedata = file.read()
        with open(data_file, 'w') as file:
            file.write(filedata)

    if args.profile:
        profile = args.profile
    elif 'AWS_PROFILE' in environ:
        profile = environ['AWS_PROFILE']
    else:
        profile = get_profile()
        print("\n")
    value_dict["profile"] = profile

    # GET VPC and REGION - REQUIRED!
    if args.region:
        region = args.region
        if args.vpc:
            vpc = sanitize_vpc(profile, args.vpc, [region])
        else:
            vpc = get_vpc(profile, region)
    else:
        if args.vpc:
            vpc = sanitize_vpc(profile, args.vpc, allowed_regions)
            region = get_region(profile, args.vpc, allowed_regions)
        else:
            print("VPC or Region is required for all builds.")
            print("Specify a region with the '--region' flag, or VPC with '--vpc'")
            print("Exiting...")
            sys.exit(1)

    value_dict["region"] = region

    # USER GEN - REQUIRED

    # Instance type
    if args.type:
        value_dict["instance_type"] = args.type
    else:
        print("\nTYPE")
        print("====")
        print("Instance type is required.")
        selection = input("\nEnter instance type (size): ")
        value_dict["instance_type"] = selection

    # key pair
    if args.key:
        value_dict[f"key_name_{build_no}"] = args.key
    else:
        key = get_key_pairs(profile, region)
        if key:
            value_dict[f"key_name_{build_no}"] = key
        # else:
        #     print("Keypair is required")
        #     print("Exiting...")
        #     sys.exit(1)
    vars_list += (f'\nvariable "key_name_{build_no}"')
    vars_list += " {}\n"

    # GET AUTOMATIC VALUES

    # value_dict["ami"] = get_amimap(profile, region)

    # USER GEN OR PROMPTED
    if args.sgs:
        sgs_fmt = []
        for group in args.sgs:
            sgs_fmt.append(group)
        value_dict["security_groups"] = dumps(sgs_fmt)
    else:
        value_dict["security_groups"] = dumps(get_sgs(vpc, region, profile))

    # If OS is entered, use it. Else, create as parameter
    if args.os in allowed_os:
        os = args.os
        value_dict["os"] = os
    else:
        os = get_os(allowed_os)
        if os:
            value_dict["os"] = os

        # else:
        #     print("\nProceeding without OS")
        #     os_params = "OS:"
        #     os_params += "\n    Type: String"
        #     os_params += "\n    AllowedValues:"
        #     for os in allowed_os:
        #         if os not in linux_os:
        #             os_params += "\n      - " + os

        #     value_dict["# VAR_PARAM_OS"] = os_params
        #     value_dict["os"] = "!Ref OS"
        #     skipped_opts["os"] = "Enter as PARAMETER in CloudFormation (Windows Only)"

    # USER GEN - REQUIRED

    # Hostname (Required)
    if args.hostname:
        hn = args.hostname
    else:
        print("\nHOSTNAME")
        print("========")
        hn = input("Please enter hostname: ")

    vars_list += (f'\nvariable "hostname_{build_no}"')
    vars_list += " {}\n"
    value_dict[f"hostname_{build_no}"] = hn

    # USER GEN - OPTIONAL

    # Availability Zone
    if args.zone:
        az = args.zone
    else:
        az = "a"
    vars_list += (f'variable "availability_zone_{build_no}"')
    vars_list += " {}\n"
    value_dict[f"availability_zone_{build_no}"] = region + az

    # If network type is entered, use it. Else, create as parameter
    if args.network and args.network.lower() == 'public':
        # value_dict["network"] = "Public"
        value_dict["subnets_public"] = get_subnets(vpc, main_file, profile, region, "Public")
    else:
        # value_dict["network"] = "Private"
        value_dict["subnets_private"] = get_subnets(vpc, main_file, profile, region, "Private")

    # If role is entered, use it. Else, create as parameter
    if args.role:
        value_dict["iam_instance_profile"] = args.role
    else:
        value_dict["iam_instance_profile"] = get_iam_role(profile, region)
    if not value_dict["iam_instance_profile"]:
        role_params = "IamInstanceProfile:"
        role_params += "\n    Type: String"
        role_params += "\n    Default: EC2-S3-Access"

        value_dict["# VAR_PARAM_ROLE"] = role_params
        value_dict["iam_instance_profile"] = "!Ref IamInstanceProfile"
        skipped_opts["role"] = "Enter as PARAMETER in CloudFormation"

    # Write user_data
    if os in linux_os:
        # Get values to populate user_data file
        if args.timezone:
            # value_dict["# timedatectl"] = "timedatectl"
            tz = args.timezone
        elif pop_dict and pop_dict["timezone"]:
            tz = pop_dict["timezone"]
        else:
            tz = "UTC"
            skipped_opts["timezone"] = tz

        # Default system user - maybe only necessary for linux
        if args.user:
            user = args.user
        elif pop_dict and pop_dict['user']:
            user = pop_dict['user']
        else:
            print("\nUSERNAME")
            print("========")
            print("Default username is required for Linux instances:")
            user = input("Please enter user: ")

        add_user_data(os, hn, tz, user, build_no)

    # Format root EBS vol properly
    # if os in ['amazonlinux2']:
    #     value_dict["VAR_ROOT_VOL_NAME"] = "/dev/xvda"
    # else:
    #     value_dict["VAR_ROOT_VOL_NAME"] = "/dev/sda1"

    # Override root volume size if necessary
    if args.root:
        # root_vol_size = str(args.root)
        value_dict["root_vol_size"] = str(args.root)

    # If disks are entered, add them. Else, ignore
    if args.disks:
        disks = args.disks
        block_device_pool = [
            '/dev/xvdb', '/dev/xvdc', '/dev/xvdd', \
            '/dev/xvde', '/dev/xvdf', '/dev/xvdg', '/dev/xvdh' \
            '/dev/xvdi', '/dev/xvdj', '/dev/xvdk', '/dev/xvdl'
            ]
        counter = 0
        disk_params = ""

        for disk in disks:
            disk_params += "\tebs_block_device {\n"
            disk_params += f'\t\tdevice_name = "{block_device_pool[counter]}"\n'
            disk_params += "\t\tencrypted = true\n"
            disk_params += f"\t\tvolume_size = {disk}\n"
            disk_params += "\t}\n"

            counter += 1

        # value_dict["# VAR_PARAM_DISKS"] = disk_params
        # Read in the file
        with open(main_file, 'r') as file :
            filedata = file.read()
        # Replace the target string
            filedata = filedata.replace('#VAR_EBS', disk_params)
        # Write the file out again
        with open(main_file, 'w') as file:
            file.write(filedata)
    else:
        skipped_opts["Addt'l EBS Volumes"] = "None"

    ## CHECK VALUES

    if skipped_opts:
        print("\n#-------------#\n")
        print("You skipped the following optional parameters.")
        print("They are not required, but please confirm you did not omit them by accident")
        print("Default values shown when available\n")
        for key, value in skipped_opts.items():
            print(f"{key}: {value}")
        print("\n#-------------#\n")

    # with open(source_file, 'r') as f:
    #     build = f.read()
    #     for key, value in value_dict.items():
    #         build = build.replace(key, value)

## FINALIZE
    # Finalize instance main file
    with open(main_file, 'r') as file :
        filedata = file.read()
    # Replace the target string
        filedata = filedata.replace('BUILD_NO', build_no)
        filedata = filedata.replace("VAR_OS", os)
    # Write the file out again
    with open(main_file, 'w') as file:
        file.write(filedata)

    # Finalize variables file
    with open(ec2_var_file, 'r') as file :
        filedata = file.read()
        # filedata = filedata.replace("BUILD_NO", build_no)
        filedata += vars_list
    with open(ec2_var_file, 'w') as file:
        file.write(filedata)

    # Finalize TFVARS file
    with open(tfvars_file, 'r+') as f:
        existing = f.read()
        for key, value in value_dict.items():
            if key not in existing:
                if key == "security_groups":
                    f.write(f"{key} = {value}")
                    f.write("\n")
                elif isinstance(value,dict):
                    f.write(f"{key}")
                    f.write(" = {\n")
                    for k, v in value.items():
                        f.write(f'\t{k} = "{v}"\n')
                    f.write("}\n")
                else:
                    f.write(f'{key} = "{value}"\n')

    # print(value_dict)

    # Print final output
    print(f"\nYou created this template with the profile {profile}")
    print("Please make sure this is for the correct profile\n")
    print(f"Your moonraker TF File is now available at {main_file}\n")
    print(f"Your moonraker variables now available at {tfvars_file}\n")


# Authored by Cem Kaya & ...
import threading
import json
from time import strptime
import requests as req
from flask_cors import CORS
from flask import Flask, render_template, request, send_from_directory, send_file
import torch
import torchvision
import cv2
import numpy as np
import base64
from matplotlib import pyplot as plt2

# Program for converting bytes to string using decode()

# import required module
import codecs
import matplotlib.image as mpimg

# py -3.10 .\back_end.py # to run the server
# py -3.10 -m pip freeze > requirements.txt # to create requirements.txt
# py -3.10 -m pip install -r requirements.txt # to install requirements.txt

def results_parser(results):
  s = ""
  if results.pred[0].shape[0]:
    for c in results.pred[0][:, -1].unique():
      n = (results.pred[0][:, -1] == c).sum()  # detections per class
      s += f"{n} {results.names[int(c)]}{'s' * (n > 1)}, "  # add to string
  return s
app = Flask(__name__)
CORS(app)

@app.route('/')
def index():
  return render_template('index.html')

@app.route('/post_test', methods=['POST'], strict_slashes=False  )
def test():
    global picture_index

    file = request.files['image']

    this_img_name= 'pics/tmp'+str(picture_index)+'.jpg'
    file.save(this_img_name)
    picture_index=picture_index+1

    # Reading an image in default mode
    #src = cv2.imread('tmp.jpg')

	# Window name in which image is displayed
    #window_name = 'Image'
    #plt2.switch_backend('agg')

    plt2.ion()
    img = mpimg.imread(this_img_name)

    imgplot = plt2.imshow(img)

    results = yolo_model(img)
    result=results_parser(results)
    result=result.split(" ")
    #results.print()

    is_exists_human=next((i for i, x in enumerate(result) if( x=="person," or x=="persons,")), None)
    number_of_person=0

    #find("person")
    print("resuls ",result)

    if(is_exists_human!=None):
       number_of_person=int(result[is_exists_human-1])

    print(number_of_person)

    results.show()
	# Using cv2.flip() method
	# Use Flip code 0 to flip vertically
   # image = cv2.flip(src, 0)
    #cv2.imshow(window_name, image)
    #cv2.waitKey()
    img.save(this_img_name)
    #resmi byte cevirip at
    #byte_image=bytearray(img)
    x=codecs.img.encode()
    print("x is",type(x))
    return "x" #render_template("success.html", data= req.post(data = json.dumps(myobj)).text )

#################################################
if __name__ == '__main__':  #python interpreter assigns "__main__" to the file you run
    imgs = ['https://ultralytics.com/images/zidane.jpg']  # batch of images
    picture_index=0
    print("sssssssssssssssssssjkddms")
    #img = mpimg.imread('tmp.jpg')
    #plt2.axis([-50,50,0,10000])

    yolo_model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
    results = yolo_model(imgs)
    plt2.pause(0.5)
    # Results
    plt2.draw()

    #results.print()
    #results.show()  # or .show()
    app.run(debug=True , host="0.0.0.0")


"""
767. Reorganize String
Given a string s, rearrange the characters of s so that any two adjacent characters are not the same.
Return any possible rearrangement of s or return "" if not possible.

Example1:
Input: s = "aab"
Output: "aba"

Example2:
Input: s = "aaab"
Output: ""

Constraints:
1 <= s.length <= 500
s consists of lowercase English letters
"""

"""
Note:
1. maxHeap: O(n) time | O(n) space - where n is the length of given string s
(1) store the char count using collections.Counter(s)
(2) using a maxHeap with (-count, char)
(3) for each run, pop two items out and append one by one, heappush back to the maxHeap if new count are still larger than 0
"""
import collections
import heapq
class Solution:
    def reorganizeString(self, s: str) -> str:
        charCount = collections.Counter(s)
        if len(s) % 2 == 1 and max(charCount.values()) > len(s) // 2 + 1:
            return ""
        if len(s) % 2 == 0 and max(charCount.values()) > len(s) // 2:
            return ""

        maxHeap = []
        for char, count in charCount.items():
            heapq.heappush(maxHeap, (-1 * count, char))

        chars = []
        while len(chars) < len(s):
            if len(maxHeap) == 1:
                chars.append(heapq.heappop(maxHeap)[1])
            else:
                firstMaxItem = heapq.heappop(maxHeap)
                secondMaxItem = heapq.heappop(maxHeap)
                chars.append(firstMaxItem[1])
                chars.append(secondMaxItem[1])

                newFirstItemCount = -1 * firstMaxItem[0] - 1
                newSecondItemCount = -1 * secondMaxItem[0] - 1
                if newFirstItemCount > 0:
                    heapq.heappush(maxHeap, (-1 * (newFirstItemCount), firstMaxItem[1]))
                if newSecondItemCount> 0:
                    heapq.heappush(maxHeap, (-1 * (newSecondItemCount), secondMaxItem[1]))
        return "".join(chars)

# Unit Tests
import unittest
funcs = [Solution().reorganizeString]

class TestReorganizeString(unittest.TestCase):
    def testReorganizeString1(self):
        for func in funcs:
            s = "aab"
            self.assertEqual(func(s=s), "aba")

    def testReorganizeString2(self):
        for func in funcs:
            s = "aaab"
            self.assertEqual(func(s=s), "")

    def testReorganizeString3(self):
        for func in funcs:
            s = "vvvlo"
            self.assertTrue(func(s=s) in ("vlvov", "vlvov"))

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


# import sqlite3 database
import sqlite3

# handel the exception in try and except block
try:
    # create a database with name of mymovie
    con = sqlite3.connect("data.db")
    print("Database is Created")  # message show
    con.commit()  # commit all task
except:  # except block
    print("Error is occurred")  # message show


import bpy
import random
import urllib
import urllib.request
import numpy as np

# Constants
mapWidthInDegree = 360.0
mapHeightInDegree = 180.0
mapStartLon = -180.0
mapStartLat = 90.0
pixelTileWidth = 256
pixelTileHeight = pixelTileWidth

maximumZoom = 3
elevationOffset = -8201
elevationScale = 115.4588

tileWidth = mapWidthInDegree/pow(2, maximumZoom+1);
tileHeight = tileWidth;

altitude_url_template = "https://api.nasa.gov/mars-wmts/catalog/Mars_MGS_MOLA_DEM_mosaic_global_463m_8/1.0.0/default/default028mm/{0}/{1}/{2}.png"
texture_url_template = "https://api.nasa.gov/mars-wmts/catalog/Mars_Viking_MDIM21_ClrMosaic_global_232m/1.0.0/default/default028mm/{0}/{1}/{2}.png"

def get_image(lat, lon):
    col = np.floor((lon - mapStartLon)/tileWidth);
    row = np.floor( (mapHeightInDegree - (lat + mapStartLat)) /tileHeight);
    altitude_url = altitude_url_template.format(int(maximumZoom), int(row), int(col));
    texture_url = texture_url_template.format(int(maximumZoom), int(row), int(col));

    print("altitude: {0}".format(altitude_url))
    print("texture: {0}".format(texture_url))

    dirpath = os.path.dirname(bpy.data.filepath)
    altitude_filename = os.path.join(dirpath, "{0}_{1}_altitude.png".format(lat, lon))
    texture_filename = os.path.join(dirpath, "{0}_{1}_texture.png".format(lat, lon))
    urllib.request.urlretrieve(altitude_url, altitude_filename)
    urllib.request.urlretrieve(texture_url, texture_filename)

def render_tile(lat, lon):
    altitude_filename = "{0}_{1}_altitude.png".format(lat, lon)
    texture_filename = "{0}_{1}_texture.png".format(lat, lon)

    dirpath = os.path.dirname(bpy.data.filepath)
    texture_filename = os.path.join(dirpath, texture_filename)

    try:
        img = bpy.data.images.load(texture_filename)
    except:
        raise NameError("Cannot load image %s" % realpath)

    # Create image texture from image
    cTex = bpy.data.textures.new('ColorTex', type = 'IMAGE')
    cTex.image = img

    # Create material
    mat = bpy.data.materials.new('TexMat')

    # Add texture slot for color texture
    mtex = mat.texture_slots.add()
    mtex.texture = cTex
    mtex.texture_coords = 'UV'
    mtex.use_map_color_diffuse = True
    mtex.use_map_color_emission = True
    mtex.emission_color_factor = 0.5
    mtex.use_map_density = True
    mtex.mapping = 'FLAT'

    # Import surface height map as plane
    bpy.ops.import_image.to_plane(files=[{"name":altitude_filename, "name":altitude_filename}], directory="/Users/nsadras/Documents/spaceapps/", filter_image=True, filter_movie=True, filter_glob="", relative=False)

    # Add material to current object
    ob = bpy.context.object
    me = ob.data
    me.materials.clear()
    me.materials.append(mat)

    # Surface displacement
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.subdivide(number_cuts=300)
    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.ops.object.modifier_add(type="DISPLACE")

    #objname = filename.split(".")[0]
    objname = "{0}_{1}_altitude".format(lat, lon)
    bpy.data.objects[objname].modifiers['Displace'].texture = bpy.data.textures[objname]
    #bpy.data.objects['tile'].modifiers['Displace'].texture_coords = 'OBJECT'
    #bpy.data.objects['tile'].modifiers['Displace'].strength = 3.5
    #bpy.data.objects['tile'].modifiers['Displace'].mid_level = .1
    bpy.ops.object.shade_smooth()
    #bpy.ops.transform.resize(value=(8.48719, 8.48719, 8.48719), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)

def main():
    lat = random.randint(-90,90) # -90 to 90
    lon = random.randint(-180,180) # -180 to 180
    filename = 'tile4.png'
    get_image(lat, lon)
    render_tile(lat, lon)
main()


import hashlib
import random
import time
from concurrent.futures import ThreadPoolExecutor
from typing import Dict, List, Optional, Union

from Pyro5.api import Proxy, expose
from Pyro5.errors import CommunicationError

from .node import Node, NodeType, Linker
from .finger_table import FingerTable
from .hash_table import HashTable
from .monitoring import echo_error, monitor

USE_MONITOR = False

@expose
class ChordNode(Node):

    _node_type = NodeType.chord

    def __init__(
        self,
        id: int,
        linker: Linker,
        cache_size: int,
        use_stabilization: bool = True,
        stabilization_interval: int = 1000,
        fix_finger_interval: int = 1000,
    ) -> None:
        self._id = id
        self.linker = linker
        self._ft = FingerTable(id, linker.BITS_COUNT)
        self.MAX = linker.MAX
        self.BIT_COUNT = linker.BITS_COUNT

        self.use_stabilization = use_stabilization
        self.stabilization_interval = stabilization_interval
        self.fixing_fingers_interval = fix_finger_interval

        self.hash_table = HashTable(cache_size)
        self.executor = ThreadPoolExecutor()

    @property
    def id(self) -> int:
        return self._id

    @property
    def finger_table(self) -> FingerTable:
        return self._ft

    @property
    def node_type(self) -> NodeType:
        return self._node_type

    @property
    def serialized_finger_table(self) -> List[str]:
        return [str(x) for x in self.finger_table]

    @property
    def serialized_hash_table_keys(self):
        return [s for s in self.hash_table]

    ###################################
    # Successor - Predecessor Section #
    ###################################
    @property
    def successor(self) -> Proxy:
        """Return the Proxy objecto to the successor node"""
        return self.linker.get_node(self.node_type, self._ft[1].node)

    @property
    def predecessor(self) -> Proxy:
        """Return the Proxy objecto to the predecessor node"""
        return self.linker.get_node(self.node_type, self._ft[0].node)

    @property
    def successor_id(self) -> Optional[int]:
        return self._ft[1].node

    @property
    def predecessor_id(self) -> Optional[int]:
        return self._ft[0].node

    def set_successor(self, value: Optional[int]):
        self._ft[1].node = value

    def set_predecessor(self, value: Optional[int]):
        self._ft[0].node = value

    #######
    # End #
    #######

    #########
    # Utils #
    #########
    def hash(self, key: str) -> int:
        return int(hashlib.md5(key.encode()).hexdigest(), base=16) % self.MAX

    def in_between(self, k: int, a: int, b: int, equals: bool = True) -> bool:
        a %= self.MAX
        b %= self.MAX

        if a == b:
            return equals
        if a < b:
            return a <= k < b
        return a <= k < b + self.MAX or (a <= k + self.MAX and k < b)

    #######
    # End #
    #######

    ##################
    # Hash Table API #
    ##################
    @monitor(active=USE_MONITOR)
    def insert(self, key: str, value: str):
        hashed_key = self.hash(key)
        print(hashed_key)
        node = self.find_successor(hashed_key)
        if node.id == self.id:
            self.hash_table[key] = value
        else:
            node.insert(key, value)

    @monitor(active=USE_MONITOR)
    def constains(self, key: str) -> bool:
        hashed_key = self.hash(key)
        node = self.find_successor(hashed_key)
        return (node.id == self.id and key in self.hash_table) or (
            node.id != self.id and node.contains(key)
        )

    @monitor(active=USE_MONITOR)
    def get(self, key: str) -> Optional[str]:
        hashed_key = self.hash(key)
        node = self.find_successor(hashed_key)
        if node.id == self.id:
            return self.hash_table[key] if key in self.hash_table else None
        else:
            return node.get(key)

    @monitor(active=USE_MONITOR)
    def pop_in_interval(self, start: int, end: int) -> Dict[str, str]:
        """
        Pop keys of the cache hashed in interval [start, end]
        """

        data = {
            x: self.hash_table[x]
            for x in self.hash_table
            if self.in_between(self.hash(x), start, end + 1)
        }
        self.hash_table.pop_many(data.keys())
        return data

    @monitor(active=USE_MONITOR)
    def update_hash_table(self):
        """
        Update the node cache transfering the keys from it successor
        The keys of a node are hashed in interval [predecessor + 1, self.id]
        """
        if self.successor_id == self.id or self.predecessor_id is None:
            return

        data = self.successor.pop_in_interval(self.predecessor_id + 1, self.id)
        self.hash_table.update(data)

    @monitor(active=USE_MONITOR)
    def update_hash_table_with_keys(self, keys):
        self.hash_table.update(keys)

    #######
    # End #
    #######

    ##########################
    # Find Successor Section #
    ##########################
    @monitor(active=USE_MONITOR)
    def find_successor(self, k: int) -> Union["ChordNode", Proxy]:
        node = self.find_predecessor(k)
        return node.successor

    @monitor(active=USE_MONITOR)
    def find_predecessor(self, key: int) -> Union["ChordNode", Proxy]:
        node = self

        while not self.in_between(key, node.id + 1, node.successor_id + 1):
            node = node.closest_preceding_finger(key)

        return node

    @monitor(active=USE_MONITOR)
    def closest_preceding_finger(self, key: int) -> Union["ChordNode", Proxy]:
        ft = self.finger_table

        for i in range(self.linker.BITS_COUNT, 0, -1):
            if ft[i].node is not None and self.in_between(ft[i].node, self.id + 1, key):
                node = self.linker.get_node(self.node_type, self._ft[i].node)
                return node
        return self

    #######
    # End #
    #######

    @monitor(active=USE_MONITOR)
    def join(self, anchor_node):
        if not self.use_stabilization and anchor_node is not None:
            self.init_finger_table(anchor_node)
            self.update_others()
            self.update_hash_table()
        elif self.use_stabilization:
            if anchor_node is not None:
                for entry in self.finger_table:
                    entry.node = None

                self.set_successor(anchor_node.find_successor(self.id).id)

            self.executor.submit(self.stabilize_subprocess)
            self.executor.submit(self.fix_fingers_subprocess)

    ##############################
    # Join without stabilization #
    ##############################
    @monitor(active=USE_MONITOR)
    def init_finger_table(self, anchor_node: Union["ChordNode", Proxy]):
        ft = self.finger_table  # I do this so as not to write a lot

        successor = anchor_node.find_successor(ft[1].start)
        ft[1].node = successor.id
        ft[0].node = successor.predecessor_id
        successor.set_predecessor(self.id)

        for i in range(1, self.linker.BITS_COUNT):
            if self.in_between(ft[i + 1].start, self.id, ft[i].node):
                ft[i + 1].node = ft[i].node
            else:
                succ = anchor_node.find_successor(ft[i + 1].start).id
                if self.in_between(self.id, ft[i + 1].start, succ, False):
                    ft[i + 1] = self.id
                else:
                    ft[i + 1] = succ

    @monitor(active=USE_MONITOR)
    def update_others(self):
        for i in range(1, self.linker.BITS_COUNT + 1):
            node = self.find_predecessor((self.id - 2 ** (i - 1)) % self.MAX)
            node.update_finger_table(self.id, i)

    @monitor(active=USE_MONITOR)
    def update_finger_table(self, new_id: int, index: int):
        ft = self.finger_table

        if self.in_between(new_id, self.id, ft[index].node):
            ft[index].node = new_id

            # node with the "new_id" id is calling remote this node
            # and it finger table is computed correctly
            if self.predecessor_id == new_id:
                return
            predecessor = self.predecessor
            predecessor.update_finger_table(new_id, index)

    #######
    # End #
    #######

    ###########################
    # Join with stabilization #
    ###########################
    @monitor(active=USE_MONITOR)
    def stabilize_subprocess(self):
        interval = self.stabilization_interval
        interval_over_4 = interval // 4
        while True:
            waiting_time = random.randint(
                interval - interval_over_4, interval + interval_over_4
            )  # milliseconds

            try:
                self.stabilize()
            except Exception as e:
                echo_error(e)

            time.sleep(waiting_time / 1000)

    @monitor(active=USE_MONITOR)
    def stabilize(self):
        node_id = self.successor.predecessor_id

        # check if exists a better succesor
        if node_id is not None and self.in_between(
            node_id, self.id + 1, self.successor_id, equals=False
        ):
            self.set_successor(node_id)

        self.successor.notify(self)
        self.update_hash_table()

    @monitor(active=USE_MONITOR)
    def notify(self, node):
        # check if node has not been eliminated from the network
        if self.predecessor_id is not None and self.predecessor_id != node.id:
            try:
                self.predecessor.id
            except CommunicationError:
                self.set_predecessor(None)

        # check if exist a better predecessor
        if self.predecessor_id is None or self.in_between(
            node.id, self.predecessor_id + 1, self.id
        ):
            self.set_predecessor(node.id)
            self.predecessor.update_hash_table()

    @monitor(active=USE_MONITOR)
    def fix_fingers_subprocess(self):
        interval = self.stabilization_interval
        interval_over_4 = interval // 4
        while True:
            waiting_time = random.randint(
                interval - interval_over_4, interval + interval_over_4
            )  # milliseconds
            try:
                self.fix_fingers()
            except Exception as e:
                echo_error(e)

            time.sleep(waiting_time / 1000)

    @monitor(active=USE_MONITOR)
    def fix_fingers(self):
        if self.BIT_COUNT < 2:
            return
        i = random.randint(2, self.BIT_COUNT)
        self.finger_table[i].node = self.find_successor(self.finger_table[i].start).id

    #######
    # End #
    #######

    #############
    # disconect #
    #############
    @monitor(active=USE_MONITOR)
    def disconnect(self):
        succ = self.successor
        pred = self.predecessor
        succ.set_predecessor(pred.id)
        pred.set_successor(succ.id)
        succ.update_hash_table_with_keys(
            {key: self.hash_table[key] for key in self.hash_table}
        )

        succ._pyroRelease()
        pred._pyroRelease()
        self.linker.remove_node(self.node_type, self.id)

    #######
    # End #
    #######

    def start_loop(self):
        self.linker.start_loop()

    def __str__(self) -> str:
        return f"Node.{self.id}"

    def __repr__(self) -> str:
        return str(self)


from __future__ import annotations

import os
from random import randrange

from ndspy.rom import NintendoDSRom
from PIL import Image, ImageDraw

from skytemple_files.graphics.kao.handler import KaoHandler

rom: NintendoDSRom = NintendoDSRom.fromFile(
    os.path.join("..", "..", "..", "..", "..", "..", "CLEAN_ROM", "pmdsky.nds")
)

kao = KaoHandler.deserialize(rom.getFileByName("FONT/kaomado.kao"))

for idx, sidx, img in kao:
    print(idx, sidx, img)
    pil = Image.new("P", (40, 40))
    pal = bytearray()
    pil.putpalette([randrange(0, 256) for _ in range(0, 256 * 3)])
    for i in range(0, 15):
        pil.putpixel((i, 0), i)
    d = ImageDraw.Draw(pil)
    d.text((5, 5), str(idx), fill=1)
    d.text((5, 20), str(sidx), fill=2)
    if img is not None:
        img.set(pil)
    if idx in (0, 552, 1153):
        os.makedirs(f"rgb/{idx:04}", exist_ok=True)
        os.makedirs(f"{idx:04}", exist_ok=True)
        pil.save(f"{idx:04}/{sidx:02}.png")
        pil.convert("RGB").save(f"rgb/{idx:04}/{sidx:02}.png")

with open("kaomado.kao", "wb") as f:
    f.write(KaoHandler.serialize(kao))


import os
from pprint import pprint as pp

import colorgram
from flask import Flask
from flask import render_template
app = Flask(__name__)

def get_filenames(f_dir, prefix='', suffix=''):
    """Get list of filenames within a directory. Optionally scope by prefix/suffix."""
    f_names = []
    for r,d,files in os.walk(f_dir):
        for f in files:
            if f.startswith(prefix) and f.endswith(suffix):
                f_names.append('{}/{}'.format(r, f))
    return f_names

def get_colors(num_cols=6):
    imgs = []
    for suffix in ['jpg', 'jpeg', 'png', 'gif']:
        for fname in get_filenames('static', suffix=suffix):
            colors = colorgram.extract(fname, num_cols)
            summary = []
            for color in colors:
                summary.append({
                    'rgb':tuple(color.rgb),
                    'hsl':tuple(color.hsl),
                    'percent':round(color.proportion * 100, 1)
                })
            imgs.append({
                'fname':fname,
                'colors':summary
            })
    return imgs

@app.route("/")
def home():
    imgs = get_colors()
    return render_template('color.html', imgs=imgs)

@app.route("/api")
def api_home():
    imgs = get_colors()
    return {'images':imgs}

@app.route("/<int:num_cols>")
def number_colors(num_cols):
    imgs = get_colors(num_cols)
    return render_template('color.html', imgs=imgs)


'''
Created by Francesco
29 November 2021
'''
#functions and script to compute correlations in space and time
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm
import sys
import os
import utils
import utilsPlot as uplot

############################ Static Structure Factor ###########################
def computeStructureFactor(dirName, plot="plot"):
    boxSize = np.loadtxt(dirName + os.sep + "boxSize.dat")
    phi = utils.readFromParams(dirName, "phi")
    numParticles = int(utils.readFromParams(dirName, "numParticles"))
    rad = np.loadtxt(dirName + os.sep + "particleRad.dat")
    meanRad = np.mean(rad)
    qList = np.arange(np.pi/2, np.pi/(15*np.min(rad)), 2*np.min(rad))
    pos = utils.getPBCPositions(dirName + os.sep + dir + "/particlePos.dat", boxSize)
    sfList = utils.getStructureFactor(pos, qList, numParticles)
    if(plot == "plot"):
        uplot.plotCorrelation(qList, sfList, "$Structure$ $factor,$ $S(q)$", "$Wave$ $vector$ $magnitude,$ $q$")
    else:
        return sfList

def averageStructureFactor(dirName, dirSpacing=1):
    numParticles = int(utils.readFromParams(dirName, "numParticles"))
    scale = 2*np.mean(np.loadtxt(dirName + os.sep + "particleRad.dat"))
    boxSize = np.array(np.loadtxt(dirName + os.sep + "boxSize.dat"),dtype=np.float64)
    qList = np.arange(1.2, np.pi/(5*scale), 2*scale)
    dirList, timeList = utils.getOrderedDirectories(dirName)
    timeList = timeList.astype(int)
    dirList = dirList[np.argwhere(timeList%dirSpacing==0)[:,0]]
    sf = np.zeros(qList.shape[0])
    for dir in dirList:
        pos = utils.getPBCPositions(dirName + os.sep + dir + "/particlePos.dat", boxSize)/scale
        sf += utils.getStructureFactor(pos, qList, numParticles)
    sf[sf>0] /= dirList.shape[0]
    np.savetxt(dirName + os.sep + "structureFactor.dat", np.column_stack((qList, sf)))
    uplot.plotCorrelation(qList, sf, "$Structure$ $factor,$ $S(q)$", "$Wave$ $vector$ $magnitude,$ $q$", markersize=0, linewidth=1)#, logx=True, logy=True)

########################## Average Velocity Correlator #########################
def averageVelocityStructureFactor(dirName, dirSpacing=1):
    numParticles = int(utils.readFromParams(dirName, "numParticles"))
    scale = 2*np.mean(np.loadtxt(dirName + os.sep + "particleRad.dat"))
    boxSize = np.loadtxt(dirName + os.sep + "boxSize.dat")
    qList = np.concatenate((np.geomspace(1e-03, 1e-01, 30), np.geomspace(1e-01, 1, 50), np.geomspace(1, 3, 50)))
    dirList, timeList = utils.getOrderedDirectories(dirName)
    timeList = timeList.astype(int)
    dirList = dirList[np.argwhere(timeList%dirSpacing==0)[:,0]]
    velsf = np.zeros(qList.shape[0])
    for dir in dirList:
        pos = utils.getPBCPositions(dirName + os.sep + dir + "/particlePos.dat", boxSize)/scale
        vel = np.array(np.loadtxt(dirName + os.sep + dir + "/particleVel.dat"), dtype=np.float64)
        velsf += utils.getVelocityStructureFactor(pos, vel, qList, numParticles)
    velsf[velsf>0] /= dirList.shape[0]
    np.savetxt(dirName + os.sep + "velocitySF.dat", np.column_stack((qList, velsf)))
    uplot.plotCorrelation(qList, velsf/velsf[-1], "$Velocity$ $structure$ $factor,$ $S_{v}(q)$", "$Wave$ $vector$ $magnitude,$ $q$", markersize=0, linewidth=1)

########################### Energy Transform in Space ##########################
def averageSpaceFourierEnergy(dirName, dirSpacing=1):
    numParticles = int(utils.readFromParams(dirName, "numParticles"))
    scale = 2*np.mean(np.loadtxt(dirName + os.sep + "particleRad.dat"))
    boxSize = np.loadtxt(dirName + os.sep + "boxSize.dat")
    #qList = np.linspace(1e-03, np.pi/scale, 50)
    qList = np.concatenate((np.geomspace(1e-03, 1e-01, 30), np.geomspace(1e-01, 1, 50), np.geomspace(1, 3, 50)))
    #qList = np.concatenate((-qList, qList))
    qList = np.sort(qList)
    energyq = np.zeros((qList.shape[0],3))
    dirList, timeList = utils.getOrderedDirectories(dirName)
    timeList = timeList.astype(int)
    dirList = dirList[np.argwhere(timeList%dirSpacing==0)[:,0]]
    for dir in dirList:
        pos = utils.getPBCPositions(dirName + os.sep + dir + "/particlePos.dat", boxSize)/scale
        vel = np.array(np.loadtxt(dirName + os.sep + dir + "/particleVel.dat"), dtype=np.float64)
        epot = np.array(np.loadtxt(dirName + os.sep + dir + "/particleEnergy.dat"), dtype=np.float64)
        kqtemp, uqtemp, kctemp = utils.getSpaceFourierEnergy(pos, vel, epot, qList, numParticles)
        energyq[:,0] += kqtemp
        energyq[:,1] += uqtemp
        energyq[:,2] += kctemp
    energyq[energyq>0] /= dirList.shape[0]
    np.savetxt(dirName + os.sep + "spaceFourierEnergy.dat", np.column_stack((qList, energyq)))
    uplot.plotCorrelation(qList, energyq[:,0], "$\\tilde{K}(q)$", "$Wave$ $vector$ $magnitude,$ $q$", markersize=0, linewidth=1, logy=True)
    #plt.show()

########################### Energy Transform in Time ###########################
def averageTimeFourierEnergy(dirName, dirSpacing=1):
    numParticles = int(utils.readFromParams(dirName, "numParticles"))
    dirList, timeList = utils.getOrderedDirectories(dirName)
    timeList = timeList.astype(int)
    dirList = dirList[np.argwhere(timeList%dirSpacing==0)[:,0]]
    timeList = timeList[np.argwhere(timeList%dirSpacing==0)[:,0]]
    energyTransform = utils.getTimeFourierEnergy(dirName, dirList, dirSpacing, numParticles)
    np.savetxt(dirName + os.sep + "timeFourierEnergy.dat", energyTransform)
    uplot.plotCorrelation(energyTransform[:,0], energyTransform[:,1], "$\\langle \\tilde{K}(\\omega) \\rangle$", "$Frequency,$ $\\omega$", markersize=0, linewidth=1, logy=True)
    #plt.show()

########################### Average Velocity Transform #########################
def averageSpaceFourierVelocity(dirName, dirSpacing=1):
    numParticles = int(utils.readFromParams(dirName, "numParticles"))
    scale = 2*np.mean(np.loadtxt(dirName + os.sep + "particleRad.dat"))
    boxSize = np.loadtxt(dirName + os.sep + "boxSize.dat")
    qList = np.geomspace(1e-03, 10, 150)#, np.geomspace(1, 3, 50)))
    #qList = np.linspace(2*np.pi/scale, np.sqrt(boxSize[0]*boxSize[0] + boxSize[1]*boxSize[1]), 100)
    dirList, timeList = utils.getOrderedDirectories(dirName)
    timeList = timeList.astype(int)
    dirList = dirList[np.argwhere(timeList%dirSpacing==0)[:,0]]
    vf = np.zeros((qList.shape[0],2))
    for dir in dirList:
        pos = utils.getPBCPositions(dirName + os.sep + dir + "/particlePos.dat", boxSize)/scale
        vel = np.array(np.loadtxt(dirName + os.sep + dir + "/particleVel.dat"), dtype=np.float64)
        vf += utils.getSpaceFourierVelocity(pos, vel, qList, numParticles)
    vf[vf>0] /= dirList.shape[0]
    np.savetxt(dirName + os.sep + "spaceFourierVel.dat", np.column_stack((qList, vf)))
    uplot.plotCorrelation(qList, vf[:,0], "$\\tilde{v}_\\parallel(q)$", "$Wave$ $vector$ $magnitude,$ $q$", markersize=0, linewidth=1, logx=True)
    #plt.show()

######################### Velocity Correlation in Time #########################
def averageTimeFourierVel(dirName, dirSpacing=1):
    numParticles = int(utils.readFromParams(dirName, "numParticles"))
    timeStep = utils.readFromParams(dirName, "dt")
    dirList, timeList = utils.getOrderedDirectories(dirName)
    timeList = timeList.astype(int)
    dirList = dirList[np.argwhere(timeList%dirSpacing==0)[:,0]]
    timeList = timeList[np.argwhere(timeList%dirSpacing==0)[:,0]]
    velTransform = utils.getTimeFourierVel(dirName, dirList, dirSpacing, numParticles)
    spacing = (timeList[1]-timeList[0])
    velTransform[:,0] /= spacing
    np.savetxt(dirName + os.sep + "fourierVelCorr.dat", velTransform)
    uplot.plotCorrelation(velTransform[:,0], velTransform[:,2], "$\\langle | \\vec{v}(\\omega) |^2 \\rangle$", "$Frequency,$ $\\omega$", markersize=0, linewidth=1, logy=True, logx=True)
    #plt.show()

if __name__ == '__main__':
    dirName = sys.argv[1]
    whichCorr = sys.argv[2]

    if(whichCorr == "psf"):
        plot = sys.argv[3]
        computeStructureFactor(dirName)

    elif(whichCorr == "averagesf"):
        dirSpacing = int(sys.argv[3])
        averageStructureFactor(dirName, dirSpacing)

    elif(whichCorr == "averagevsf"):
        dirSpacing = int(sys.argv[3])
        averageVelocityStructureFactor(dirName, dirSpacing)

    elif(whichCorr == "spaceeq"):
        dirSpacing = int(sys.argv[3])
        averageSpaceFourierEnergy(dirName, dirSpacing)

    elif(whichCorr == "timeeq"):
        dirSpacing = int(sys.argv[3])
        averageTimeFourierEnergy(dirName, dirSpacing)

    elif(whichCorr == "spacevf"):
        dirSpacing = int(sys.argv[3])
        averageSpaceFourierVelocity(dirName, dirSpacing)

    elif(whichCorr == "velf"):
        dirSpacing = int(sys.argv[3])
        averageTimeFourierVel(dirName, dirSpacing)

    else:
        print("Please specify the correlation you want to compute")


#!/usr/bin/env python
import subprocess
import argparse
import itertools
import json
import sys

parser = argparse.ArgumentParser(description='Create a grid from json')
parser.add_argument('filename', help='Json or python that contains the grid parameters')
parser.add_argument('--cmd', required=True)
parser.add_argument('--project', required=True)
parser.add_argument('--experiment', required=True)
parser.add_argument('--run', action='store_true', default=False)

args = parser.parse_args()
with open(args.filename, 'r') as f:
    if args.filename.endswith('.json'):
        grid = json.load(f)
    else:
        grid = eval(f.read())

project = args.project
experiment= args.experiment
namefmt = grid['name']
parameters = grid['parameters']

perms = list(itertools.product(*parameters.values()))

names = set()
for p in perms:
    argstr = ""
    name = namefmt.format(**dict(zip(parameters.keys(),
                          [str(p_i).replace('/', '~').replace('-', '_') for p_i in p])))
    for i,k in enumerate(parameters.keys()):
        if type(p[i]) == bool:
            if p[i]:
                argstr += " --" + str(k)
        else:
            argstr += " --" + str(k) + " " + str(p[i]).format(name=name)
    if name in names:
        sys.stderr.write('WARNING: {} already exists\n'.format(name))
    else:
        names.add(name)
    cmd = "PROJECT={project} EXPERIMENT={experiment} NAME={name} {cmd} {argstr}".format(
    #cmd = "sbatch --partition=learnfair --nodes=1 --ntasks-per-node=1 --job-name={name} " \
    #    "--output=checkpoint-data/%j.out --error=checkpoint-data/%.err --gres=gpu:1 --cpus-per-task=4 " \
    #    "{cmd} {argstr}".format(
        project=project,
        experiment=experiment,
        name=name,
        cmd=args.cmd,
        argstr=argstr)
    if args.run:
        subprocess.run(cmd, shell=True)
    else:
        print(cmd)


from django.core.cache import cache
from django.http import Http404
from django.utils.translation import ugettext as _
from rest_framework.permissions import BasePermission

from apps.api import AuthApi
from apps.dataflow.handlers.dataid import DataId
from apps.dataflow.handlers.flow import Flow

# from apps.dataflow.models import RoleUserAccess

class Permission(object):
    @staticmethod
    def has_project_perm(username, project_id, action="project.retrieve"):
        """
        对象级的权限控制，用户是否具有项目的权限
        """
        return AuthApi.check_user_perm({"user_id": username, "action_id": action, "object_id": project_id})

    @staticmethod
    def batch_judge_flow_project_perm(username, project_ids, action="project.retrieve"):
        """
        批量判断用户是否具有flow对应项目的权限
        """
        permissions_list = [
            {"user_id": username, "action_id": action, "object_id": project_id} for project_id in project_ids
        ]
        has_permission_list = AuthApi.batch_check_user_perm(
            {
                "permissions": permissions_list,
            }
        )
        return has_permission_list

    @staticmethod
    def has_biz_perm(username, biz_id, action="biz.manage"):
        """
        对象级的权限控制，用户是否具有业务的权限
        """
        return AuthApi.check_user_perm({"user_id": username, "action_id": action, "object_id": biz_id})

    @staticmethod
    def has_biz_list_perm(username, biz_ids, action="biz.manage"):
        """
        用户是否有业务列表的权限（批量）
        """
        biz_ids = [int(biz_id) for biz_id in biz_ids]
        return all([Permission.has_biz_perm(username, biz_id, action) for biz_id in biz_ids])

    @staticmethod
    def has_dataid_perm(username, data_id, action="raw_data.retrieve"):
        """
        对象级的权限控制，用户是否具有 dataid 的权限
        """
        return AuthApi.check_user_perm({"user_id": username, "action_id": action, "object_id": data_id})

    @staticmethod
    def has_flow_perm(username, flow_id, action="flow.retrieve"):
        """
        对象级的权限控制，用户是否具有 flow 的权限
        """
        return AuthApi.check_user_perm({"user_id": username, "action_id": action, "object_id": flow_id})

    @staticmethod
    def has_result_table_perm(username, result_table_id, action="result_table.retrieve"):
        """
        对象级的权限控制，用户是否具有RT的权限
        @param action {String} 操作方式，目前仅支持 query_info、query_data、others
        @todo 需要关联 project_biz_data 确定是否有二级授权
        """
        return AuthApi.check_user_perm({"user_id": username, "action_id": action, "object_id": result_table_id})

def perm_cache(func):
    """
    根据用户+请求参数，把权限验证结果结果进行缓存
    """

    def _deco(self, request, view):
        # 只对查询（GET方法）进行权限缓存
        if request.method != "GET":
            return func(self, request, view)

        user = request.user.username
        kwargs = "_".join("{}:{}".format(_k, _w) for _k, _w in list(view.kwargs.items()))
        cache_name = "{}__{}__{}".format(user, view.action, kwargs)
        perm = cache.get(cache_name)
        if perm is None:
            perm = func(self, request, view)
            cache.set(cache_name, perm, 60)

        return perm

    return _deco

class ProjectPermissions(BasePermission):
    """
    通过 DRF 在对象层面进行权限控制
    """

    message = _("您没有项目权限")
    DELETE_CHECK_EXEMPT_ACTION = ["restore"]

    M_VIEW_ACTION = {
        "update_project_user": "project.manage",
        "destroy": "project.delete",
        "restore": "project.manage",
        "partial_update": "project.update",
    }

    @perm_cache
    def has_permission(self, request, view):
        project_id = view.kwargs.get("project_id", None)

        if project_id is None:
            return True

        username = request.user.username
        action = self.M_VIEW_ACTION.get(view.action, "project.retrieve")

        return Permission.has_project_perm(username, project_id, action=action)

class DataIdPermissions(BasePermission):
    """
    通过 DRF 在对象层面进行权限控制
    """

    M_VIEW_ACTION = {
        "retrieve": "raw_data.retrieve",
        "list_data_flow": "raw_data.retrieve",
        "get_data_id": "raw_data.retrieve",
        "get_latest_msg": "raw_data.query_data",
        "list_latest_msg": "raw_data.query_data",
        "list_data_count_by_time": "raw_data.query_data",
    }

    message = _("您没有 DataId 权限")

    @perm_cache
    def has_permission(self, request, view):
        data_id = view.kwargs.get("raw_data_id", None)
        if data_id is None:
            return True

        if not DataId(data_id=data_id).is_exist():
            raise Http404(_("DataID不存在"))

        username = request.user.username
        action = self.M_VIEW_ACTION[view.action] if view.action in self.M_VIEW_ACTION else "raw_data.retrieve"
        return Permission.has_dataid_perm(username, data_id, action=action)

class FlowPermissions(BasePermission):
    """
    Flow权限
    """

    @perm_cache
    def has_permission(self, request, view):
        username = request.user.username
        # 创建权限特殊处理
        if view.action == "create":
            project_id = request.data.get("project_id")

            return Permission.has_project_perm(username, project_id)

        # 批量权限特殊处理
        multi_action = ["multi_destroy", "multi_start", "multi_stop"]
        if view.action in multi_action:
            flow_ids = request.data.get("flow_ids")
            # @todo 有效率问题，待优化成批量鉴权
            return all([Permission.has_flow_perm(username, flow_id) for flow_id in flow_ids])

        flow_id = view.kwargs.get("flow_id", None)
        # 非特殊处理的list_route接口
        if flow_id is None:
            return True

        if not Flow(flow_id=flow_id).is_exist():
            raise Http404(_("任务不存在"))

        return Permission.has_flow_perm(username, flow_id)

class ResultTablePermissions(BasePermission):
    """
    对结果表操作，统一进行权限控制
    """

    M_VIEW_ACTION = {
        "retrieve": "result_table.retrieve",
        "list_query_history": "result_table.retrieve",
        "get_storage_info": "result_table.retrieve",
        "get_rt_schema": "result_table.retrieve",
        "list_field": "result_table.retrieve",
        "set_selected_history": "result_table.retrieve",
        "delete_selected_history": "result_table.retrieve",
        "list_data_count_by_time_rt": "result_table.query_data",
        "list_latest_msg": "result_table.query_data",
        # 由后台API保证权限，此处豁免校验
        "query_es": None,
        "get_es_chart": None,
        "query_rt": None,
        "get_latest_msg": "result_table.query_data",
        "list_kafka_latest_msg": "result_table.query_data",
    }

    @perm_cache
    def has_permission(self, request, view):
        username = request.user.username
        result_table_id = view.kwargs.get("result_table_id", None)

        if result_table_id is None:
            return True

        action = self.M_VIEW_ACTION[view.action] if view.action in self.M_VIEW_ACTION else "result_table.retrieve"

        # action是None时豁免校验
        if action is None:
            return True
        return Permission.has_result_table_perm(username, result_table_id, action=action)

class BizPermissions(BasePermission):
    """
    通过 DRF 在对象层面进行权限控制
    """

    message = _("您没有业务权限")

    def has_permission(self, request, view):
        biz_id = view.kwargs.get("biz_id", None)
        if biz_id is None:
            return True

        username = request.user.username

        return Permission.has_biz_perm(username, biz_id)


import torch
import coremltools as ct

from conv_tasnet import ConvTasNet
from cached_model import CachedModel

from thop import profile

RECEPTIVE_FIELD = 516
R = 2
X = 7
N = 256
H = 512

base_model = CachedModel("checkpoints/clearvoice_iphone_causal_anechoic_reverb/final.pth.tar", input_channels=2, N=N, use_cuda=False)

total_macs = 0
# Encoder
dummy_input = torch.randn(1, 2, 400)
encoder_buffer = torch.randn(1, N, RECEPTIVE_FIELD)

# macs, params = profile(base_model.encoder_object, inputs=(dummy_input, encoder_buffer))
# total_macs += params

traced_model = torch.jit.trace(base_model.encoder_object, (dummy_input, encoder_buffer))

ml_input1 = ct.TensorType(name='mixture', shape=(1, 2, 400))
ml_input2 = ct.TensorType(name='encoder_buffer', shape=(1, N, RECEPTIVE_FIELD))

model = ct.convert(
    traced_model,
    inputs=[ml_input1, ml_input2], #name "input_1" is used in 'quickstart'
)
model.save("ClearVoiceEncoder.mlmodel")

# LayerNorm, Bottleneck Conv
dummy_input1 = torch.randn(1, N, RECEPTIVE_FIELD)
dummy_input2 = torch.randn(15, 1, N, RECEPTIVE_FIELD)

# macs, params = profile(base_model.layernorm_object, inputs=(dummy_input1, dummy_input2))
# total_macs += params

traced_model = torch.jit.trace(base_model.layernorm_object, (dummy_input1, dummy_input2))
ml_input1 = ct.TensorType(name='encoder_buffer', shape=(1, N, RECEPTIVE_FIELD))
ml_input2 = ct.TensorType(name='conv_buffers', shape=(15, 1, N, RECEPTIVE_FIELD))

model = ct.convert(
    traced_model,
    inputs=[ml_input1, ml_input2], #name "input_1" is used in 'quickstart'
)
model.save("ClearVoiceLayerNorm.mlmodel")

# # Separator
# dummy_input = torch.randn(1, 128, RECEPTIVE_FIELD)
# traced_model = torch.jit.trace(base_model.separator_object, dummy_input)
# ml_input = ct.TensorType(name='layernorm_buffer', shape=(1, 128, RECEPTIVE_FIELD))

# model = ct.convert(
#     traced_model,
#     inputs=[ml_input], #name "input_1" is used in 'quickstart'
# )

# model.save("ClearVoiceSeparator.mlmodel")

# Decoder
dummy_input1 = torch.randn(1, N, RECEPTIVE_FIELD)
dummy_input2 = torch.randn(15, 1, N, RECEPTIVE_FIELD)

traced_model = torch.jit.trace(base_model.decoder_object, (dummy_input1, dummy_input2))

# macs, params = profile(base_model.decoder_object, inputs=(dummy_input1, dummy_input2))
# total_macs += params

ml_input1 = ct.TensorType(name='encoder_buffer', shape=(1, N, RECEPTIVE_FIELD))
ml_input2 = ct.TensorType(name='conv_buffers', shape=(15, 1, N, RECEPTIVE_FIELD))

model = ct.convert(
    traced_model,
    inputs=[ml_input1, ml_input2], #name "input_1" is used in 'quickstart'
)

# print("Total Macs: {}".format(total_macs))

model.save("ClearVoiceDecoder.mlmodel")