# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.linux-i686/egg/tests/test_platform.py
# Compiled at: 2011-04-20 15:31:07
import unittest, os, sys, tempfile
from linkexchange.platform import Platform
from linkexchange.config import file_config
from test_clients import SapeTestServer

class SapeTestServer1(SapeTestServer):
    data = {'/': [
           'xlink1',
           'xlink2'],
       '/path/1': [
                 'xlink1',
                 'xlink2',
                 'xlink3',
                 'xlink4'],
       '/path/2': [
                 'Plain text and link text'],
       '__sape_new_url__': '',
       '__sape_delimiter__': '. '}

class SapeTestServer2(SapeTestServer):
    data = {'/': [
           'ylink1'],
       '__sape_new_url__': '',
       '__sape_delimiter__': '. '}

class PlatformTestCase(unittest.TestCase):

    def setUpClass(cls):
        cls.server1 = SapeTestServer1()
        cls.server2 = SapeTestServer2()
        clients = [
         ('sape', [],
          dict(user='user123456789', db_driver=('mem', ), server_list=[
           cls.server1.url.replace('%', '%%')])),
         (
          'sape', [],
          dict(user='user123456789', db_driver=('mem', ), server_list=[
           cls.server2.url.replace('%', '%%')]))]
        cls.platform = Platform(clients=clients)

    setUpClass = classmethod(setUpClass)

    def tearDownClass(cls):
        del cls.platform
        del cls.server1
        del cls.server2

    tearDownClass = classmethod(tearDownClass)

    def testGetRawLinks(self):
        lx = self.platform.get_raw_links('http://example.com/')
        self.assertEqual(unicode(lx[0]), 'xlink1')
        self.assertEqual(unicode(lx[1]), 'xlink2')
        self.assertEqual(unicode(lx[2]), 'ylink1')

    def testGetBlocks(self):
        formatters = [
         (
          'inline', [2],
          dict(class_='links', class_for_empty='empty', suffix='. ')),
         (
          'list', [None], dict(id='links'))]
        bx = self.platform.get_blocks('http://example.com/', formatters)
        self.assertEqual(unicode(bx[0]), '
xlink1. xlink2.
')
        self.assertEqual(unicode(bx[1]), '

ylink1

')
        bx = self.platform.get_blocks('http://example.com/notexists', formatters)
        self.assertEqual(unicode(bx[0]), '

')
        self.assertEqual(unicode(bx[1]), '')
        bx = self.platform.get_blocks('http://example.com/path/1', formatters)
        self.assertEqual(unicode(bx[0]), '
xlink1. xlink2.
')
        self.assertEqual(unicode(bx[1]), '

xlink3

xlink4

')
        formatters = [
         (
          'inline', [None], dict()),
         (
          'inline', [None], dict(client=1))]
        bx = self.platform.get_blocks('http://example.com/', formatters)
        self.assertEqual(unicode(bx[0]), '
ylink1
')
        self.assertEqual(unicode(bx[1]), '
xlink1xlink2
')
        return

class PlatformConfigTestCase(PlatformTestCase):
    linkexchange_cfg = '\n[client-1]\ntype = sape\nuser = user123456789\ndb_driver.type = mem\nserver-1 = %(server1)s\n\n[client-2]\ntype = sape\nuser = user123456789\ndb_driver.type = mem\nserver-1 = %(server2)s\n'

    def setUpClass(cls):
        cls.server1 = SapeTestServer1()
        cls.server2 = SapeTestServer2()
        cfgfd, cls.cfgfn = tempfile.mkstemp()
        os.close(cfgfd)
        open(cls.cfgfn, 'w').write(cls.linkexchange_cfg % {'server1': cls.server1.url.replace('%', '%%'),
           'server2': cls.server2.url.replace('%', '%%')})
        vars = {}
        result = file_config(vars, cls.cfgfn)
        assert result == [cls.cfgfn]
        cls.platform = vars['platform']

    setUpClass = classmethod(setUpClass)

    def tearDownClass(cls):
        del cls.platform
        del cls.server1
        del cls.server2
        os.unlink(cls.cfgfn)

    tearDownClass = classmethod(tearDownClass)

    def testLXRefresh(self):
        from linkexchange.commands import lxrefresh
        retcode = os.spawnl(os.P_WAIT, sys.executable, sys.executable, lxrefresh.__file__, '-c', self.cfgfn, '-r', 'http://example.com')
        self.assertEqual(retcode, 0)


from django.shortcuts import render
from django.http import HttpResponseNotAllowed

from . import models
from . import forms

def cadastro(request):
    form= forms.SerieForm()
    if request.method == 'POST':
        print('vou salvar os dados!!')
        form=forms.SerieForm(request.POST)
        print(form)
        if form.is_valid():
            form.save(commit=True)
        else:
            print('ERROR')
    series_list=models.Serie.objects.order_by('nome')
    data_dict = {'serie_records':series_list , 'form':form}
    return render(request, 'serie/serie.html', data_dict)

def delete(request, id):
    try:
        models.Serie.objects.filter(id=id).delete()
        form=forms.SerieForm();
        series_list=models.Serie.objects.order_by('nome')
        data_dict={'form':form, 'serie_records':series_list}
        return render(request, 'serie/serie.html', data_dict)
    except:
        return HttpResponseNotAllowed()

def update(request, id):
    item= models.Serie.objects.get(id=id)
    if request.method=='GET':
        form=forms.SerieForm(initial={'nome':item.nome, 'idGenero':item.idGenero})
        data_dict={'form':form}
        return render(request,'serie/serie_upd.html', data_dict)
    else:
        form=forms.SerieForm(request.POST)
        item.nome=form.data['nome']
        item.idGenero_id=form.data['idGenero'] # COLOQUEI O  _id  PQ SE NAO ELE NAO VAI PEGAR REALMENTE O id, E SIM A DESCRICAO DO GENERO
        item.save()
        form = forms.SerieForm()
        series_list=models.Serie.objects.order_by('nome')
        data_dict={'form':form, 'serie_records':series_list}
        return render(request, 'serie/serie.html', data_dict)


import os
import csv
from io import StringIO
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver

from Data_Manager.models import EnvironmentClass
from .models import Environment, EnvironmentBulkUpload

@receiver(post_save, sender=EnvironmentBulkUpload)
def create_bulk_student(sender, created, instance, *args, **kwargs):
  if created:
    opened = StringIO(instance.csv_file.read().decode())
    reading = csv.DictReader(opened, delimiter=',')
    environment = []
    for row in reading:
      if 'stage' in row and row['stage']:
        stage = row['stage']
        platform = row['platform'] if 'platform' in row and row['platform'] else ''
        database = row['database'] if 'database' in row and row['database'] else ''
        run_stack = row['run_stack'] if 'run_stack' in row and row['run_stack'] else ''
        web_server = row['web_server'] if 'web_server' in row and row['web_server'] else ''
        ip_servername = row['ip_servername'] if 'ip_servername' in row and row['ip_servername'] else ''
        hardware = row['hardware'] if 'hardware' in row and row['hardware'] else ''
        product_app = row['product_app'] if 'product_app' in row and row['product_app'] else ''
        provider = row['provider'] if 'provider' in row and row['provider'] else ''
        port = row['port'] if 'port' in row and row['port'] else ''
        created_by = row['created_by'] if 'created_by' in row and row['created_by'] else ''
        creation_date = row['creation_date'] if 'creation_date' in row and row['creation_date'] else ''
        brd = row['brd'] if 'brd' in row and row['brd'] else ''
        if Environment:
          EnvironmentClass, kind = EnvironmentClass.objects.get_or_create(name=Environment)

        check = Environment.objects.filter(stage=stage).exists()
        if not check:
          environment.append(
            Student(
                stage=stage,
                platform=platform,
                database=database,
                run_stack=run_stack,
                web_server=web_server,
                ip_servername=ip_servername,
                hardware=hardware,
                product_app=product_app,
                provider = provider,
                port=port,
                created_by=created_by,
                creation_date=creation_date,
                brd=brd
            )
          )

    environment.objects.bulk_create(environment)
    instance.csv_file.close()
    instance.delete()

def _delete_file(path):
   """ Deletes file from filesystem. """
   if os.path.isfile(path):
       os.remove(path)

@receiver(post_delete, sender=EnvironmentBulkUpload)
def delete_csv_file(sender, instance, *args, **kwargs):
  if instance.csv_file:
    _delete_file(instance.csv_file.path)

@receiver(post_delete, sender=Environment)
def delete_passport_on_delete(sender, instance, *args, **kwargs):
  if instance.brd:
    _delete_file(instance.brd.path)


__author__ = 'doaa'

from netAnalysis.Peak import *
import csv, datetime
from netAnalysis.experiments.expr_util import *
from collections import OrderedDict
from netAnalysis.scoring.PR_ROC import get_accuracy_realData

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if __name__ == "__main__":

    datasetName = 'Root' #'Net1', 'Net3_conn_final', 'Net4', 'Grene'

    alphas_small = [0.001, 0.01, 0.05, 0.1]
    alphas_large = [0.1, 0.2, 0.4, 0.6, 0.8, 1, 2, 3]
    #
    params = OrderedDict([('fileName', ''), ('priorPercent', 0), ('falsePriorRatio', 0),
                          ('priorWeight', 0.01),
                          ('alpha', 0.1), ('l1_ratio', 0.5), ('isCV', 1), ('fit_intercept', 1),
                          ('scaleX', 1), ('halfLife', 10), ('method', 'FeatureScaling'), ('freeCV', 0)])

    # params = OrderedDict([('fileName', ''), ('priorPercent', 100), ('falsePriorRatio', 0),
    #                       ('priorWeight', 0.01),
    #                       ('alpha', 0.1), ('l1_ratio', 0.5), ('isCV', 1), ('fit_intercept', 1),
    #                       ('scaleX', 1), ('halfLife', 10), ('method', 'PenaltyScaling'), ('freeCV', 1)])

    filename = run_Peak_test(datasetName, params, alphas_small)
    pathToPred = ds.datasets['Root'].get_outFile() + filename
    AUROC, AUPR, _, _ = get_accuracy_realData(network=datasetName, predFile=pathToPred, visualize=False)
    pathToCombined = pathToPred[:-9] + "::Combined"
    AUROC, AUPR, _, _ = get_accuracy_realData(network=datasetName, predFile=pathToCombined, visualize=False)
    mapGeneNames(datasetName='Root', filename=pathToPred)

    # print(paramsFileName)
    # filename = ds.datasets['Root'].get_outFile() + '_priorPerct100_weight0.01__scaled__Nov-18_18-22--03FeatureScaling::InfOnly'
    # mapGeneNames(datasetName='Root', filename=filename)


# coding=utf-8
"""
Восстановление даты после перезагрузки.
"""
import os
import datetime
import time
from pyutils.stopablethread import StopableThread, Singleton
import logging

log = logging.getLogger("date_recovery")

class DateRecovery(StopableThread):
    __metaclass__ = Singleton

    # Период сохранения текущего времени
    UPDATE_PERIOD = 600
    DELAY_PERIOD = 10
    PATH_RECOVER_FILE = "./.date_recovery"

    DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'

    def __init__(self):
        super(DateRecovery, self).__init__()
        self._callbackRecovery = None
        self.start(delay=0)

    def _recover(self):
        """Вызываем каллбек восстановления"""
        if self._callbackRecovery:
            self._callbackRecovery()

    def worker(self, *args, **kwargs):
        """Перезаписываем последний файл"""
        current_date = datetime.datetime.now()
        if os.path.isfile(DateRecovery.PATH_RECOVER_FILE):
            with open(DateRecovery.PATH_RECOVER_FILE, "rb") as f:
                date = f.read()
            old_date = datetime.datetime.strptime(date, self.DATETIME_FORMAT)
            delta = current_date - old_date
            delta_seconds = delta.seconds
            # проверка на ошибочность
            if delta_seconds > self.UPDATE_PERIOD + self.DELAY_PERIOD:
                lag = delta_seconds - (self.UPDATE_PERIOD + self.DELAY_PERIOD)
                log.warning("Delta period is not correctly. Lag is %s second. Run recovery callback." % lag)
                self._recover()
        # добавим новую запись
        with open(DateRecovery.PATH_RECOVER_FILE, "wb") as f:
            t = current_date
            f.write(t.strftime(self.DATETIME_FORMAT))
        return self.UPDATE_PERIOD

    def setRecoveryCallback(self, callback):
        """Вызывать функцию в случае непредвиденного обстоятельства"""
        self._callbackRecovery = callback

if __name__ == "__main__":
    DateRecovery()
    while True:
        time.sleep(1)


from PIL import Image
import numpy as np

def draw(h, w):
    iar = []
    for y in range(h):
        irow = []
        for x in range(w):
            irow.append(get_pxl(x, y))
        iar.append(irow)
    return np.asarray(iar, dtype='uint8')

def get_pxl(x: int, y: int):
    #z3 = ((x**2) + (y**2)) % 256
    z = 0.56 * y % (0.06 * x + 1) % 256
    return [z, z, z]

img = Image.fromarray(draw(2160, 4096))
img.save('10_4k2.png')


## Matthew Smarsch
## Cooper Union Electrical Engineering Master's Thesis
## Advisor: Professor Sam Keene
## Neural Network NNMF Implementation for the Cocktail Party Problem

import argparse
import sys, os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
import numpy as np
import datetime
import wave
import matplotlib.pyplot as plt
import pylab
import librosa
import librosa.display
import scipy
from scipy.io import wavfile
from scipy.fftpack import fft
from scipy import signal

# Alias for wavfile.read -> reads .wav to numpy array
#
# Returns: Numpy array representation of .wav file
def read_wav(filename):
    return librosa.load(filename, None)

def save_plot(values, filename):
    plt.figure()
    plt.plot(values)
    plt.savefig(filename, bbox_inches='tight')

class SentenceMixture:
    def __init__(self, speaker_model, sess, speaker, noisy_speaker = None, bases_hack = None):
        self._speaker_model = speaker_model
        self._rank = self._speaker_model._rank
        self._sess = sess
        self._speaker = speaker
        self._bases_hack = bases_hack
        self._noisy_speaker = noisy_speaker
        self._bases = self._speaker_model._bases
        if noisy_speaker is None:
            self._testing_sentence = self._speaker._testing_sentence
            self._clean_testing_sentence = self._testing_sentence
        else:
            self._clean_testing_sentence, self._testing_sentence = self.create_testing_sentence()
        self._testing_fs = self._speaker._testing_fs
        self.compute_testing_spectrogram()
        self._testing_data = self._testing_spectrogram[0]
        self._lambduh = 0.0001
        self._num_epochs = 10000
        self._learning_rate = 0.001
        #self.build_singular_model()
        self.build_double_model()

    def create_testing_sentence(self):
        min_length = min(np.shape(self._speaker._testing_sentence)[0], np.shape(self._noisy_speaker._testing_sentence)[0])
        clean_sentence = self._speaker._testing_sentence[:min_length]
        noise_sentence = signal.decimate(self._noisy_speaker._testing_sentence, 6)[2000:2000+min_length]
        noise_sentence = self._noisy_speaker._testing_sentence[:min_length]
        noisy_sentence = clean_sentence + noise_sentence
        #clean_sentence = self._speaker._testing_sentence
        #print(np.shape(clean_sentence))
        #print(np.shape(self._noisy_speaker._testing_sentence))
        #noisy_sentence = np.append(clean_sentence, self._noisy_speaker._testing_sentence)
        librosa.output.write_wav('noisy.wav', noisy_sentence, int(self._speaker._fs))
        return clean_sentence, noisy_sentence

    def compute_testing_spectrogram(self):
        fft_size = 512
        D = librosa.stft(self._testing_sentence, n_fft=fft_size, hop_length=int(fft_size/4))
        mag, phase = librosa.magphase(D)
        self._testing_spectrogram = (mag, phase)

    def build_singular_model(self):
        self._x = tf.placeholder(tf.float32)
        self._w = tf.placeholder(tf.float32)
        self._H_act = model_variable([self._rank, np.shape(self._testing_data)[1]], 'H_act_mix')
        tf.add_to_collection('l1', tf.reduce_sum(tf.abs(self._H_act)))
        self._reconstruction = tf.nn.softplus(tf.matmul(self._w, self._H_act))
        self._cost_function = tf.reduce_mean(tf.add(tf.subtract(tf.multiply(self._x, tf.subtract(tf.log(self._x), tf.log(self._reconstruction))), self._x), self._reconstruction))
        self._l1_penalty = tf.reduce_sum(tf.get_collection('l1'))
        self._loss = self._cost_function + self._lambduh * self._l1_penalty

    def build_double_model(self):
        self._x_1 = tf.placeholder(tf.float32)
        self._w_1 = tf.placeholder(tf.float32)
        self._H_act_1 = model_variable([self._rank, np.shape(self._testing_data)[1]], 'H_act_1')
        tf.add_to_collection('l1', tf.reduce_sum(tf.abs(self._H_act_1)))
        self._x_2 = tf.placeholder(tf.float32)
        self._w_2 = tf.placeholder(tf.float32)
        self._H_act_2 = model_variable([self._rank, np.shape(self._testing_data)[1]], 'H_act_2')
        tf.add_to_collection('l1', tf.reduce_sum(tf.abs(self._H_act_2)))
        self._reconstruction_1 = tf.nn.softplus(tf.matmul(self._w_1, self._H_act_1))
        self._reconstruction_2 = tf.nn.softplus(tf.matmul(self._w_2, self._H_act_2))
        self._reconstruction = self._reconstruction_1 + self._reconstruction_2
        self._cost_function = tf.reduce_mean(tf.add(tf.subtract(tf.multiply(self._x_1, tf.subtract(tf.log(self._x_1), tf.log(self._reconstruction))), self._x_1), self._reconstruction))
        self._l1_penalty = tf.reduce_sum(tf.get_collection('l1'))
        self._loss = self._cost_function + self._lambduh * self._l1_penalty

    def train_init(self):
        model_variables = tf.get_collection('model_variables')
        self._optim = tf.train.AdamOptimizer(self._learning_rate).minimize(self._loss, var_list=model_variables)
        self._sess.run(tf.global_variables_initializer())

    def train_single(self):
        loss_values = []
        self.train_init()
        print("BEGINNING MIXTURE TRAINING:")
        print(self._testing_data)
        for epoch in range(self._num_epochs):
            iter_loss = self.train_single_iter(self._testing_data)
            if epoch % 100 == 0:
                print('epoch: {}'.format(epoch))
                print('loss: {}'.format(iter_loss))
                loss_values.append(iter_loss)
        return loss_values

    def train_single_iter(self, data):
        loss, _ = self._sess.run([self._loss, self._optim], feed_dict={self._x: data, self._w: self._bases})
        return loss

    def single_test(self):
        return self._sess.run([self._reconstruction, self._H_act], feed_dict={self._x: self._testing_data, self._w: self._bases})

    def train_double(self):
        loss_values = []
        self.train_init()
        print("BEGINNING MIXTURE TRAINING:")
        print(self._testing_data)
        for epoch in range(self._num_epochs):
            iter_loss = self.train_double_iter(self._testing_data)
            if epoch % 100 == 0:
                print('epoch: {}'.format(epoch))
                print('loss: {}'.format(iter_loss))
                loss_values.append(iter_loss)
        return loss_values

    def train_double_iter(self, data):
        loss, _ = self._sess.run([self._loss, self._optim], feed_dict={self._x_1: data, self._w_1: self._bases_hack[0], self._x_2: data, self._w_2: self._bases_hack[1]})
        return loss

    def double_test(self):
        return self._sess.run([self._x_1, self._reconstruction_1, self._w_1, self._H_act_1, self._reconstruction_2, self._w_2, self._H_act_2], feed_dict={self._x_1: self._testing_data, self._w_1: self._bases_hack[0], self._x_2: self._testing_data, self._w_2: self._bases_hack[1]})

class SentenceSpeaker:
    def __init__(self, audio_directory, name):
        self._audio_directory = audio_directory
        self._name = name
        self._sentence_1, self._fs_1 = self.preprocess_sentence(audio_directory + 'Sentence_1.wav')
        self._sentence_2, self._fs_2 = self.preprocess_sentence(audio_directory + 'Sentence_2.wav')
        self._sentence_3, self._fs_3 = self.preprocess_sentence(audio_directory + 'Sentence_3.wav')
        self._sentence_4, self._fs_4 = self.preprocess_sentence(audio_directory + 'Sentence_4.wav')
        self._sentence_5, self._fs_5 = self.preprocess_sentence(audio_directory + 'Sentence_5.wav')
        self._training_sentences = [self._sentence_1, self._sentence_2, self._sentence_3, self._sentence_4]
        self._testing_sentence = self._sentence_5
        self._big_training_sentence =  self.get_big_training_sentence()
        self._fs = self._fs_1
        self._training_fs = [self._fs_1, self._fs_2, self._fs_3, self._fs_4]
        self._testing_fs = self._fs_5
        self._mixture_sentence = self._sentence_5

    def get_big_training_sentence(self):
        reshaped_sentences = []
        for training_sentence in self._training_sentences:
            reshaped_sentences.append(np.reshape(training_sentence, (-1,1)))
        return np.reshape(scipy.vstack(reshaped_sentences), (-1,))

    def preprocess_sentence(self, sentence_file):
        sentence, fs = read_wav(sentence_file)
        return sentence, fs

    def compute_training_spectrograms(self):
        fft_size = 512
        self._training_spectrograms = []
        for fs, sentence in zip(self._training_fs, self._training_sentences):
            D = librosa.stft(sentence, n_fft=fft_size, hop_length=int(fft_size/4))
            mag, phase = librosa.magphase(D)
            self._training_spectrograms.append((mag, phase))

    def compute_big_training_spectrogram(self):
        fft_size = 512
        D = librosa.stft(self._big_training_sentence, n_fft=fft_size, hop_length=int(fft_size/4))
        mag, phase = librosa.magphase(D)
        self._big_training_spectrogram = (mag, phase)

    def plot_spectrograms(self):
        for spectr in self._training_spectrograms:
            plt.figure(figsize=(12, 8))
            librosa.display.specshow(spectr[0], y_axis='linear', x_axis='time')
            plt.colorbar(format='%+2.0f')
            plt.title('Linear-frequency power spectrogram')
            plt.tight_layout()
            plt.show()

def reconstruct_wave(magnitude, phase):
    reconstr = librosa.istft(magnitude * phase)
    return reconstr

def reconstruct_target_wave(noisy_input, reconstruction_targ, reconstruction_noise, phase):
    reconstr = librosa.istft((reconstruction_targ + reconstruction_noise) * phase)
    reconstr1 = librosa.istft((reconstruction_targ) * phase)
    reconstr2 = librosa.istft((reconstruction_noise) * phase)
    #reconstr1 = librosa.istft(np.multiply(np.multiply(np.divide(reconstruction_targ, np.add(reconstruction_noise, reconstruction_targ)), noisy_input), np.exp(1j * phase)))
    #reconstr2 = librosa.istft(np.multiply(np.multiply(np.divide(reconstruction_noise, np.add(reconstruction_noise, reconstruction_targ)), noisy_input), np.exp(1j * phase)))
    return reconstr, reconstr1, reconstr2

def model_variable(shape, name):
    variable = tf.get_variable(name=name,
                                dtype=tf.float32,
                                initializer=tf.random_uniform(shape, -0.1, 0.1))
    tf.add_to_collection('model_variables', variable)
    return variable

class SpeechModel:

    def __init__(self, sess, speaker, args):
        self._sess = sess
        self._speaker = speaker
        self._activation = args.activation
        self._rank = args.rank
        self._learning_rate = args.alpha
        self._num_epochs = args.epochs
        self._lambduh = args.lambduh
        self._training_data = speaker._big_training_spectrogram[0]
        self.build_model()

    def build_model(self):
        self._x = tf.placeholder(tf.float32)
        W_enc = model_variable([self._rank, np.shape(self._training_data)[0]], 'W_enc')
        if self._activation == 'SOFTPLUS':
            self._H_act = tf.nn.softplus(tf.matmul(W_enc, self._x))
        elif self._activation == 'RELU':
            self._H_act = tf.nn.relu(tf.matmul(W_enc, self._x))
        tf.add_to_collection('l1', tf.reduce_sum(tf.abs(self._H_act)))
        self._W_dec = model_variable([np.shape(self._training_data)[0], self._rank], 'W_dec')
        if self._activation == 'SOFTPLUS':
            self._reconstruction = tf.nn.softplus(tf.matmul(self._W_dec, self._H_act))
        elif self._activation == 'RELU':
            self._reconstruction = tf.nn.relu(tf.matmul(self._W_dec, self._H_act))
        self._cost_function = tf.reduce_mean(tf.add(tf.subtract(tf.multiply(self._x, tf.subtract(tf.log(self._x), tf.log(self._reconstruction))), self._x), self._reconstruction))
        self._l1_penalty = tf.reduce_sum(tf.get_collection('l1'))
        self._loss = self._cost_function + self._lambduh * self._l1_penalty

    def train_init(self):
        model_variables = tf.get_collection('model_variables')
        self._optim = tf.train.AdamOptimizer(self._learning_rate).minimize(self._loss, var_list=model_variables)
        self._sess.run(tf.global_variables_initializer())

    def train(self, log_file):
        loss_values = []
        self.train_init()
        print("BEGINNING TRAINING:", file=log_file)
        print(self._training_data, file=log_file)
        for epoch in range(self._num_epochs):
            iter_loss = self.train_iter(self._training_data)
            if epoch % 100 == 0:
                print('epoch: {}'.format(epoch), file=log_file)
                print('loss: {}'.format(iter_loss), file=log_file)
                loss_values.append(iter_loss)
        return loss_values

    def train_iter(self, data):
        loss, _ = self._sess.run([self._loss, self._optim], feed_dict={self._x: data})
        return loss

    def test(self):
        return self._sess.run([self._reconstruction, self._W_dec, self._H_act], feed_dict={self._x: self._training_data})

def main():

    '''

    ENVIRONMENT SETUP

    '''

    parser = argparse.ArgumentParser()
    parser.add_argument('-a', '--alpha', dest='alpha', type=float, default=0.01, help='the learning rate for backpropagation')
    parser.add_argument('-act', '--activation', dest='activation', type=str, choices=['RELU', 'SOFTPLUS'], default='SOFTPLUS', help='activation function for the speech model')
    parser.add_argument('-c', '--clean', dest='clean_model', type=str, choices=['Sentences', 'Book', 'Piano'], default='Sentences', help='clean speech type that we are trying to model')
    parser.add_argument('-e', '--epochs', dest='epochs', type=int, default=50000, help='number of passes through the training set')
    parser.add_argument('-l', '--lambduh', dest='lambduh', type=float, default=0, help='L1 regularization coefficient')
    parser.add_argument('-r', '--rank', dest='rank', type=int, default=20, help='number of spectral components in the W (bases) matrix')
    args = parser.parse_args()

    simulations_dir = '/Users/MSmarsch/Documents/Thesis/Simulations/' + datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + '/'
    os.mkdir(simulations_dir)

    '''

    AUDIO PREPROCESSING

    '''

    if args.clean_model == 'Sentences':
        ## Load Sentences in
        sentences_dir = '/Users/MSmarsch/Documents/Thesis/Audio/Clean_Speech/Sentences/'
        male_speaker_1 = SentenceSpeaker(sentences_dir + 'Male_1/', 'Male_1')
        male_speaker_2 = SentenceSpeaker(sentences_dir + 'Male_2/', 'Male_2')
        male_speaker_3 = SentenceSpeaker(sentences_dir + 'Male_3/', 'Male_3')
        female_speaker_1 = SentenceSpeaker(sentences_dir + 'Female_1/', 'Female_1')
        female_speaker_2 = SentenceSpeaker(sentences_dir + 'Female_2/', 'Female_2')
        noisy_speaker = female_speaker_2
        #speakers = [male_speaker_1, male_speaker_2, male_speaker_3, female_speaker_1, female_speaker_2]
        speakers = [male_speaker_1, female_speaker_2]
        bases_hack = []

        ## Compute Training Spectrogram by taking 512 pt. DFT with hann window and 25% overlap
        for speaker in speakers:
            speaker_dir = simulations_dir + speaker._name + '/'
            os.mkdir(speaker_dir)
            audio_dir = speaker_dir + 'Audio/'
            os.mkdir(audio_dir)
            plot_dir = speaker_dir + 'Plots/'
            os.mkdir(plot_dir)
            model_dir = speaker_dir + 'Model/'
            os.mkdir(model_dir)

            speaker.compute_big_training_spectrogram()
            reconstr = reconstruct_wave(speaker._big_training_spectrogram[0], speaker._big_training_spectrogram[1])
            librosa.output.write_wav(audio_dir + speaker._name + '_Input.wav', reconstr, int(speaker._fs))

            '''

            MODEL CONSTRUCTION

            '''
            with tf.Session() as sess:
                speech_model = SpeechModel(sess, speaker, args)
                log_file = open(speaker_dir + 'output.log', 'w+')
                loss_values = speech_model.train(log_file)
                prediction, bases, activations = speech_model.test()
                bases_hack.append(bases)
                reconstr = reconstruct_wave(prediction, speaker._big_training_spectrogram[1])
                librosa.output.write_wav(audio_dir + speaker._name + '_Reconstruction.wav', reconstr, int(speaker._fs))
                loss_filename = plot_dir + 'Loss.png'
                save_plot(loss_values, loss_filename)
                bases_filename = plot_dir + 'Bases.png'
                save_plot(bases, bases_filename)
                activations_filename = plot_dir + 'Activations.png'
                save_plot(activations, activations_filename)
                base_matrix_filename = model_dir + 'Bases.npy'
                np.save(base_matrix_filename, bases)
                speaker._bases = bases
                speech_model._bases = speaker._bases
            tf.reset_default_graph()

        for speaker in speakers:
            with tf.Session() as sess:
                sentence_mixture = SentenceMixture(speech_model, sess, speaker, noisy_speaker, bases_hack)
                speaker._speech_model = speech_model
                speaker._sentence_mixture = sentence_mixture
                loss_values = sentence_mixture.train_double()
                noisy_input, reconstruction_targ, bases_targ, activations_targ, reconstruction_noise, bases_noise, activations_noise = sentence_mixture.double_test()
                #reconstr = reconstruct_wave(reconstruction, sentence_mixture._testing_spectrogram[1])
                reconstr, reconstr1, reconstr2 = reconstruct_target_wave(noisy_input, reconstruction_targ, reconstruction_noise, sentence_mixture._testing_spectrogram[1])
                librosa.output.write_wav('Mixture_Reconstruction.wav', reconstr, int(speaker._fs))
                librosa.output.write_wav('Mixture_Reconstruction_1.wav', reconstr1, int(speaker._fs))
                librosa.output.write_wav('Mixture_Reconstruction_2.wav', reconstr2, int(speaker._fs))
                for variable in tf.trainable_variables():
                    print(variable)
                print(bases_targ)
                print('-------------------------------------')
                print(speaker._bases)
                print('-------------------------------------')
                print(bases_noise)
                print('-------------------------------------')
                print(noisy_speaker._bases)
                if np.array_equal(noisy_speaker._bases, bases_noise):
                    print('noise is good')
                if np.array_equal(speaker._bases, bases_targ):
                    print('targ is good')
                sys.exit()
            tf.reset_default_graph()

if __name__ == '__main__':
    main()


from __future__ import print_function
import numpy as np
import tqdm
import json
import copy
from multiprocessing import Pool
from math import sin, tan, cos
from simple_bicycle_ekf import SimpleBicycleEKF
from matplotlib import pyplot as plt
from noiseestimation.sensor import Sensor
from noiseestimation.correlator import Correlator
from noiseestimation.estimation import (
    estimate_noise_approx,
    estimate_noise_mehra,
    estimate_noise_extended
)

# parameters
runs = 100
skip_initial = 50
num_samples = 250
used_taps = (num_samples - skip_initial) / 2
dt = 0.1
Q = 0.02
var_vel = Q
var_steer = Q
measurement_var = 0.05
R_proto = np.array([[1, 0],
                    [0, 1]])
filter_misestimation_factor = 1
turning_threshold_angle = 0.001
wheelbase = 1

# move simulated robot
def f(x, u):
    heading = x[2, 0]
    vel = u[0, 0]
    steering_angle = u[1, 0]
    dist = vel * dt

    # check whether robot is turning
    if abs(steering_angle) > turning_threshold_angle:
        beta = (dist / wheelbase) * tan(steering_angle)
        r = wheelbase / tan(steering_angle)
        dx = np.array([[-r * sin(heading) + r * sin(heading + beta)],
                       [r * cos(heading) - r * cos(heading + beta)],
                       [beta]])
    else:
        dx = np.array([[dist * cos(heading)],
                       [dist * sin(heading)],
                       [0]])

    return x + dx

def matrix_error(estimate, truth):
    return np.sqrt(np.sum(np.square(truth - estimate)))

def setup():
    # set up sensor simulator
    H = np.array([[1, 0, 0],
                  [0, 1, 0]])

    def h(x):
        return np.dot(H, x)

    x0 = np.array([[0],
                   [-1],
                   [0]])
    sim = Sensor(x0, f, h)

    # set up kalman filter
    tracker = SimpleBicycleEKF(dt, wheelbase, var_vel, var_steer)
    tracker.R = R_proto * measurement_var * filter_misestimation_factor
    tracker.x = np.array([[0, 0, 0]]).T
    tracker.P = np.eye(3) * 500

    return sim, tracker

def filtering(sim, tracker):
    # perform sensor simulation and filtering
    R = R_proto * measurement_var
    readings, truths, filtered, residuals, Ps, Fs, Ks = [], [], [], [], [], [], []
    cmds = [np.array([[0.6],
                      [0.23]])] * (num_samples / 4)
    cmds.extend([np.array([[2],
                           [-0.20]])] * (num_samples / 2))
    cmds.extend([np.array([[1],
                           [0.2]])] * (num_samples / 4))
    for cmd in cmds:
        sim.step(cmd)
        reading = sim.read(R)
        tracker.predict(cmd)
        tracker.update(reading)
        readings.append(reading)
        truths.append(sim.x)
        filtered.append(copy.copy(tracker.x))
        Ps.append(tracker.P)
        residuals.append(tracker.y)
        Fs.append(tracker.F)
        Ks.append(tracker.K)

    readings = np.asarray(readings)
    truths = np.asarray(truths)
    filtered = np.asarray(filtered)
    Ps = np.asarray(Ps)
    residuals = np.asarray(residuals)
    Fs = np.asarray(Fs)
    Ks = np.asarray(Ks)
    return readings, truths, filtered, residuals, Ps, Fs, Ks

def perform_estimation(residuals, tracker, H, F_arr, K_arr):
    cor = Correlator(residuals)
    correlation = cor.autocorrelation(used_taps)
    R_extended = estimate_noise_extended(
        correlation, K_arr, F_arr, H)
    R_mehra = estimate_noise_mehra(
        correlation, K_arr[-1], F_arr[-1], H)
    R_approx = estimate_noise_approx(
        correlation[0], H, tracker.P)
    truth = R_proto * measurement_var
    error_extended = matrix_error(R_extended, truth)
    error_mehra = matrix_error(R_mehra, truth)
    error_approx = matrix_error(R_approx, truth)
    return (error_extended, error_mehra, error_approx)

def plot_results(readings, filtered, truths, Ps):
    axarr = [plt.subplot()]

    axarr[0].plot(
        readings[:, 0],
        readings[:, 1],
        'o', label="Readings"
    )
    axarr[0].plot(
        truths[:, 0],
        truths[:, 1],
        'k', linewidth=3, label="Truth")
    axarr[0].plot(
        filtered[:, 0],
        filtered[:, 1],
        linewidth=3, label="Filter")
    axarr[0].legend(loc="lower right")
    axarr[0].set_title("Kalman filtering of position")
    axarr[0].set_xlabel("x (m)")
    axarr[0].set_ylabel("y (m)")
    axarr[0].axis('scaled')

    # axarr[0].plot(Ps[:, 0, 0], 'g', label="X state variance")
    # axarr[0].plot(Ps[:, 1, 1], 'r', label="Y state variance")
    # axarr[0].legend(loc="upper right")
    # axarr[0].set_ylim((0, 0.003))
    # axarr[0].set_ylabel("$\sigma^2$ ($m^2$)")
    # axarr[0].set_xlabel("Sample")
    # axarr[0].set_title("State covariance")

    plt.show()

def run_tracker(dummy):
    sim, tracker = setup()
    readings, truths, filtered, residuals, Ps, Fs, Ks = filtering(sim, tracker)
    errors = perform_estimation(
        residuals[skip_initial:], tracker, tracker.H,
        Fs[skip_initial:], Ks[skip_initial:])
    # plot_results(readings, filtered, truths, Ps)
    state_error = np.sqrt(np.sum(np.square(truths-filtered)[50:, [0, 1], 0]))
    if state_error > 1.5:
        print("WARNING: Outlier (%.6f)" % state_error)
    return errors

if __name__ == "__main__":
    pool = Pool(8)
    args = [0] * runs
    pbar = tqdm.tqdm(total=runs)
    errors_arr = []
    for errors in pool.imap_unordered(run_tracker, args, chunksize=2):
        pbar.update()
        errors_arr.append(errors)
    pbar.close()
    pool.close()
    pool.join()

    errors_arr = np.asarray(errors_arr)
    avg_errors = np.sum(errors_arr, axis=0) / float(runs)
    matrix_size = matrix_error(R_proto * measurement_var, 0)
    rel_errors = avg_errors / matrix_size
    print("Max. error: %.6f" % np.max(errors_arr))
    # ddof = 1 assures an unbiased estimate
    # variances = np.var(errors_arr, axis=0, ddof=1)
    print("-" * 20)
    print("Extended estimation:")
    print("\tAverage Error: %.6f" % avg_errors[0])
    print("\tRelative Error: %.6f" % rel_errors[0])
    print("Mehra estimation:")
    print("\tAverage Error: %.6f" % avg_errors[1])
    print("\tRelative Error: %.6f" % rel_errors[1])
    print("Approximate estimation:")
    print("\tAverage Error: %.6f" % avg_errors[2])
    print("\tRelative Error: %.6f" % rel_errors[2])

    res = {}
    res["types"] = ["Extended", "Mehra", "Mohamed"]
    res["abs_err"] = avg_errors.tolist()
    res["rel_err"] = rel_errors.tolist()
    with open("results.json", "w") as outfile:
        json.dump(res, outfile)


from PIL import Image
from PIL import ExifTags
import sys

def midPoint(x1,y1,x2,y2):
    x = (x1/2) - (x2/2)
    y = (y1/2) - (y2/2)

    return x,y

def main(f1name,f2name,f):
    im = Image.open(f1name)
    i_width,i_height = im.size
    watermark = Image.open(f2name)
    w_width,w_height = watermark.size
    x_start,y_start = midPoint(i_width,i_height, w_width,w_height)
    for x in range(w_width):
        for y in range(w_height):
            x1 = int(x+x_start)
            y1 = int(y+y_start)
            p1 = im.getpixel( (x1,y1) )
            p2 = watermark.getpixel( (x,y) )
            if(p2[3] == 0):
                continue
            r = (p1[0] & 0b11111110) | (p2[0] >> 7)
            g = (p1[1] & 0b11111110) | (p2[1] >> 7)
            b = (p1[2] & 0b11111110) | (p2[2] >> 7)
            im.putpixel((x1,y1), (r, g, b) )

    im.save(f1name+"-wm.jpg")

    for x in range(i_width):
        for y in range(i_height):
            p1 = im.getpixel( (x,y) )
            r = (p1[0] << 7) & 255
            g = (p1[1] << 7) & 255
            b = (p1[2] << 7) & 255
            im.putpixel((x,y), (r, g, b) )

    im.save(f1name+"-recuperated.jpg")

main(sys.argv[1],sys.argv[2],float(sys.argv[3]))


from collections import defaultdict

def solution_bf(N, K, strings):
    pre = {}
    res = 0

    for s in strings:
        for i in range(len(s), 0, -1):
            if s[:i] in pre:
                pre[s[:i]] += 1
            else:
                pre[s[:i]] = 1

    for p, c in pre.items():
        res += c // K

    return res

class TrieNode:

    def __init__(self):
        self.children = defaultdict(TrieNode)
        self.count = 0
        self.is_word = False

class Trie:

    def __init__(self):
        self.root = TrieNode()

    def insert(self, word):
        node = self.root
        for char in word:
            new_node = node.children[char]
            new_node.count += 1
            node = new_node

        node.is_word = True

    def get_node(self, word):
        node = self.root
        for char in word:
            node = node.children[char]
        return node

    def search(self, word):
        node = self.get_node(word)
        return node != None and node.is_word

    def print_trie(self):
        self.print_trie_helper(self.root)

    def print_trie_helper(self, node):
        for k, n in node.children.items():
            print(k, n.count)
            self.print_trie_helper(n)

def solution_trie(N, K, strings):
    pre = Trie()

    for s in strings:
        pre.insert(s)

    queue = [pre.root]
    total = 0
    while queue:
        node = queue.pop(0)
        for k, n in node.children.items():
            total += n.count // K
            queue.append(n)

    return total

solution = solution_trie

tc = int(input())
for i in range(1, tc + 1):
    N, K = input().split()
    strings = []
    for j in range(int(N)):
        strings.append(input())

    out = solution(int(N), int(K), strings)
    print("Case #{}: {}".format(i, out))


# fig_ggn_dep_gbar_corr_kc_spikes.py ---
# Author: Subhasis  Ray
# Created: Thu Dec 27 17:17:29 2018 (-0500)
# Last-Updated: Fri Jan 11 17:23:30 2019 (-0500)
#           By: Subhasis  Ray
# Version: $Id$

# Code:

"""Attempt to correlate GGN depolarization and GGN-KC gbar to KC spike stats"""

from __future__ import print_function
import sys
if sys.platform == 'win32':
    sys.path.append('D:/subhasis_ggn/model/analysis')
else:
    sys.path += ['/home/rays3/projects/ggn/analysis',
                 '/home/rays3/projects/ggn/morphutils',
                 '/home/rays3/projects/ggn/nrn']
import os
import h5py as h5
import numpy as np
import random
from matplotlib import pyplot as plt
import yaml
import pint
from collections import defaultdict
import pandas as pd
import network_data_analysis as nda
from matplotlib.backends.backend_pdf import PdfPages
import pn_kc_ggn_plot_mpl as myplot
import neurograph as ng
import timeit
from sklearn import cluster, preprocessing
plt.style.use('classic')

_ur = pint.UnitRegistry()
Q_ = _ur.Quantity
# datadir = 'D:\\biowulf_stage\\olfactory_network\\'
datadir = '/data/rays3/ggn/olfactory_network/'

def get_dv_kc_data(fname):
    """Create a dataset where each row contains a KC, its presynaptic GGN
    section, maximum conductance from GGN, peak depolarization of that
    section and the number of spikes generated by that KC"""
    with h5.File(fname, 'r') as fd:
        print(jid, len(nda.get_spiking_kcs(fd)))
        print(yaml.dump(nda.load_config(fd), default_style=''))
        ggn_kc_gbar = pd.DataFrame(fd[nda.ggn_kc_syn_path]['pre', 'post', 'gbar'][:, 0])
        ggn_peak_vm = fd['/data/uniform/ggn_output/GGN_output_Vm'].value.max(axis=1)
        ggn_sec = fd['/data/uniform/ggn_output/GGN_output_Vm'].dims[0]['source']
        ggn_sec_vm = pd.DataFrame(data={'sec': ggn_sec, 'vm': ggn_peak_vm})
        pn_kc_gmax = pd.DataFrame(fd[nda.pn_kc_syn_path]['post', 'gmax'][:, 0])
        pn_kc_gmax_by_kc = pn_kc_gmax.groupby('post').sum().reset_index()
        kc_sc = {kc_st.attrs['source']: len(kc_st) for kc_st in fd[nda.kc_st_path].values()}
    df_kc_sc = pd.DataFrame(data={'kc': list(kc_sc.keys()), 'spike_count': list(kc_sc.values())})
    combined = pd.merge(ggn_kc_gbar, df_kc_sc, left_on='post', right_on='kc')
    combined = pd.merge(combined, ggn_sec_vm, left_on='pre', right_on='sec')
    combined.drop(columns=['pre', 'post'], inplace=True)
    return combined

def hist_2d_spiking_kc_data(data, vm_bins=5, gbar_bins=5):
    """Create 2D histogram of fraction of KCs spiking based with specified number of Vm
    and gbar bins."""
    kc_frac = defaultdict(dict)
    spike_count_frac =  defaultdict(dict)
    vm_cut, vm_bins = pd.cut(data.vm, bins=vm_bins, retbins=True)
    gbar_cut, gbar_bins = pd.cut(data.gbar, bins=gbar_bins, retbins=True)
    for vm_int in vm_cut.cat.categories:
        for gbar_int in gbar_cut.cat.categories:
            kc_frac[vm_int][gbar_int] =  0.0
            spike_count_frac[vm_int][gbar_int] =  0.0
    print(len(list(kc_frac.keys())))
    for key, grp in data.groupby([vm_cut, gbar_cut]):
        if key[0] not in kc_frac:
            print('!!!!', vm_cut)
        spiking = grp.spike_count.nonzero()[0]
        n_spiking = spiking.shape[0]
        spike_count = grp.spike_count.sum()
        frac_spiking = n_spiking * 1.0 / len(grp)
        kc_frac[key[0]][key[1]] = frac_spiking
        print(key, n_spiking)
        spike_count_per_cell = spike_count * 1.0 / len(grp)
        spike_count_frac[key[0]][key[1]] = spike_count_per_cell
    return kc_frac, spike_count_frac

def plot_hist(ddict):
    bins_a =  sorted(list(ddict.keys()))
    bins_b =  None
    z =  None
    for k_a in bins_a:
        if bins_b is None:
            bins_b =  sorted(list(ddict[k_a].keys()))
            z =  np.zeros((len(bins_a),  len(bins_b)))
            break
    for ia, k_a in enumerate(bins_a):
        for ib, k_b in enumerate(bins_b):
            print(k_a, k_b, ddict[k_a][k_b])
            z[ia,  ib] = ddict[k_a][k_b]
    print('Here')
    x =  [ba.left for ba in bins_a] + [bins_a[-1].right]
    y =  [bb.left for bb in bins_b] + [bins_b[-1].right]
    X, Y = np.meshgrid(x, y)
    fig, ax =  plt.subplots()
    ax.pcolormesh(X, Y, z.T)
    return fig, ax

jids = [
    '16377231',
    '16377233',
    '16377234',
    '16377237']

# for jid in jids:
#     fname = nda.find_h5_file(jid, datadir)
    # with h5.File(fname, 'r') as fd:
    #     print('jid: {}, spiking kcs: {}'.format(jid, len(nda.get_spiking_kcs(fd))))
    #     print(yaml.dump(nda.load_config(fd), default_style='', default_flow_style=''))
#     myplot.plot_spike_rasters(fname)
#     myplot.plot_kc_spike_count_hist(fname)
# plt.show()

# |      jid | ggn-kc gmax | pn-kc gmax | spiking kcs |
# | 16377231 |         0.7 |        3.7 |       10525 |
# | 16377233 |         0.5 |        3.7 |       17708 |
# | 16377234 |         0.9 |        4.0 |       10536 |
# | 16377237 |         0.9 |        4.5 |       37060 |

## Taking 16377231 as a sample
# jid = '16377231'   # Had about 7031 KCs spiking
# fname =  nda.find_h5_file(jid, datadir)
# data =  get_dv_kc_data(fname)

# kc_frac, spike_frac = hist_2d_spiking_kc_data(data)
# for ka, va in kc_frac.items():
#     for kb,  vb in va.items():
#         print(ka, kb, vb)
# plot_hist(kc_frac)

# * all these turned out to spike only at the lowest GGN gbar
# for jid in jids:
#     fname = nda.find_h5_file(jid, datadir)
#     data = get_dv_kc_data(fname)
#     kc_frac, spike_frac = hist_2d_spiking_kc_data(data,  10, 10)
#     fig, ax = plot_hist(kc_frac)
#     fig2, ax2 = plot_hist(spike_frac)
#     # plt.show()

# plt.show()

spike_info_list =  []
fig, ax = plt.subplots(nrows=2, sharex='all')
for jid in jids:
    fname = nda.find_h5_file(jid, datadir)
    data = get_dv_kc_data(fname)
    vm_bins = pd.cut(data.vm, bins=10)
    # gbar_bins = pd.cut(data.gbar, bins=10)
    spike_info = []
    for key, grp in data.groupby(vm_bins):
        spiking = grp.spike_count.nonzero()[0]
        n_spiking = spiking.shape[0]
        spike_count = grp.spike_count.sum()
        frac_spiking = n_spiking * 1.0 / len(grp)
        spike_count_per_cell = spike_count * 1.0 / len(grp)
        spike_info.append({'jid': jid, 'vm_interval': key, 'frac_spiking': frac_spiking/key.length, 'spike_count_per_cell': spike_count_per_cell/key.length})
        # print(key, key.length, len(grp), n_spiking, frac_spiking, spike_count, spike_count_per_cell)
    spike_info_list.append(spike_info)
    spike_info =  pd.DataFrame(spike_info)
    ax[0].plot([vint.mid + 51.0 for vint in spike_info.vm_interval], spike_info.frac_spiking, 'o-', label='spiking KC fraction')
    # plt.legend(loc=3)
    # plt.subplot(212)
    ax[1].plot([vint.mid + 51.0 for vint in spike_info.vm_interval], spike_info.spike_count_per_cell, '^-', label='spikes per KC')

ax[1].set_xlabel('GGN dVm')
ax[0].set_ylabel('Fraction of KCs spiking')
ax[0].set_yticks([0.15, 0.25, 0.35, 0.45, 0.55])
ax[1].set_ylabel('# of spikes per KC')
ax[1].set_yticks([0.2, 1.0, 2.0])

for axis in ax:
    [spine.set_visible(False) for spine in axis.spines.values()]
    axis.tick_params(top=False, right= False)
    # plt.legend(loc=9)

fig.frameon = False
fig.savefig('ggn_peak_kc_spiking_relation.svg', transparent=True)
plt.show()

fig, ax = plt.subplots(nrows=2, sharex='all')
for si in spike_info_list:
    spike_info = pd.DataFrame(si)
    ax[0].plot([vint.mid + 51.0 for vint in spike_info.vm_interval], spike_info.frac_spiking, 'o-', label=spike_info.jid.iloc[0])
    # plt.legend(loc=3)
    # plt.subplot(212)
    ax[1].plot([vint.mid + 51.0 for vint in spike_info.vm_interval], spike_info.spike_count_per_cell, 'o-', label=spike_info.jid.iloc[0])

ax[1].legend()
ax[1].set_xlabel('GGN depolarization (mV)')
ax[0].set_ylabel('Fraction of KCs spiking')
ax[0].set_yticks([0.15, 0.25, 0.35, 0.45, 0.55])
ax[1].set_ylabel('# of spikes per KC')
ax[1].set_yticks([0.2, 1.0, 2.0])

for axis in ax:
    [spine.set_visible(False) for spine in axis.spines.values()]
    axis.tick_params(top=False, right= False)
    # plt.legend(loc=9)

fig.frameon = False
fig.savefig('ggn_peak_kc_spiking_relation.svg', transparent=True)
plt.show()

#
# fig_ggn_dep_gbar_corr_kc_spikes.py ends here


import time
import requests
import json

Token = "your token here"

url = "https://lambda-treasure-hunt.herokuapp.com/api"

headers = {'Authorization': f'Token {Token}'}

def init():
    r = requests.get(
        f'{url}/adv/init/', headers=headers)

    data = r.json()
    print(data)
    print(data["items"])
    if len(data['errors']) > 0:
        print(data)
        return False
    return data

def move(payload):
    r_move = requests.post(
        f'{url}/adv/move/', data=json.dumps(payload), headers=headers)
    data = r_move.json()
    # print('data inside the move', data)
    if len(data['errors']) > 0:
        print('move error', data)
        return False
    return data

def get_item(item):
    r = requests.post(f'{url}/adv/take',
                      data=json.dumps(item), headers=headers)
    data = r.json()
    if len(data['errors']) > 0:
        print('get_item error', data['errors'])
        return False
    return data


# -*- coding: utf-8 -*-
"""
Created on Sat May 20 14:11:03 2017

@author: Christine
"""
# Imports
import matplotlib.pyplot as plt
import numpy as np
import xml.etree.ElementTree as etree

# Functions
def xrdxmlplot():
#     print('Assume the directory path is \Data\XRD. \
#           Be sure to include the .xml suffix')
     #filename = input('Enter filename: ')
     #print(filename)

    filename = '20170615 - ThEA2MA59Pb60I181.xml'
    tree = etree.parse(filename)
    root = tree.getroot()

    # Create empty y array
    y = []
    data = root[0][2][6].text

    # Write data to the y array
    for datapoint in data.split():
         y.append(float(datapoint))

     # Create x array spanning the 2Theta range
    x = np.arange(8,50.035,0.035,dtype = None)

     # Create a plot with plot and axes labels
    plt.plot(x, y)
    plt.title('XRD Spectrum')
    plt.ylabel('Intensity (counts)')
    plt.xlabel('2θ (deg)')

#     print(y)

#     print(root[0][2][6].tag)
#     print(root[0][2][6].text)

#     for datapoint in data.split():
#          print(datapoint)

     #for data in root.findall('.//Pattern/ScanTrace/Data', namespaces = None):
      #    print (data)

          #for num in ThEAData.findall('.Pattern/ScanTrace/Data'):
          #     y.append(num)

         # print(y)

     #print (x)
     #y = xmlAttr.getContent,ctxt.xpathEval('//MDI date/diagram/point/@totaldensity')

# Run program
xrdxmlplot()


from base.dataset import DatasetManager

#from llorma_p.trainer import main as llorma_parallel_train
from llorma_g.trainer import main as llorma_global_train

import pandas as pd
import numpy as np
import sklearn as sk
from sklearn import metrics
import csv

# 1000x1000 200x200 mean=0 error=10
import argparse
import sys

parser = argparse.ArgumentParser()
parser.add_argument("--testType", type=str)
parser.add_argument("--size", type=int)
parser.add_argument("--error", type=int)
parser.add_argument("--mean", type=int)
runParams = parser.parse_args()

size = runParams.size
error = runParams.error
mean = runParams.mean
testType = runParams.testType

if __name__ == '__main__':
    # kind = DatasetManager.KIND_MOVIELENS_100K
    # kind = DatasetManager.KIND_MOVIELENS_1M
    # kind = DatasetManager.KIND_MOVIELENS_10M
    # kind = DatasetManager.KIND_MOVIELENS_20M
    #kind = DatasetManager.KIND_PTMATRIX_1000

    #llorma_parallel_train(kind)
    #llorma_global_train(kind)

#MEAN

    i = 1 #for i in range(2):#size
    j = 1 #for j in range(2):#error
    # print(size[1])
    scores = []
    accuracies = []
    k = 1 #for k in range(31):#mean
    # make Answer

    Pat_N = size
    Mat_N = 100
    print("Creating Answers")

    AnswerName = 'Answer'+str(testType)+str(size)+'-'+str(error)+'-'+str(mean)+'.csv'

    print("Answer CREATED!")

    #kind = 'DatasetManager.KIND_PTMATRIX_100_'+str(testType)+str(size)+'_'+str(error)+'_'+str(mean)
    #kind = 'DatasetManager.KIND_PTMATRIX_100' + '_' + str(testType) + str(size) + '_' + str(error) + '_' + str(mean)
    kind = 'ptmatrix-' + str(testType) +'-'+ str(size) +'-'+ str(error) +'-'+ str(mean)
    print(kind)

    #PQ Analyzer
    pNAME = './llorma_g/ptmatrix-basetimes-' + str(size)+'-'+str(error)+'-'+str(mean)+'-p.npy'
    qNAME = './llorma_g/ptmatrix-basetimes-' + str(size) + '-' + str(error) + '-' + str(mean) + '-q.npy'
    p = np.load(pNAME)
    q = np.load(qNAME)

    p = np.concatenate(p)
    q = np.concatenate(q)
    print(p.shape)
    print(p)
    print(q)

    aa = np.zeros((p.shape[0], 1))
    bb = np.zeros((q.shape[0], 1))

    print(type(q))

    sortdP = -np.sort(-p)
    sortdQ = -np.sort(-q)

    print(sortdP)

    critP = sortdP[Pat_N]
    critQ = sortdQ[Pat_N]

    p[p <= critP] = 0
    p[p > critP] = 1

    q[q <= critQ] = 0
    q[q > critQ] = 1

    aa = p.copy()
    bb = q.copy()

    cc = []

    print(pd.value_counts(aa.reshape(Mat_N,)))
    print(pd.value_counts(bb.reshape(Mat_N,)))
    cc = np.matmul(np.reshape(aa, (Mat_N, 1)), np.reshape(bb, (Mat_N, 1)).T)

    AnswerData = pd.read_csv(AnswerName, dtype=int, header=None)
    AD = AnswerData.values
    score = np.zeros(1)
    accuracy = np.zeros(1)
    confuMat = np.zeros(1)
    #print(pd.value_counts(AD.astype(int).reshape(10000, 1)))
    abc = np.array(cc)
    bcd = np.array(AD)
    Mat_S = Mat_N * Mat_N
    print('cc', pd.value_counts(abc.reshape(Mat_S,)))
    print('Answer：', pd.value_counts(bcd.reshape(Mat_S,)))
    # print(cc[0].astype(int))
    # print(AD)
    score = sk.metrics.f1_score(AD.reshape(Mat_S, 1), cc.astype(int).reshape(Mat_S, 1))
    accuracy = sk.metrics.accuracy_score(AD.reshape(Mat_S, 1), cc.astype(int).reshape(Mat_S, 1))
    confuMat = sk.metrics.confusion_matrix(AD.reshape(Mat_S, 1), cc.astype(int).reshape(Mat_S, 1),
                                               labels=[1, 0], normalize='true')

    print(confuMat)
    print("Done Calculation!")

    csv1 = str(testType) + str(size) + '-' + str(error) + 'TPTN' + '.csv'
    res1 = list([testType, size, error, mean, confuMat[0, 0], confuMat[1, 1]])
    res1 = pd.DataFrame(res1)
    res1 = res1.T
    res1.to_csv(csv1, mode='a', header=False, index=False)


import sqlite3

print("THIS IS JAVA")

db = sqlite3.connect("data.db")
while 1:
    cpr = input("CPR:")
    name = "aasda"
    note = input('What is the issue: ') + '\n'
    stmt = db.cursor()
    stmt.execute('INSERT INTO users VALUES(?,?,?,?)', (cpr, name, note, 0))
    db.commit()


import argparse
import os
import numpy as np

import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.utils import save_image

from scipy.stats import norm

import matplotlib.pyplot as plt
plt.switch_backend('agg')

from datasets.bmnist import bmnist

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

class Encoder(nn.Module):

    def __init__(self, hidden_dim=500, z_dim=20):
        super().__init__()

        self.z_dim = z_dim
        self.hidden_dim = hidden_dim
        self.i2h = nn.Linear(784, hidden_dim)
        self.h2m = nn.Linear(hidden_dim, z_dim)
        self.h2s = nn.Linear(hidden_dim, z_dim)

    def forward(self, input):
        """
        Perform forward pass of encoder.

        Returns mean and std with shape [batch_size, z_dim]. Make sure
        that any constraints are enforced.
        """
        hidden = torch.relu(self.i2h(input))
        mean, logvar = self.h2m(hidden), self.h2s(hidden)

        return mean, logvar

class Decoder(nn.Module):

    def __init__(self, hidden_dim=500, z_dim=20):
        super().__init__()
        self.z2h = nn.Linear(z_dim, hidden_dim)
        self.h2m = nn.Linear(hidden_dim, 784)

    def forward(self, input):
        """
        Perform forward pass of encoder.

        Returns mean with shape [batch_size, 784].
        """
        hidden = torch.relu(self.z2h(input))
        mean = torch.sigmoid(self.h2m(hidden))

        return mean

class VAE(nn.Module):

    def __init__(self, hidden_dim=500, z_dim=20):
        super().__init__()

        self.z_dim = z_dim
        self.encoder = Encoder(hidden_dim, z_dim)
        self.decoder = Decoder(hidden_dim, z_dim)

    def forward(self, input):
        """
        Given input, perform an encoding and decoding step and return the
        negative average elbo for the given batch.
        """
        batch_dim = input.shape[0]
        mean, logvar = self.encoder(input)

        eps = torch.randn_like(mean)
        z = eps * torch.exp(0.5 * logvar) + mean

        x_hat = self.decoder(z)

        kl_divergence = -0.5 * torch.sum(1 + logvar - mean.pow(2) - torch.exp(logvar))
        binary_cross_entropy = F.binary_cross_entropy(x_hat, input, reduction='sum')

        # average loss over the batches
        average_negative_elbo = (kl_divergence + binary_cross_entropy)/batch_dim

        return average_negative_elbo

    def sample(self, n_samples=None, z_samples=None):
        """
        Sample n_samples from the model. Return both the sampled images
        (from bernoulli) and the means for these bernoullis (as these are
        used to plot the data manifold).
        """
        if n_samples is not None and z_samples is None:
            z_samples = torch.randn(n_samples, self.z_dim).to(device)
        means = self.decoder(z_samples)
        sampled_ims, im_means = torch.bernoulli(means), means

        return sampled_ims, im_means

def epoch_iter(model, data, optimizer):
    """
    Perform a single epoch for either the training or validation.
    use model.training to determine if in 'training mode' or not.

    Returns the average elbo for the complete epoch.
    """
    average_epoch_elbo = 0.0
    for input in data:
        input = input.view(-1, 784).to(device)

        if model.training:
            optimizer.zero_grad()
            elbo = model(input)
            elbo.backward()
            optimizer.step()
        else:
            elbo = model(input)

        average_epoch_elbo += elbo.item()

    average_epoch_elbo /= len(data)
    return average_epoch_elbo

def run_epoch(model, data, optimizer):
    """
    Run a train and validation epoch and return average elbo for each.
    """
    traindata, valdata = data

    model.train()
    train_elbo = epoch_iter(model, traindata, optimizer)

    model.eval()
    val_elbo = epoch_iter(model, valdata, optimizer)

    return train_elbo, val_elbo

def save_elbo_plot(train_curve, val_curve, filename):
    plt.figure(figsize=(12, 6))
    plt.plot(train_curve, label='train elbo')
    plt.plot(val_curve, label='validation elbo')
    plt.legend()
    plt.xlabel('epochs')
    plt.ylabel('ELBO')
    plt.tight_layout()
    plt.savefig(filename)

def main():
    os.makedirs(f'images/vae/{ARGS.zdim}', exist_ok=True)

    pin_memory = True if torch.cuda.is_available() else False
    data = bmnist(pin_memory=pin_memory)[:2]  # ignore test split
    model = VAE(z_dim=ARGS.zdim).to(device)
    optimizer = torch.optim.Adam(model.parameters())

    train_curve, val_curve = [], []
    for epoch in range(ARGS.epochs):
        elbos = run_epoch(model, data, optimizer)
        train_elbo, val_elbo = elbos
        train_curve.append(train_elbo)
        val_curve.append(val_elbo)
        print(f"[Epoch {epoch}] train elbo: {train_elbo} val_elbo: {val_elbo}")

        # --------------------------------------------------------------------
        #  Add functionality to plot samples from model during training.
        #  You can use the make_grid functioanlity that is already imported.
        # --------------------------------------------------------------------
        samples, _ = model.sample(n_samples = 25)
        samples = samples.view(25, 1, 28, 28)
        save_image(samples, os.path.join(f'images/vae/{ARGS.zdim}',
            f'{epoch}_{train_elbo}_{val_elbo}_samples.eps'),
                nrow=5, normalize=True
        )

    if ARGS.zdim == 2:
        # Display a 2D manifold of the digits

        # Construct grid of latent variable values
        grid = np.meshgrid(norm.ppf(np.linspace(0.00001, 1.0, 10, endpoint=False)),
                           norm.ppf(np.linspace(0.00001, 1.0, 10, endpoint=False)))
        cartesian_grid = torch.FloatTensor(np.array(grid).T.reshape((-1, 2))).to(device)
        _, means = model.sample(z_samples = cartesian_grid)
        save_image(means.view(-1, 1, 28, 28),
                   os.path.join(f'images/vae/{ARGS.zdim}', f'latent.eps'),
            nrow=10, normalize=True
        )
    save_elbo_plot(train_curve, val_curve, f'elbo_{ARGS.zdim}.pdf')

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--epochs', default=40, type=int,
                        help='max number of epochs')
    parser.add_argument('--zdim', default=2, type=int,
                        help='dimensionality of latent space')

    ARGS = parser.parse_args()

    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr  9 08:31:14 2019

@author: slavi
"""
# Simulation 1 --> Replicating the Primacy, Recency, List length effects
# Botvinick & Plaut, 2006

#  Overall accuracy for lists of six nonconfusable letters in this data set
# was 58%, and this provided the stopping criterion for our simulations

from Binding_Pool_parameters import *
from BP_STM_parameters import *
import numpy as np
from numpy import zeros, newaxis
from keras.models import load_model
import matplotlib.pyplot as plt

# LSTM model

ISR_model = load_model(
        'first_sr_model_lstm.h5', custom_objects={'my_accuracy': my_accuracy}
        )

list_len = 6
def make_input_trial():
    global letters_26
    list_len = 6
    trial_input = np.zeros(shape=(list_len * 2 + 1, len(letters_26) + 1))
    letters = np.random.permutation(len(letters_26))
    for i in range(list_len):
        #encoding
        trial_input[i, letters[i]] = 1
        #recall
        #recall cue
        trial_input[i + list_len, len(letters_26)] = 1
        #recall cue
    trial_input[list_len * 2, len(letters_26)] = 1

    return trial_input.reshape(1, list_len * 2 + 1, len(letters_26) + 1)

global position
position = np.array([0, 0, 0, 0, 0, 0])

for _ in range(1, 1000):
    trial = make_input_trial()
    prediction = ISR_model.predict(trial)
    prediction = prediction[0][-(list_len+1):]
    prediction = prediction[:-1]
    prediction = prediction[newaxis,:,:]
    trial = trial[0][:-(list_len+1)]
    trial = trial[newaxis,:,:]
    for pos, (position_p, position_t) in enumerate(zip(prediction[0], trial[0])):
        if np.argmax(position_p) == np.argmax(position_t):
            position[pos] += 1

# Plots the model’s accuracy on six-item lists,
# evaluated separately for each position.
def plot_prim_lstm():
    plt.plot(position)
    plt.xlabel('Item Position')
    plt.ylabel('Accuracy')
    plt.axis([0,6,0,1000])
    plt.show()

plot_prim_lstm()

#  List length effect

def make_input_trial_len(list_len):
    global letters_26
    trial_input = np.zeros(shape=(list_len * 2 + 1, len(letters_26) + 1))
    letters = np.random.permutation(len(letters_26))
    for i in range(list_len):
        #encoding
        trial_input[i, letters[i]] = 1
        #recall
        #recall cue
        trial_input[i + list_len, len(letters_26)] = 1
        #recall cue
    trial_input[list_len * 2, len(letters_26)] = 1

    return trial_input.reshape(1, list_len * 2 + 1, len(letters_26) + 1)

def examples_generator_len():
    max_list_len = 9
    while(True):
        for list_len in range(1, max_list_len + 1):
            yield make_input_trial_len(list_len)

len_check = examples_generator_len()

global proportion
global error
error = False
proportion_errors = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0])

for _ in range(900):
    trial = next(len_check)
    prediction = ISR_model.predict(trial)
    prediction = prediction[0][-(
            (
                    (len(trial[0])
                    -1)
                    //2)
                    +1):]
    prediction = prediction[:-1]
    prediction = prediction[newaxis,:,:]
    trial = trial[0][:-(
            (
                    (len(trial[0])
                    -1)
                    //2)
                    +1)]
    trial = trial[newaxis,:,:]
    pos_in_proportion = len(trial[0])-1

    for position_p, position_t in zip(prediction[0], trial[0]):
        if np.argmax(position_p) == np.argmax(position_t):
            error = False

        else:
            error = True
            break

    if error:
        proportion_errors[pos_in_proportion] += 1

def plot_w_len_lstm():
    plt.plot(proportion_errors)
    plt.xlabel('Number of items in a list')
    plt.ylabel('Errors')
    plt.axis([0,9,0,150])
    plt.show()

plot_w_len_lstm()


from django.utils.translation import ugettext_lazy as _

full_table_sets = (
    (None, {
        'fields': (
            'title',
            ('paging', 'searching'), ('ordering', 'lengthChange'), ('info', 'autoWidth', 'header_at_end')
        )
    }),
    (_('Export File Setting'), {
        'classes': ('collapse',),
        'fields': (
            ('file_copy', 'file_csv'),
            ('file_excel', 'file_pdf'),
            ('file_print',),
            ('html5_copy', 'html5_excel'),
            ('html5_csv', 'html5_pdf'),
            ('pid', 'reload_pids'),
        )
    }),
)

xy_axis_sets = (
    (_('XYAxis settings'), {
        'classes': ('collapse',),
        'fields': (
            'is_convert',
            'is_xaxislabel_align',
            'is_yaxislabel_align',
            'is_xaxis_inverse',
            'is_yaxis_inverse',
            'is_xaxis_boundarygap',
            'is_yaxis_boundarygap',
            'is_xaxis_show',
            'is_yaxis_show',
            'is_splitline_show',
            #
            # 'x_axis',  # should come from API
            'xaxis_interval',
            'xaxis_force_interval',
            'xaxis_margin',
            'xaxis_name',
            'xaxis_name_size',
            'xaxis_name_gap',
            'xaxis_name_pos',
            'xaxis_min',
            'xaxis_max',
            'xaxis_pos',
            'xaxis_label_textsize',
            'xaxis_label_textcolor',
            'xaxis_type',
            'xaxis_rotate',
            'xaxis_formatter',
            'xaxis_line_color',
            'xaxis_line_width',
            #
            # 'y_axis',  # should come from API
            'yaxis_interval',
            'yaxis_force_interval',
            'yaxis_margin',
            'yaxis_name',
            'yaxis_name_size',
            'yaxis_name_gap',
            'yaxis_name_pos',
            'yaxis_min',
            'yaxis_max',
            'yaxis_pos',
            'yaxis_label_textsize',
            'yaxis_label_textcolor',
            'yaxis_type',
            'yaxis_rotate',
            'yaxis_formatter',
            'yaxis_line_color',
            'yaxis_line_width',
        )
    }),
)

line_sets = (
    (_('Line settings'), {
        'classes': ('collapse',),
        'fields': (
            'is_symbol_show',
            'symbol_size',
            'is_smooth',
            'is_stack',
            'is_step',
            'is_fill',
        )
    }),
)


"""
actuators.py
Classes to control the motors and servos. These classes
are wrapped in a mixer class before being used in the drive loop.
"""

import time

import donkeycar as dk

from donkeycar.parts.configctrl import myConfig, CONFIG2LEVEL

import logging

class PCA9685:
    '''
    PWM motor controler using PCA9685 boards.
    This is used for most RC Cars
    '''
    def __init__(self, channel, frequency=60, **kwargs):
        import Adafruit_PCA9685
        # Initialise the PCA9685 using the default address (0x40).
        self.pwm = Adafruit_PCA9685.PCA9685( **kwargs)
        self.pwm.set_pwm_freq(frequency)
        self.channel = channel

    def set_pulse(self, pulse):
        self.pwm.set_pwm(self.channel, 0, pulse)

    def run(self, pulse):
        self.set_pulse(pulse)

class PWMSteering:
    """
    Wrapper over a PWM motor cotnroller to convert angles to PWM pulses.
    """
    LEFT_ANGLE = -1
    RIGHT_ANGLE = 1

    def __init__(self, controller=None):

        self.logger = logging.getLogger(myConfig['DEBUG']['PARTS']['ACT-STEERING']['NAME'])
        self.logger.setLevel(myConfig['DEBUG']['PARTS']['ACT-STEERING']['LEVEL'])
        self.controller = controller
        self.left_pulse = myConfig['ACTUATOR']['STEERING_LEFT_PULSE']
        self.right_pulse = myConfig['ACTUATOR']['STEERING_RIGHT_PULSE']
        self.perflogger = dk.perfmon.TaskCycle('SteeringOutput')

    def run(self, angle):
        #map absolute angle to angle that vehicle can implement.
        self.logger.debug('Output angle order= {:01.2f}'.format(angle))
        pulse = dk.utils.map_range(angle,
                                self.LEFT_ANGLE, self.RIGHT_ANGLE,
                                self.left_pulse, self.right_pulse)
#        print ("PWMSteering pulse="+str(pulse))
        self.logger.debug('Output angle pulse= {:03.0f}'.format(pulse))
        dk.perfmon.LogEvent('ActuatorSteering-setPulse')
        self.perflogger.LogCycle()
        self.controller.set_pulse(pulse, 1)

    def shutdown(self):
        self.run(0) #set steering straight

class PWMThrottle:
    """
    Wrapper over a PWM motor cotnroller to convert -1 to 1 throttle
    values to PWM pulses.
    """
    MIN_THROTTLE = -1
    MAX_THROTTLE =  1

    def __init__(self, controller=None):

        self.logger = logging.getLogger(myConfig['DEBUG']['PARTS']['ACT-THROTTLE']['NAME'])
        self.logger.setLevel(myConfig['DEBUG']['PARTS']['ACT-THROTTLE']['LEVEL'])
        self.mode = "user"
        self.kick = []
        #send zero pulse to calibrate ESC
        self.controller = controller
        self.controller.set_pulse(myConfig['ACTUATOR']['THROTTLE_STOPPED_PWM'], 0)
        self.fullspeed_hysteresis = 0
        self.brake_hysteresis = 0
        self.closeup_state = 0
        self.perflogger = dk.perfmon.TaskCycle('ThrottleOutput')
        time.sleep(1)

    def reloadKick(self):
        self.kick = [myConfig['ACTUATOR']['THROTTLE_KICK_PULSE']]*myConfig['ACTUATOR']['THROTTLE_KICK_LENGTH']
        self.logger.debug('Kicker reloaded')

    def run(self, throttle, mode=None, vehicle_armed=None, fullspeed=None, brake=None, lane=None, sensor_left=None, sensor_right=None):

        global myConfig

        if self.mode == "user" and mode != "user":
            self.closeup_state=0
            self.reloadKick()

        if (self.mode != "user" and mode == "user"):
            self.brake_hysteresis = myConfig['ACTUATOR']['BRAKE_HYSTERESIS_LENGTH']
            self.closeup_state=0

        self.mode = mode

        if (fullspeed==None):
            fullspeed = 0
        if (brake==None):
            brake=0

        self.logger.debug('throttle order= {:01.2f}'.format(throttle))

        if ((throttle > 0) or (self.mode != "user")):
            #Forward direction
            pulse = dk.utils.map_range(throttle,
                                    0, self.MAX_THROTTLE,
                                    myConfig['ACTUATOR']['THROTTLE_STOPPED_PWM'], myConfig['ACTUATOR']['THROTTLE_FORWARD_PWM'])
            if (self.mode != "user"):
                # Autonomous mode
                if (myConfig['ACTUATOR']['THROTTLE_CONSTANT_MODE'] == 1):
                    # If constant mode, just apply always kick  as nominal value
                    self.logger.debug('constant speed mode : fullspeed prediction = '+str(fullspeed) + ' brake_decision = '+str(brake))
                    if (brake > myConfig['ACTUATOR']['BRAKE_DECISION_THRESH']):
                        self.logger.debug('constant speed mode : brake')
                        self.brake_hysteresis = myConfig['ACTUATOR']['BRAKE_HYSTERESIS_LENGTH']
                    elif (fullspeed > myConfig['ACTUATOR']['FULLSPEED_DECISION_THRESH']):
                        self.logger.debug('constant speed mode : fullspeed')
                        self.fullspeed_hysteresis = myConfig['ACTUATOR']['FULLSPEED_HYSTERESIS_LENGTH']
                    else:
                        self.logger.debug('constant speed mode : regular speed')
                        pulse = myConfig['ACTUATOR']['THROTTLE_KICK_PULSE']
                else:
                    # Not in constant mode, Ensure thottle order would not go below a limit (risk of motor shutdown)
                    if pulse < myConfig['ACTUATOR']['THROTTLE_MIN_SPD_PULSE']:
                        self.logger.debug('PWMThrottle order too low')
                        pulse = myConfig['ACTUATOR']['THROTTLE_MIN_SPD_PULSE']

                # Motor cann not start at too low throttle, kick it for the first cycles
                if (len(self.kick)>0):
                    self.logger.debug('Kicker active')
                    pulse = self.kick.pop()

        else:
            #reverse direction in manual mode
            pulse = dk.utils.map_range(throttle,
                                    self.MIN_THROTTLE, 0,
                                    myConfig['ACTUATOR']['THROTTLE_REVERSE_PWM'], myConfig['ACTUATOR']['THROTTLE_STOPPED_PWM'])

        if (self.brake_hysteresis>0):
            self.logger.debug('Apply brake for next '+str(self.brake_hysteresis-1)+' cycle')
            pulse = myConfig['ACTUATOR']['THROTTLE_BRAKE_PULSE']
            self.brake_hysteresis -= 1
            if (self.brake_hysteresis == 0):
                self.reloadKick()

        if (self.fullspeed_hysteresis>0):
            self.logger.debug('Apply fullspeed for next '+str(self.fullspeed_hysteresis-1)+' cycle')
            pulse = myConfig['ACTUATOR']['THROTTLE_FULLSPEED_PULSE']
            self.fullspeed_hysteresis -= 1

        if (myConfig['ACTUATOR']['CHALLENGE_CLOSEUP']==1 and self.mode != "user"):
            #rng = max(sensor_left, sensor_right)
            rng = (sensor_left + sensor_right)/2.0
            self.logger.debug('Closeup: Sensor fusion :'+str(rng))

            if (self.closeup_state == 0 and rng<=myConfig['ACTUATOR']['CHALLENGE_TRESH_SLOW']):
                #In normal state, no object detected ahead, regular speed
                pulse=myConfig['ACTUATOR']['THROTTLE_KICK_PULSE']

            if (self.closeup_state == 0 and rng>myConfig['ACTUATOR']['CHALLENGE_TRESH_SLOW']):
                #In normal state, object detected ahead, slowdown
                self.logger.debug('Closeup: Switch to state 1')
                self.closeup_state=1

            if (self.closeup_state==1):
                if (rng<=myConfig['ACTUATOR']['CHALLENGE_TRESH_STOP']):
                    #In slow state, use min speed
                    pulse=myConfig['ACTUATOR']['THROTTLE_MIN_SPD_PULSE']
                    if (rng<=(myConfig['ACTUATOR']['CHALLENGE_TRESH_STOP']-myConfig['ACTUATOR']['CHALLENGE_TRESH_STATE'])):
                        #In slow state, no more object detected ahead, switch back to normal state
                        self.logger.debug('Closeup: Switch to state 0')
                        self.closeup_state=0

                else:
                    #In slow state, very close to object ahead, switch to mode 2
                    self.logger.debug('Closeup: Switch to state 2')
                    self.closeup_state=2

            if (self.closeup_state==2):
                #In stop state, stop car
                pulse=myConfig['ACTUATOR']['THROTTLE_STOPPED_PWM']

            self.logger.debug('Closeup: state :'+str(self.closeup_state))

        self.logger.debug('Output throttle pulse= {:03.0f}'.format(pulse))
        dk.perfmon.LogEvent('ActuatorThrottle-setPulse')
        self.perflogger.LogCycle()
        if (vehicle_armed == True or myConfig['ACTUATOR']['USE_ARM_LOGIC']==0):
            self.controller.set_pulse(pulse, 0)
        else:
            self.controller.set_pulse(1500, 0)

    def shutdown(self):
        self.controller.set_pulse(1500, 0) #stop vehicle

    def gracefull_shutdown(self):
        self.controller.set_pulse(1500, 0) #stop vehicle

class Adafruit_DCMotor_Hat:
    '''
    Adafruit DC Motor Controller
    Used for each motor on a differential drive car.
    '''
    def __init__(self, motor_num):
        from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor
        import atexit

        self.FORWARD = Adafruit_MotorHAT.FORWARD
        self.BACKWARD = Adafruit_MotorHAT.BACKWARD
        self.mh = Adafruit_MotorHAT(addr=0x60)

        self.motor = self.mh.getMotor(motor_num)
        self.motor_num = motor_num

        atexit.register(self.turn_off_motors)
        self.speed = 0
        self.throttle = 0

    def run(self, speed):
        '''
        Update the speed of the motor where 1 is full forward and
        -1 is full backwards.
        '''
        if speed > 1 or speed < -1:
            raise ValueError( "Speed must be between 1(forward) and -1(reverse)")

        self.speed = speed
        self.throttle = int(dk.utils.map_range(abs(speed), -1, 1, -255, 255))

        if speed > 0:
            self.motor.run(self.FORWARD)
        else:
            self.motor.run(self.BACKWARD)

        self.motor.setSpeed(self.throttle)

    def shutdown(self):
        self.mh.getMotor(self.motor_num).run(Adafruit_MotorHAT.RELEASE)

class Maestro:
    '''
    Pololu Maestro Servo controller
    Use the MaestroControlCenter to set the speed & acceleration values to 0!
    '''
    import threading

    maestro_device = None
    astar_device = None
    maestro_lock = threading.Lock()
    astar_lock = threading.Lock()

    def __init__(self, channel, frequency = 60):
        import serial

        if Maestro.maestro_device == None:
            Maestro.maestro_device = serial.Serial('/dev/ttyACM0', 115200)

        self.channel = channel
        self.frequency = frequency
        self.lturn = False
        self.rturn = False
        self.headlights = False
        self.brakelights = False

        if Maestro.astar_device == None:
            Maestro.astar_device = serial.Serial('/dev/ttyACM2', 115200, timeout= 0.01)

    def set_pulse(self, pulse):
        # Recalculate pulse width from the Adafruit values
        w = pulse * (1 / (self.frequency * 4096)) # in seconds
        w *= 1000 * 1000  # in microseconds
        w *= 4  # in quarter microsenconds the maestro wants
        w = int(w)

        with Maestro.maestro_lock:
            Maestro.maestro_device.write(bytearray([ 0x84,
                                                     self.channel,
                                                     (w & 0x7F),
                                                     ((w >> 7) & 0x7F)]))

    def set_turn_left(self, v):
        if self.lturn != v:
            self.lturn = v
            b = bytearray('L' if v else 'l', 'ascii')
            with Maestro.astar_lock:
                Maestro.astar_device.write(b)

    def set_turn_right(self, v):
        if self.rturn != v:
            self.rturn = v
            b = bytearray('R' if v else 'r', 'ascii')
            with Maestro.astar_lock:
                Maestro.astar_device.write(b)

    def set_headlight(self, v):
        if self.headlights != v:
            self.headlights = v
            b = bytearray('H' if v else 'h', 'ascii')
            with Maestro.astar_lock:
                Maestro.astar_device.write(b)

    def set_brake(self, v):
        if self.brakelights != v:
            self.brakelights = v
            b = bytearray('B' if v else 'b', 'ascii')
            with Maestro.astar_lock:
                Maestro.astar_device.write(b)

    def readline(self):
        ret = None
        with Maestro.astar_lock:
            # expecting lines like
            # E n nnn n
            if Maestro.astar_device.inWaiting() > 8:
                ret = Maestro.astar_device.readline()

        if ret != None:
            ret = ret.rstrip()

        return ret

class Teensy:
    '''
    Teensy Servo controller
    '''
    import threading

    teensy_device = None
    astar_device = None
    teensy_lock = threading.Lock()
    astar_lock = threading.Lock()

    def __init__(self, channel, frequency = 60):
        import serial

        if Teensy.teensy_device == None:
            Teensy.teensy_device = serial.Serial('/dev/teensy', 115200, timeout = 0.01)

        self.channel = channel
        self.frequency = frequency
        self.lturn = False
        self.rturn = False
        self.headlights = False
        self.brakelights = False

        if Teensy.astar_device == None:
            Teensy.astar_device = serial.Serial('/dev/astar', 115200, timeout = 0.01)

    def set_pulse(self, pulse):
        # Recalculate pulse width from the Adafruit values
        w = pulse * (1 / (self.frequency * 4096)) # in seconds
        w *= 1000 * 1000  # in microseconds

        with Teensy.teensy_lock:
            Teensy.teensy_device.write(("%c %.1f\n" % (self.channel, w)).encode('ascii'))

    def set_turn_left(self, v):
        if self.lturn != v:
            self.lturn = v
            b = bytearray('L' if v else 'l', 'ascii')
            with Teensy.astar_lock:
                Teensy.astar_device.write(b)

    def set_turn_right(self, v):
        if self.rturn != v:
            self.rturn = v
            b = bytearray('R' if v else 'r', 'ascii')
            with Teensy.astar_lock:
                Teensy.astar_device.write(b)

    def set_headlight(self, v):
        if self.headlights != v:
            self.headlights = v
            b = bytearray('H' if v else 'h', 'ascii')
            with Teensy.astar_lock:
                Teensy.astar_device.write(b)

    def set_brake(self, v):
        if self.brakelights != v:
            self.brakelights = v
            b = bytearray('B' if v else 'b', 'ascii')
            with Teensy.astar_lock:
                Teensy.astar_device.write(b)

    def teensy_readline(self):
        ret = None
        with Teensy.teensy_lock:
            # expecting lines like
            # E n nnn n
            if Teensy.teensy_device.inWaiting() > 8:
                ret = Teensy.teensy_device.readline()

        if ret != None:
            ret = ret.rstrip()

        return ret

    def astar_readline(self):
        ret = None
        with Teensy.astar_lock:
            # expecting lines like
            # E n nnn n
            if Teensy.astar_device.inWaiting() > 8:
                ret = Teensy.astar_device.readline()

        if ret != None:
            ret = ret.rstrip()

        return ret

class MockController(object):
    def __init__(self):
        pass

    def run(self, pulse):
        pass

    def shutdown(self):
        pass

{'threading': 'threading', 'serial': 'serial'}.maestro_lock

{'threading': 'threading', 'serial': 'serial'}.maestro_device.write

{'threading': 'threading', 'serial': 'serial'}.astar_lock

{'threading': 'threading', 'serial': 'serial'}.astar_device.write

{'threading': 'threading', 'serial': 'serial'}.astar_device.inWaiting

{'threading': 'threading', 'serial': 'serial'}.astar_device.readline

{'threading': 'threading', 'serial': 'serial'}.teensy_lock

{'threading': 'threading', 'serial': 'serial'}.teensy_device.write

{'threading': 'threading', 'serial': 'serial'}.teensy_device.inWaiting

{'threading': 'threading', 'serial': 'serial'}.teensy_device.readline


import logging
import requests
from urllib import parse
from PIL import Image
import io
import boto3

client = boto3.client('rekognition')
s3 = boto3.client(
   "s3"
)
s3_resource = boto3.resource('s3')
def is_url(path):
    """
    Checks if given path is a URL

    Args:
        path (str): input path

    Returns:
        bool : True if URL else False
    """
    return parse.urlparse(path).scheme in ['http','https','ftp']

def get_file_name(url):

    """
    Returns filename from given URL

    Args:
        url (str): input url

    Returns
        str: filename
    """
    slug = url.split('/')[-1]
    try:
        slug = slug.split('?')[0]
        return slug
    except:
        return slug

def allowed_file(filename):
    """
    Check whether given file is allowed
    Args:
        filename (str): input filename
    Returns:
        bool: True if file is images else False
    """
    try:
        format = filename.split('.')[-1]
    except:
        format = None
    if format in ['jpg','png','jpeg']:
        return True
    return False

def upload_file_to_s3(file, bucket_name,get_url=False, acl="public-read"):
    """
    Uploads file to given s3 bucket

    Args:
        file (obj): File object to be uploaded
        bucket_name (str): Name of destination bucket
        get_url (bool): True if user wants url after uploading
        acl (str):Access control list
    Returns:
        bool/str: url if get_url is True else True if successfully uploaded else False
    """
    try:
        s3.upload_fileobj(
            file,
            bucket_name,
            file.filename,
            ExtraArgs={
                "ACL": acl,
                "ContentType": file.content_type
            }
        )
        object_name = file.filename
        url = f"https://{bucket_name}.s3.amazonaws.com/{object_name}"
    except Exception as e:
        # This is a catch all exception, edit this part to fit your needs.
        logging.error("Something Happened: ", e)
        return False
    return url if get_url else True

def delete_from_s3(url):
    bucket_name = url.split('.')[0].split("//")[-1]
    object_name = url.split('/')[-1]

    obj = s3_resource.Object(bucket_name,object_name)
    try:
        obj.delete()
        return True
    except Exception as e:
        logging.error(e)
        return False

def get_facial_features(client,path=None):
    """
    Get facial feautre from AWS Rekognition
    Args:
        client (obj): boto client object
        path (str): path of file
    Returns:
        dict/None: Response from AWS Rekognition if exceuted successfully else None
    """
    if path is None:
        return None
    if is_url(path):
        image = requests.get(path,stream=True).raw
    else:
        image = open(path,'rb')

    if image is not None:
        try:
            response = client.detect_faces(Image={'Bytes': image.read()},Attributes=['ALL'])
            return response
        except Exception as e :
            logging.error(e)

            return None

def get_emotion(emotions):
    confidence_list = list(map(lambda x:x['Confidence'],emotions))
    emotion_list = list(map(lambda x:x['Type'],emotions))
    #find index of confidence with max confidence
    idx = confidence_list.index(max(confidence_list))
    return emotion_list[idx]

def get_gender(gender):
    return gender['Value'],gender['Confidence']

def get_age_range(ageRange):
    return f"{ageRange['Low']} to {ageRange['High']} years"

def get_sunglasses(sunglasses):
    return f"has sunglasses" if sunglasses['Value']  else f"has no sunglasses"

def get_eyeglasses(eyeglasses):
    return f"has eyeglasses" if eyeglasses['Value']  else f"has no eyeglasses"

def get_mustache(mustache):
    return f"has mustache" if mustache['Value']  else f"has no mustache"

def get_beard(beard):
    return f"has beard" if beard['Value']  else f"has no beard"

def generate_content(img_url):
    """
    Generate meaningful summary
    Args:
        img_url (str): URL of target image

    Returns:
        dict: meaningful summary of image
    """
    response = get_facial_features(client,path=img_url)
    content = {'FaceDetails':[]}
    try:
        for image in response['FaceDetails']:
            content['FaceDetails'].append({'Emotion':get_emotion(image['Emotions']),
                                            'Gender':get_gender(image['Gender']),
                                            'AgeRange':get_age_range(image['AgeRange']),
                                            'Sunglasses':get_sunglasses(image['Sunglasses']),
                                            'Eyeglasses':get_eyeglasses(image['Eyeglasses']),
                                            'Mustache':get_mustache(image['Mustache']),
                                            'Beard':get_beard(image['Beard']),
                                            })

        return content
    except:
        content['FaceDetails'].append({'Error':"An error occured at server. Please try again later"})
        return content